尝试以下方法而无需自定义处理程序。这个想法是从HttpStatusCodeException获取作为字符串的响应,然后可以将其转换为对象。对于转换,我使用了Jackson的ObjectMapper:
try { restTemplate.postForObject(url, pojoInstance, responseClass); } catch (HttpStatusCodeException e) { if (e.getStatusCode() == HttpStatus.UNAUTHORIZED) { String responseString = e.getResponseBodyAsString(); ObjectMapper mapper = new ObjectMapper(); CustomError result = mapper.readValue(responseString, CustomError.class); } }更新: 使用另一家工厂也可能会有所帮助,因为默认工厂中存在一个与您的问题相关的错误(请参见下面的评论):
RestTemplate template = new RestTemplate(new HttpComponentsClientHttpRequestFactory());



