2012-06-28 3 views
10

Verwendung der JAX-RS (Jersey) Ich versuche eine POST-Anforderung zu implementieren, die eine Liste von JSON-Objekt nehmenJax-rs (Jersey) zu Verbraucht Array von JSON-Objekt in POST-Anfrage

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

wie immer Wenn ich versuche, die REST-API mit curl zu testen, bekomme ich immer einen Fehler "schlechte Anfrage", vermisse ich hier etwas?

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

Also, wie haben Sie es lösen? Nach der Antwort von user311174 gibt es keine Unterstützung für eine direkte Zuordnung von json. Ist das wahr? – OneWorld

Antwort

3

Wenn Sie nichts dagegen haben die Signatur Ihrer Methode zu ändern:

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

Auf der Serverseite:

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 
} 

Und auf der Client-Seite:

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

eine späte Antwort, aber möglicherweise hilfreich für andere Post this:

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

Denn durch diese gesendet:

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

Sie veröffentlichen ein Objekt mit einer einzigen 'SomeObj' genannten Eigenschaft. Sie sind nicht ein Array Posting

+0

Denn wer mich mag, steht heute vor dem gleichen Problem, diese Antwort ist diejenige, die für mich gearbeitet hat. Ich benutze gerade Jax-RS (Resteasy) mit JBoss EAP 6.3; D –

+0

Aber wie unterstützt man dann application/xml? Auf diese Weise kann ich nur json, aber kein xml posten – jlanza

0

wie Ihr JSON-Array innerhalb eines Objekts Versuchen Verpackung:

@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