11

Using the jax-rs(Jersey) I am try to implement a POST request that take a list of JSON object

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

how ever when I try to test the REST api using curl I always get a "bad request" error, am I missing something here?

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
Oleksi
  • 12,802
  • 4
  • 55
  • 79
LOK
  • 339
  • 2
  • 4
  • 14
  • 2
    So, how did you solve it? According to user311174's answer there is no support for a direct mapping of json. Is that true? – OneWorld Jul 25 '12 at 09:46

4 Answers4

4

If you don't mind changing the signature of your method:

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());
user311174
  • 1,708
  • 1
  • 18
  • 17
2

a late answer but may be helpful for others Post this:

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

Because by sending this:

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

you are posting an object with a single 'SomeObj' named property. you are not posting an array

  • For does who like myself are facing the same problem now-a-days, this answer is the one who worked for me. I'm currently using Jax-RS (Resteasy) with JBoss EAP 6.3 ;D – Kim Aragon Escobar Sep 24 '16 at 18:38
  • But then how do you support application/xml with that? This way I have only being able to post json but not a xml – jlanza Aug 23 '17 at 15:43
0

Try wrapping your JSON array inside an object like:

@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
guilhebl
  • 7,660
  • 10
  • 43
  • 65
-3

On the server side:

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
}

And on the client side:

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