2012-06-28 9 views
10

Usando los JAX-RS (Jersey) Estoy tratar de implementar una petición POST que tienen una lista de objetos JSONJAX-RS (Jersey) a Consume Matriz de objeto JSON en solicitud 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; 
} 
} 

cómo cada vez cuando trato de probar la API REST usando curl, siempre obtengo un error de "solicitud incorrecta", ¿me falta algo aquí?

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

Así que, ¿cómo solucionarlo? De acuerdo con la respuesta del usuario311174 no hay soporte para un mapeo directo de json. ¿Es eso cierto? – OneWorld

Respuesta

3

Si no te importa el cambio de la firma de su método:

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

En el lado del servidor:

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 
} 

Y en el lado del cliente:

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

una respuesta tardía, pero puede ser útil para otros Agregar:

[{ "etiqueta": "valor" "abc": "ghi"}, { "etiqueta": "123", "valor": "456"}]

Debido a que mediante el envío de esto:

{ "someObj": [{ "etiqueta": "valor" "abc": "ghi"}, { "etiqueta": "123", "valor" : "456"}]}

está publicando un objeto con una sola propiedad llamada 'SomeObj'. no se está fijando una serie

+0

Porque, ¿quién, como yo, está enfrentando el mismo problema ahora, esta respuesta es la que trabajó para mí. Actualmente estoy usando Jax-RS (Resteasy) con JBoss EAP 6.3; D –

+0

Pero, ¿cómo soportas application/xml con eso? De esta forma solo puedo publicar json pero no un xml – jlanza

0

trate de envolver su matriz JSON dentro de un objeto como:

@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 
Cuestiones relacionadas