Restlet POST using JSON

前端 未结 6 1470
独厮守ぢ
独厮守ぢ 2020-12-13 21:33

How do I implement Restlet function which accepts JSON post? And how do I test this using curl?

Thanks

相关标签:
6条回答
  • 2020-12-13 22:02

    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 accepting LoginRequest and responding with LoginResponse, both capable of XML and JSON media types

    I hope, this answer will help someone sometime. :-)

    0 讨论(0)
  • 2020-12-13 22:03

    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

    0 讨论(0)
  • 2020-12-13 22:07
    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'
    
    0 讨论(0)
  • 2020-12-13 22:09

    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.)

    0 讨论(0)
  • 2020-12-13 22:19

    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
    0 讨论(0)
  • 2020-12-13 22:24

    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.

    0 讨论(0)
提交回复
热议问题