自己使用的测试用例
package com.sides.test;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
@Path("/helloGet/{username}")
public class HelloworldGet {
@GET
@Produces("text/plain")
public String sayHello(@PathParam("username") String username) throws Exception{
// username = "中文"
System.out.println("username == "+ username);
String result = username;//URLEncoder.encode(username,"UTF-8");
return result;
}
public static void main(String[] args){
Client c = Client.create();
String url = "http://localhost:8080/JerseyTest/rest/helloGet/";
try {
url += URLEncoder.encode("张三", "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
WebResource r = c.resource(url);
ClientResponse response = r.get(ClientResponse.class);
String entity = response.getEntity(String.class);
//WebResource r = c.resource(url);
//String entity = r.get(String.class);
//注释部分是另一种方式
System.out.println(entity);
}
}