Restlet clientResource post returns http错误415

Restlet clientResource post returns http error 415

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

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

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

现在可以使用了!