首页 > 代码库 > Jersey Client传递中文参数

Jersey Client传递中文参数

客户端需要客户端的包:

        <dependency>            <groupId>com.sun.jersey</groupId>            <artifactId>jersey-client</artifactId>            <version>1.18</version>        </dependency>

版本和之前对应。

代码如下:

package client;import com.sun.jersey.api.client.Client;import com.sun.jersey.api.client.ClientResponse;import com.sun.jersey.api.client.WebResource;import java.io.UnsupportedEncodingException;import java.net.URLEncoder;/** * Created by wl on 2014/10/15. */public class RestfulClient {    public static void main(String args[]){        String url="http://localhost:8080/services/hello/";        Client client=Client.create();        try {            url+= URLEncoder.encode("你好啊","utf-8");        } catch (UnsupportedEncodingException e) {            e.printStackTrace();        }        WebResource resource=client.resource(url);        ClientResponse response=resource.get(ClientResponse.class);        String entity=response.getEntity(String.class);        System.out.println(entity);    }}

他对应的服务端方法:

    //带参数    @GET    @Produces(MediaType.TEXT_PLAIN)    @Path("{name}")    public String sayHello(@PathParam("name")String name){        return "hello,"+name;    }

所以,结果是打印:

hello,你好啊

Jersey Client传递中文参数