【问题标题】:Restlet clientResource post returns http error 415Restlet clientResource 帖子返回 http 错误 415
【发布时间】:2015-08-10 11:41:04
【问题描述】:

我正在尝试将JSON 发布到一些REST 服务,但我总是以HTTP Error 415: Unsupported Media Type 结束。

REST 文档 明确指出我应该使用application/json,我这样做了。我肯定忽略了一些东西。

public JSONObject fetchResponse() throws ResourceException, JSONException, IOException {
    JRRequest jr = new JRRequest();

    jr.setJql(jql);
    jr.setMaxResults(Integer.parseInt(maxresults));
    jr.setFields(fields);

    Gson json = new Gson();
    String payload = json.toJson(jr);


    JSONObject jsObj = new JSONObject(getClientResource(restUri).post(payload,MediaType.APPLICATION_JSON).getText());

    return jsObj;
}


private ClientResource getClientResource(String uri) {
    ClientResource clientResource = new ClientResource(uri);
    Application app = new Application();
    clientResource.setChallengeResponse(ChallengeScheme.HTTP_BASIC,username, password);
    return clientResource;
}

【问题讨论】:

  • 您有Restlet 服务器吗?还是您在 NodePHP 等其他服务器上发帖?
  • 在不同的服务器上发布,实际上是 JIRA (Tomcat)。
  • 我已经在 Chrome 中尝试了高级 REST 客户端,使用 application/json 发布了一个 json 有效负载,它工作正常。所以服务器肯定工作正常。
  • 您是否检查过JSON 是否格式错误或是否在请求标头上记录了一些日志?打印标题显示在此链接中stackoverflow.com/questions/17743785/…

标签: java json restlet


【解决方案1】:

好的,我找到了解决方案。我没有在一行中完成所有操作,而是尝试了以下操作:

    Representation rep = new StringRepresentation(payload, MediaType.APPLICATION_JSON);
    JSONObject jsObj = new JSONObject(getClientResource(restUri).post(rep).getText());

现在可以使用了!

【讨论】:

  • 如果这解决了你的问题,我认为你应该接受这个作为答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-26
  • 1970-01-01
  • 1970-01-01
  • 2011-05-17
  • 1970-01-01
  • 2015-02-28
相关资源
最近更新 更多