泽西岛客户端响应状态 204

发布于 2025-01-08 20:23:39 字数 1940 浏览 0 评论 0原文

我将 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());
}

我真的不知道问题出在哪里。我知道这里有另一个问题,主题看似相同,但事实并非如此。

如果我遗漏了什么或者您需要任何额外信息,请告诉我。

I am using Jersey for both service and client. When I am trying to call the service, I get this error:

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)

I do not understand why.

Here is the service:

@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;
}

and this is the client:

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());
}

I really have no idea about what the problem could be. I am aware of another question here with a seemingly identical subject, but they are not.

Please let me know if I am missing something or if you need any extra info.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧伤还要旧人安 2025-01-15 20:23:39

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

JSONArray array = new JSONArray(); // should fix the issue :)

204 is an HTTP response status code informing the client that there is no content returned.
When your client calls get(JSONArray.class) it's expecting 200 with data, hence the exception.
It looks from your server implementation that the array variable is never instantiated, so if your list were not empty, it would likely NPE in array.add(), but in this case it look like your list may be empty, so the for loop is not iterated up and getOverviewEntities method returns null, hence the 204 result.

JSONArray array = new JSONArray(); // should fix the issue :)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文