org.restlet PUT方法失败

时间:2018-07-17 16:43:38

标签: java rest http restlet

我的代码如下:

try {
            ClientResource cr = new ClientResource(fullUrl);

            if(authentication != null) cr.setChallengeResponse(authentication);

            cr.accept(MediaType.APPLICATION_JSON);

            cr.getRequestAttributes().put("Content-Type", "application/json; charset=UTF-8");
            cr.getRequestAttributes().put("Accept", "application/json");

            cr.getResponseAttributes().put("Content-Type", "application/json; charset=UTF-8");
            cr.getResponseAttributes().put("Accept", "application/json");
            cr.getResponseAttributes().put(HeaderConstants.HEADER_ACCEPT_CHARSET, "UTF-8");
            cr.getResponseAttributes().put(HeaderConstants.HEADER_ACCEPT_ENCODING, "gzip");

//            cr.head(MediaType.APPLICATION_JSON);
            logger.debug("Client Request = " + cr.getRequest());
            logger.debug("Client Resource Headers = " + cr.getRequestAttributes());
            logger.debug("Request Headers = " + cr.getRequestAttributes()
                .get(HeaderConstants.HEADER_ACCEPT_CHARSET));
            logger.debug("Response Headers = " + cr.getResponseAttributes()
                .get(HeaderConstants.HEADER_ACCEPT));
//            logger.debug("Head = " + cr.head());
            cr.put(body);
            if(!cr.getStatus().isSuccess()){
                logger.error(cr.getStatus().getDescription());
                return false;
            } else {
                return true;
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
            logger.debug(ExceptionUtils.getStackTrace(e));
        }

运行此命令时,出现以下错误:

18/07/17 12:28:08 DEBUG HttpClient:143: org.restlet.resource.ResourceException: Not Acceptable (406) - The resource identified by the request is only capable of generating response entities which have content characteristics not acceptable according to the accept headers sent in the request
        at org.restlet.resource.ClientResource.doError(ClientResource.java:592)
        at org.restlet.resource.ClientResource.handleInbound(ClientResource.java:1156)
        at org.restlet.resource.ClientResource.handle(ClientResource.java:1049)
        at org.restlet.resource.ClientResource.handle(ClientResource.java:1024)
        at org.restlet.resource.ClientResource.put(ClientResource.java:1567)
        at org.restlet.resource.ClientResource.put(ClientResource.java:1505)

restlet依赖性如下所示:

<dependency>
  <groupId>org.restlet.jse</groupId>
  <artifactId>org.restlet</artifactId>
  <version>2.3.9</version>
</dependency>

我尝试了不同的方法来设置所需的标头,如上面的代码所示。但是似乎没有任何效果。

要添加到上面,如果我尝试用邮递员访问URL,我将得到有效的答复。

PUT /message_daily_aggr_idx_2018jul171129 HTTP/1.1
Host: localhost:8080
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: a0f2bbad-5bac-5e49-dead-a6c193b7f113

{
    "hello": "there"
}

不知道我在做什么错了。

有人可以帮我解决这个问题吗?

0 个答案:

没有答案