Jersey customer response status 204

I use jersey for service and customer. When I try to call a 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 don't know what the problem is. I know of another question here with a seemingly identical theme, but it is not.

Please let me know if I am missing something or you need more information.

+6
source share
1 answer

204 is an HTTP response status code informing the client that there is no returned content. When your client calls get (JSONArray.class), it expects 200 with data, hence an exception. You can see from your server implementation that an array variable is never created, so if your list was not empty, it is most likely NPE in array.add (), but in this case it looks like your list may be empty. therefore, the for loop does not repeat, and the getOverviewEntities method returns null, hence the result is 204.

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

Source: https://habr.com/ru/post/909362/


All Articles