问题
How do I implement Restlet function which accepts JSON post? And how do I test this using curl?
Thanks
回答1:
With Restlet 2, you can either:
test the entity media-type compatibility in
@Post acceptRepresentation(Representation entity)
:@Post public Representation acceptRepresentation(Representation entity) throws ResourceException { if (entity.getMediaType().isCompatible(MediaType.APPLICATION_JSON)) { // ... } // ... }
or use
@Post
with one or two parameters:@Post("json") Representation acceptAndReturnJson(Representation entity) { // ... }
See these links:
- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2585586
- http://www.restlet.org/documentation/2.0/jse/api/org/restlet/resource/Post.html
(With Restlet 1, you would need to test the type of the entity.)
回答2:
As of writing this response (2 years after your question), Restlet 2.1 requires proper dependencies fulfilled to properly consume and respond with JSON. Point is, apart from "Unsupported media type
" response, there is not much clue about what is going on internally.
To activate JSON media type, you need to include dependency to org.restlet.ext.jackson
; if you need to support both XML and JSON, you need to include Jackson FIRST and then org.restlet.ext.xstream
, as XStream is also capable of JSON representations but the implementation is rather poor (as described in restlet docs, this is recommended order by restlet authors).
Then, you don't actually need to include media type in annotation and you just need to include proper Content-Type
header in your curl request, i.e.:
curl -X post -H "Content-Type: application/json" http://localhost:8080/login -d @login.json
- where
login.json
contains the actual JSON request. - login is
@Post
annotated method acceptingLoginRequest
and responding withLoginResponse
, both capable of XML and JSON media types
I hope, this answer will help someone sometime. :-)
回答3:
The example linked to by Daniel Vassallo shows data posted using a form. This is how to send JSON:
@Post
public void acceptJsonRepresentation(JsonRepresentation entity) {
JSONObject json = null;
try {
json = entity.getJsonObject();
// business logic and persistence
} catch (JSONException e) {
setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
return;
}
}
To test with curl:
curl -X POST <your url> -H "Content-Type: application/json" -d '{"key" : "value"}'
The single quotes ('') around the data in the curl command are important.
回答4:
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
回答5:
Here is a good and complete example of a Restlet that accepts JSON via POST:
- Creating a simple web service with Restlet
And a basic guide on how to test RESTful web services with cURL:
- REST-esting with cURL
回答6:
curl -u uid:4c521655 -X POST -H "Content-Type: application/json" -d "type=Big&data="{\"name\":\"test\"}"" --dump-header headers 'http://localhost:8190/appli/add'
来源:https://stackoverflow.com/questions/2033132/restlet-post-using-json