使用 JSON 的 Restlet POST

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2033132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:23:51  来源:igfitidea点击:

Restlet POST using JSON

jsonpostrestlet

提问by Don Ch

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

如何实现接受 JSON post 的 Restlet 功能?我如何使用 curl 测试这个?

Thanks

谢谢

回答by Bruno

With Restlet 2, you can either:

使用 Restlet 2,您可以:

  • 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 @Postwith one or two parameters:

    @Post("json") Representation acceptAndReturnJson(Representation entity) {
        // ...
    }
    
  • 测试实体媒体类型兼容性@Post acceptRepresentation(Representation entity)

    @Post
    public Representation acceptRepresentation(Representation entity)
            throws ResourceException {
        if (entity.getMediaType().isCompatible(MediaType.APPLICATION_JSON)) {
           // ...
        }
        // ...
    }
    
  • @Post与一两个参数一起使用:

    @Post("json") Representation acceptAndReturnJson(Representation entity) {
        // ...
    }
    

See these links:

请参阅这些链接:

(With Restlet 1, you would need to test the type of the entity.)

(使用 Restlet 1,您需要测试实体的类型。)

回答by Miro Hudak

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.

在撰写此回复时(在您提出问题 2 年后),Restlet 2.1 需要满足适当的依赖关系才能正确使用和响应 JSON。重点是,除了“ Unsupported media type”响应外,没有太多关于内部发生的事情的线索。

To activate JSON media type, you need to include dependency to org.restlet.ext.Hymanson; if you need to support both XML and JSON, you need to include Hymanson 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).

要激活 JSON 媒体类型,您需要包含对org.restlet.ext.Hymanson; 如果您需要同时支持 XML 和 JSON,则需要先包含 Hymanson ,然后包含org.restlet.ext.xstream,因为 XStream 也能够进行 JSON 表示,但实现相当差(如 restlet 文档中所述,这是 restlet 作者推荐的顺序)。

Then, you don't actually need to include media type in annotation and you just need to include proper Content-Typeheader in your curl request, i.e.:

然后,您实际上不需要在注释中包含媒体类型,您只需要Content-Type在 curl 请求中包含正确的标头,即:

curl -X post -H "Content-Type: application/json" http://localhost:8080/login -d @login.json
  • where login.jsoncontains the actual JSON request.
  • login is @Postannotated method accepting LoginRequestand responding with LoginResponse, both capable of XML and JSON media types
  • 其中login.json包含实际的 JSON 请求。
  • login 是@Post接受LoginRequest和响应的带注释的方法LoginResponse,都支持 XML 和 JSON 媒体类型

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

我希望,这个答案会在某个时候对某人有所帮助。:-)

回答by Pallavi Anderson

The example linked to by Daniel Vassallo shows data posted using a form. This is how to send JSON:

Daniel Vassallo 链接的示例显示了使用表单发布的数据。这是发送 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 进行测试:

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.

curl 命令中数据周围的单引号 ('') 很重要。

回答by Thierry Templier

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.

以下是有关这个老问题的一些更新。Restlet 支持包含 bean 的方法签名。在这种情况下,Restlet 将使用注册的转换器尝试将接收到的有效负载转换/填充到 bean 实例中。向客户端发送内容时也是如此。

Here is the sample of a method that handles a request POST:

以下是处理请求的方法示例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:

bean 可以简单地具有以下结构:

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 Hymanson (org.restlet.ext.Hymanson) within your classpath. The corresponding converter will be automatically registered under the hood.

要使这种机制起作用,您只需org.restlet.ext.Hymanson在类路径中添加扩展名 Hymanson ( )。相应的转换器将在引擎盖下自动注册。

The curl request is simple and the data to send has to be specified

curl 请求很简单,必须指定要发送的数据

curl -X POST http://... -H "Content-Type: application/json" -d '{"name" : "myname","description":"my description"}'

Hope it helps you, Thierry

希望对你有帮助,蒂埃里

回答by Daniel Vassallo

Here is a good and complete example of a Restlet that accepts JSON via POST:

这是通过 POST 接受 JSON 的 Restlet 的一个很好的完整示例:

And a basic guide on how to test RESTful web services with cURL:

以及有关如何使用 cURL 测试 RESTful Web 服务的基本指南:

回答by Aravind Cheekkallur

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'