How do I implement Restlet function which accepts JSON post? And how do I test this using curl?
Thanks
Here are some updates regarding this old question. Restlet supports method signatures that contains beans. In such cases, Restlet will use a registered converter to try to convert / fill the received payload into a bean instance. This is also true when sending content to the client.
Here is the sample of a method that handles a request POST:
public class TestServerResource extends ServerResource {
@Post
public void test(TestBean bean) {
System.out.println(">> bean = " + bean.getMessage());
}
}
The bean can simply have the following structure:
public class TestBean {
private String name;
private String message;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
To make work such mechanism, you can simply add the extension Jackson (org.restlet.ext.jackson) within your classpath. The corresponding converter will be automatically registered under the hood.
The curl request is simple and the data to send has to be specified
curl -X POST http://... -H "Content-Type: application/json" -d '{"name" : "myname","description":"my description"}'
Hope it helps you, Thierry