If we try to return a JsonOject in jersey, like this
@Path("/formSubmit")
@POST
@Produces("application/json")
public JSONObject formSubmit()
{
JSONObject json1 =new JSONObject();
return json1;
}
it may give an error (org.codehaus.jackson.map.JsonMappingException: No serializer found for class org.json.JSONObject and no properties discovered to create BeanSerializer)
org.codehaus.jackson.map.JsonMappingException: No serializer found for class org.json.JSONObject and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) )
org.codehaus.jackson.map.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:52)
org.codehaus.jackson.map.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:25)
org.codehaus.jackson.map.ser.StdSerializerProvider._serializeValue(StdSerializerProvider.java:610)
org.codehaus.jackson.map.ser.StdSerializerProvider.serializeValue(StdSerializerProvider.java:256)
org.codehaus.jackson.map.ObjectMapper.writeValue(ObjectMapper.java:1604)
To avoid this , first return a json by using
@Produces("application/json").
This determines that Jersey will know that the output is in json format.
Now inspite of using JSONObject, we can use any class and transform it in json by putting
@XmlRootElement
public class Employee {}
Here, json will represent all the attributes of class.
@POST
@Consumes("application/json")
public Employee formSubmit(){
return employee;
}
0 Comment(s)