2012-02-25 18 views
6

Utilizzo Jersey sia per assistenza che per cliente. Quando io sto cercando di chiamare il servizio, ottengo questo errore:Jersey Stato di risposta del cliente 204

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) 

Non capisco perché.

Qui è il servizio:

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

e questo è il cliente:

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

ho davvero idea di quale sia il problema potrebbe essere. Sono a conoscenza di un'altra domanda qui con un argomento apparentemente identico, ma non lo sono.

Per favore fatemi sapere se mi manca qualcosa o se avete bisogno di ulteriori informazioni.

risposta

6

204 è un codice di stato della risposta HTTP che informa il client che non è stato restituito alcun contenuto. Quando il client chiama get (JSONArray.class) si aspetta 200 con dati, quindi l'eccezione. Dall'implementazione del server sembra che la variabile dell'array non sia mai istanziata, quindi se il tuo elenco non fosse vuoto, sarebbe probabilmente NPE in array.add(), ma in questo caso sembra che il tuo elenco possa essere vuoto, quindi il il ciclo non viene iterato e il metodo getOverviewEntities restituisce null, quindi il risultato 204.

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

Sì, questo ha risolto il problema. Ora sto ottenendo uno stato 500 (Internal Server Error). Ma questa è un'altra domanda, immagino. Molte grazie! – Dragos

Problemi correlati