方法
我们在使用restTemplate的时候如果catch Exception。其实我们拿不到具体的错误信息,我们需要catch HttpStatusCodeException。
具体使用
try{
responseEntity = restTemplate.exchange(apiUrl , HttpMethod.POST, entity, String.class);
if(HttpStatus.OK.equals(responseEntity.getStatusCode())) {
//do something
}
} catch (HttpStatusCodeException e){
//for 4XX,5XX from BRE,this HttpStatusCodeException can obtain error message rather than Exception
throw new BusinessException("error message [{}]," + e.getResponseBodyAsString());
} catch (Exception e){
throw new BusinessException(“error message [{}]," + e.getMessage());
}
这样就可以打印出具体错误信息了。