我正在为服务和客户端使用 Jersey。当我尝试调用该服务时,出现此错误:
Exception in thread "main" com.sun.jersey.api.client.UniformInterfaceException: GET http://localhost:8080/Maze/rest/service/overview?countryid=1 returned a response status of 204 No Content
at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:528)
at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:506)
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:674)
at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
at com.sun.jersey.api.client.WebResource$Builder.get(WebResource.java:503)
at com.maze.client.MyClient.overviewTest(MyClient.java:34)
at com.maze.client.MyClient.main(MyClient.java:64)
我不明白为什么。
这是服务:
@GET
@Produces(MediaType.APPLICATION_JSON )
@Path("/overview")
public JSONArray getOverviewEntities(@QueryParam("countryid")String id){
JSONArray array = null;
try{
Integer countryId = Integer.parseInt(id);
ArrayList<Event> list = new ArrayList<Event>();
EventService event = new EventService();
EntityManagerSingleton.getInstance().getTransaction().begin();
list.addAll(event.getList(countryId, "country", 5));
EntityManagerSingleton.getInstance().getTransaction().commit();
for(Event ev : list){
array.add(EventService.toJSONObject(ev));
}
} catch(Exception e){
e.printStackTrace();
}
return array;
}
这是客户端:
public static void overviewTest(){
WebResource wbr;
Client client = Client.create();
wbr = client.resource("http://localhost:8080/Maze/rest/service/overview");
JSONArray result = wbr.queryParam("countryid", "1").accept(MediaType.APPLICATION_JSON).get(JSONArray.class);
System.out.println(result.toString());
}
我真的不知道问题出在哪里。我知道这里有一个看似相同的主题的另一个问题,但事实并非如此。
如果我遗漏了什么或者您需要任何额外信息,请告诉我。
最佳答案
204 是一个 HTTP 响应状态码,通知客户端没有返回内容。 当您的客户端调用 get(JSONArray.class) 时,它期望 200 包含数据,因此异常。 从你的服务器实现来看,数组变量永远不会被实例化,所以如果你的列表不为空,它可能会在 array.add() 中出现 NPE,但在这种情况下,你的列表看起来可能是空的,所以 for 循环没有向上迭代并且 getOverviewEntities 方法返回 null,因此返回 204 结果。
JSONArray array = new JSONArray(); // should fix the issue :)
关于java - 泽西客户端响应状态 204,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9447981/