【问题标题】:Querying a JSON REST Service with POSTMAN/Rest Client使用 POSTMAN/Rest Client 查询 JSON REST 服务
【发布时间】:2015-04-13 01:03:38
【问题描述】:

我有一个简单的 RESTLET 服务,它返回一个 JSON 表示,如下所示:

@Override
    @Post("json")
public Representation helloWorld() {
    Representation representation = new JacksonRepresentation<Hello>(new Hello("Hello World"));
    representation.setMediaType(MediaType.APPLICATION_JSON);
    representation.
    return representation;
}

当我使用 cURL 查询时,我得到了预期的响应:

{"greeting":"Hello World"}

但是,当我使用浏览器或 POSTMAN 或任何其他 Web REST 客户端时,我没有得到响应。我收到来自 POSTMAN 的响应“无法得到任何响应”。

POSTMAN 请求的预览是:

POST /testingrestlet/hello HTTP/1.1
Host: 127.0.0.1:6000
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: 5141cd87-505c-e813-0045-3b7f4aa54144

我至少希望 POSTMAN 等 REST 客户端能够工作,还是我遗漏了什么?

任何帮助表示赞赏。

问候

【问题讨论】:

    标签: json rest restlet postman


    【解决方案1】:

    我认为您应该使用标题Accept 进行内容协商(conneg)。您的要求应该是:

    POST /testingrestlet/hello HTTP/1.1
    Host: 127.0.0.1:6000
    Content-Type: application/json
    Accept: application/json
    Cache-Control: no-cache
    Postman-Token: 5141cd87-505c-e813-0045-3b7f4aa54144
    

    事实上,由于您在注释中指定了扩展名“json”,Restlet 预计此标头与 JSON 内容的一种可能的媒体类型一起发送。此提示用于选择处理方法...如果您删除扩展名,则应该不会出现此问题,如下所述:

    @Post
    public Representation helloWorld() {
        Representation representation
            = new JacksonRepresentation<Hello>(new Hello("Hello World"));
        representation.setMediaType(MediaType.APPLICATION_JSON);
        (...)
        return representation;
    }
    

    希望对你有帮助 蒂埃里

    【讨论】:

    • 您好 Thierry,尝试了附加的 Accept 标头,还删除了 json 提示。仍然没有运气:(。有趣的是 POSTMAN 提供了一个 CURL 版本的查询,可以从命令行运行。(curl -X POST -H "Content-Type: application/json" -H "Accept: application/json" -H " Cache-Control: no-cache" -H "Postman-Token: 58bb4b2e-fa2d-e204-7a8b-e7e83766f1a1" -d '' 127.0.0.1:6000/testingrestlet/hello)
    • 您好,返回的payload有错误或者您可以给我发消息吗?谢谢!
    • 我刚刚使用您提供的代码进行了真正的测试,并且没有添加特定的标头,它对我有用。我使用 chrome 41.0.2272.118 (linux) 和 postman 0.8.4.14。
    • POSTMAN 返回:'无法得到任何响应。这似乎是连接到 http://....' 的错误。响应状态为 0。REST 控制台返回:“连接失败!”高级 Rest 客户端返回:'0 无响应'
    • 另一个可能有用的细节是我在 Android 上执行此操作。 Restlet 服务器在 Android 上运行。 (我已经完成了 curl 演示的端口转发等)
    【解决方案2】:

    在 Thierry 的 cmets 的帮助下,该问题被追溯到 Google Chrome 阻止对端口 6000 的请求,因为它认为这些请求不安全。该错误仅在报告 ERR_UNSAFE_PORT 错误的 Chrome 调试模式下可见。更改为更高的端口解决了这个问题!此错误的详细信息也可以在这里找到:https://superuser.com/questions/188006/how-to-fix-err-unsafe-port-error-on-chrome-when-browsing-to-unsafe-ports

    【讨论】:

      猜你喜欢
      • 2015-06-05
      • 1970-01-01
      • 2012-07-26
      • 2019-04-07
      • 2016-10-15
      • 2016-01-30
      • 2015-11-02
      • 2019-05-15
      • 2013-03-09
      相关资源
      最近更新 更多