Restlet clientResource post返回http错误415

时间:2015-08-10 11:41:05

标签: java json restlet

我正在尝试将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;
}

1 个答案:

答案 0 :(得分:1)

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

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

它现在有效!

相关问题