Posting and retrieving a JSON object via REST call in java
I have a Rest endpoint (jersey based) which accepts a JSON object which I retrieve by mapping it to a POJO, eg
@POST
@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
public void getResult(PojoClass pojo)
My PojoClass is:
@XmlRootElement
public class PojoClass {
private List<String> list;
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
}
Now if I send a json data via curl command:
curl -H "Content-type: application/json" -i -X 'POST' -d @/tmp/xyz.json http://127.0.0.1:8080/test
I am able to get it mapped properly into my PojoClass.
xvz.json is:
{
"list":[
"123",
"456"
]
}
The list of PojoClass will have two elements ("123" and "456").
But if do a post call from JAVA. And I am sending the same json structure as payload, it is being received as a PojoClass with list as single element, which is a concatenation like ["123","456"]
I am using "HttpURLConnection" to make a post call from java.
Is something extra needed to get the same result as cURL command ?
It was a library conflict between JSONObject and JSONArray which was corrupting my JSON in request Payload. When I handled this error, request went just fine and everything worked like charm.
链接地址: http://www.djcxy.com/p/48788.html