简体   繁体   English

Java RestTemplate挂在204状态码响应上

[英]Java RestTemplate hangs on 204 status code response

I'm creating test framework and using RestTemplate class for HTTP request creation. 我正在创建测试框架,并将RestTemplate类用于HTTP请求创建。

In general cases I use next code: 在一般情况下,我使用下一个代码:

Response response = null;
    ResponseEntity<String> responseEntity = null;

    try{
        responseEntity = getRest().exchange(url, httpMethod, httpEntity, String.class);
        response = new Response(
                responseEntity.getStatusCodeValue(),
                responseEntity.getStatusCode().getReasonPhrase(),
                responseEntity.getBody(),
                responseEntity.getHeaders()
        );
    } catch (HttpStatusCodeException e){
        response = new Response(
                e.getRawStatusCode(),
                e.getStatusText(),
                e.getResponseBodyAsString(),
                e.getResponseHeaders()
        );
    }

It works perfectly for all cases except DELETE HTTP method which receives 204 status code and empty body as a response. 它适用于所有情况,但DELETE HTTP方法除外,该方法接收204状态代码和空主体作为响应。

Now, I have to reinitialize RestTemplate to fix it. 现在,我必须重新初始化RestTemplate进行修复。 But I hope that another way should exist. 但是我希望应该有另一种方式。

Could you help me with this? 你能帮我吗?

Please check if you use HttpMethod.Delete for httpMethod and also try when you use delete to put as last parameter of exchange not String.class but Void.class. 请检查是否对HttpMethod使用HttpMethod.Delete,并在使用delete代替Exchange的最后一个参数(不是String.class而是Void.class)时尝试。

I hope it help you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用204输入响应代码 - PUT response code with 204 泽西客户响应状态204 - Jersey Client response status 204 如何设置 RestTemplate 以重试某些响应状态代码的调用? - How to set up RestTemplate to retry calls on certain response status code? Resttemplate如何在收到响应之前确定响应的状态码? - How does Resttemplate determine the status code of response before receiving it? Java - 如何测试 RestTemplate POST 方法成功状态代码? - Java - How to test RestTemplate POST method success status code? 返回200而不是204的响应代码 - Returning 200 response code instead of 204 是否可以通过java.net访问带有204响应代码的站点的html? - Is it possible to access the html of a site with a 204 response code via java.net? OkHttp“java.net.ProtocolException:意外状态行:nullHTTP / 1.1 200 OK”在使用相同连接的“204 NO-CONTENT”响应之后 - OkHttp “java.net.ProtocolException: Unexpected status line: nullHTTP/1.1 200 OK” after a “204 NO-CONTENT” response using same connection 使RestTemplate流程响应主体不受状态影响 - Make RestTemplate process response body regardless of status 尽管检查了状态代码,但jsoup仍抛出204状态 - jsoup throws 204 status despite a status code check
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM