我从第三方服务器的403 http代码响应中获取响应主体时遇到问题。在2019年4月5日之前,我能够正确地从第三方获取响应主体,但是现在我正在从getResponseBodyAsString检索可能未正确转换为字符串的字节。这是来自org.springframework.web.client包的问题吗?
@Autowired
RestClient restClient;
try{
//Request via restTemplate
ResponseEntity<String> responseEntity = restClient.makeClientRequestJson(batch_type.getEndpoint(), HttpMethod.POST, httpHeaders, batch_payload.getRequest(), ProcessorConstants.moduleId);
} catch (HttpClientErrorException | HttpServerErrorException ex) {
loggingService.writeLogs("Error From Third Party Request, Message: " + ex.getMessage() + "| Response Body: " + ex.getResponseBodyAsString(), this.getClass(), LoggingEnum.ERROR, BatchConstants.moduleId);
...
这是makeClientRequestJson的实现:
public ResponseEntity<String> makeClientRequestJson(String url, HttpMethod httpMethod, HttpHeaders headers, String jsonBody , String moduleId){
RestTemplate restTemplate = new RestTemplate();
HttpEntity<String> entity = new HttpEntity<>(jsonBody, headers);
loggingService.writeLogs("Third Party Request: " + entity.getBody() + "| url: " + url + " | method: " + httpMethod.toString(), RestClientImpl.class, LoggingEnum.INFO, moduleId);
ResponseEntity<String> response = restTemplate.exchange(url, httpMethod, entity, String.class);
loggingService.writeLogs("Third Party Response: " + response.getBody() + "| url: " + url + " | method: " + httpMethod.toString(), RestClientImpl.class, LoggingEnum.INFO, moduleId);
return response;
}
问题是当获取ex.getResponseBodyAsString()返回字节数组时,我直接从服务器获取json响应,如:
{
"code": "403020",
"transactionID": "1555495478-1409322457-83999786",
"message": "Account is Level 1"
}