
Spring Boot の RestTemplate で 500 系エラーが発生した場合でも reponse header / body の中身を取得する
·1 min read
困ったこと
RestTemplate で http request を送って 500 系のエラーが帰ってきた場合、RestClientExceptionが送出され、戻り値としては http response が取得できない。
解決方法
RestClientExceptionのサブクラスであるRestClientResponseExceptionを catch すること。
} catch(RestClientResponseException e) {
e.getRawStatusCode();
e.getResponseHeaders();
e.getResponseBodyAsString();
}なぜ?
Common base class for exceptions that contain actual HTTP response data.
とのことなので。