2012-06-28 11 views
10

Utilizzando i JAX-RS (Jersey) sto cerco di implementare una richiesta POST che prende una lista di oggetti JSONJAX-RS (Jersey) a Consuma Array di oggetto JSON a richiesta POST

//The resource look like this 
@Path("/path") 
@POST 
@Consumes(MediaType.APPLICATION_JSON) 
public void setJsonl(List<SomeObj> test) { 
    //do work 
    System.out.println(test); 
} 


//The class to define the json structure 
@XmlRootElement 
public class SomeObj{ 

private String tag; 
private String value; 

public String getTag() { 
return tag; 
} 

public void setTag(String tag) { 
    this.tag = tag; 
} 

public String getValue() { 
    return value; 
} 

public void setValue(String value) { 
    this.value = value; 
} 
} 

come mai quando provo a testare l'API REST usando curl, ottengo sempre un errore di "richiesta errata", mi manca qualcosa qui?

curl -X POST -H "Content-Type: application/json" -d '{"SomeObj":[{"tag":"abc", "value":"ghi"},{"tag":"123", "value":"456"}]}' http://{host_name}:8080/path_to_resource 
+2

Quindi, come hai fatto a risolverlo? Secondo la risposta dell'utente311174 non c'è supporto per una mappatura diretta di JSON. È vero? – OneWorld

risposta

3

Se non ti dispiace cambiare la firma del metodo:

import org.json.JSONArray; 

    //The resource look like this 
    @Path("/path") 
    @POST 
    @Consumes(MediaType.APPLICATION_JSON) 
    public void setJsonl(String array){ 
     JSONArray o = new JSONArray(last_data); 
     System.out.println(o.toString()); 
-2

Sul lato server:

import _root_.org.codehaus.jettison.json.{JSONArray, JSONObject} 
@POST 
@Path("/wants-json-array") 
@Consumes(Array(MediaType.APPLICATION_JSON)) 
def wantsJSONArray(array: JSONArray): Response = 
{ 
    // here's your array 
} 

E sul lato client:

$.ajax(
{ 
    type: "GET", 
    url: '/your-web-service/wants-json-array', 
    data: JSON.stringify(THEARRAYYOUARESENDINGTOTHESERVER), 
    contentType: "application/json", 
    dataType: "json", 
    processData: false 
}); 
2

una risposta in ritardo ma potrebbe essere utile per gli altri Messaggio questo:

[{ "tag": "abc", "valore": "ghi"}, { "tag": "123", "valore": "456"}]

Perché con l'invio di questo:

{ "someObj": [{ "tag": "abc", "valore": "ghi"}, { "tag": "123", "valore" : "456"}]}

si sta postando un oggetto con una singola proprietà denominata 'SomeObj'. non si sta scrivendo una serie

+0

Perché chi come me si trova di fronte allo stesso problema ora-a-giorni, questa risposta è quella che ha funzionato per me. Attualmente sto usando Jax-RS (Resteasy) con JBoss EAP 6.3; D –

+0

Ma come si supporta application/xml con quello? In questo modo ho solo potuto pubblicare json ma non un xml – jlanza

0

Prova avvolgendo l'array JSON all'interno di un oggetto come:

@XmlRootElement 
public class SomeObjListWrapper { 
private List<SomeObj> list; 
// getters and setters 
} 

curl -X POST -H "Content-Type: application/json" -d '{"list":[{"tag":"abc", "value":"ghi"},{"tag":"123", "value":"456"}]}' http://{host_name}:8080/path_to_resource 
Problemi correlati