[英]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.