泽西客户响应状态204

时间:2012-02-25 20:56:50

标签: java web-services jersey

我正在使用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());
}

我真的不知道问题可能是什么。我知道另一个问题,看似相同的主题,但他们不是。

如果我遗失了某些内容或者您需要任何额外信息,请告诉我。

1 个答案:

答案 0 :(得分:7)

204是HTTP响应状态代码,通知客户端没有返回内容。 当您的客户端调用get(JSONArray.class)时,它期望有200个数据,因此异常。 从服务器实现看,数组变量从未实例化,所以如果你的列表不是空的,它可能是array.add()中的NPE,但在这种情况下看起来你的列表可能是空的,所以for循环没有迭代,getOverviewEntities方法返回null,因此得到204结果。

JSONArray array = new JSONArray(); // should fix the issue :)
相关问题