I would like to write a simple method that receives a json that represents a POJO and just use that POJO.
Example (receiving and returning a POJO):
@Put("json")
public Representation b(JacksonRepresentation<Device> deviceRepresentation)
throws IOException {
Device device = deviceRepresentation.getObject();
// Use the device
return new JacksonRepresentation<Device>(device);
}
The above example throws an exception: Conflicting setter definitions for property "locationRef"...
Another option would have been using a JsonRepresentation, but I couldn't find a way to convert it to a POJO:
@Put("json")
public Representation b(JsonRepresentation jsonRepresentation) {
// How to convert the jsonRepresentation to a POJO???
return new JsonRepresentation(new Device("2", 2));
}
Jackson sounds a better tool for the job, as it has generic, thus mech more type safe - if only it would work...
Ne need to use any representation object. The following worked beautifully, using jackson in the background:
@Put
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Device b(Device device) {
// Do something with the POJO!!!
return device;
}
It converts the input and converts the output. Here's a curl example of how it works:
curl -i -X PUT -H 'Content-Type: application/json' -d '{"port":3,"ip":"3"}' http://localhost:8888/restlet/
Result:
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Date: Sun, 13 Oct 2013 02:03:48 GMT
Accept-Ranges: bytes
Server: Development/1.0
Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept
Cache-Control: no-cache
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Content-Length: 19
{"ip":"3","port":3}