목차

Spring RestTemplate

HttpEntity

POST에서 파라미터 보내기

Exception 처리

public class MyErrorHandler implements ResponseErrorHandler {
  @Override
  public void handleError(ClientHttpResponse response) throws IOException {
    // your error handling here
  }
 
  @Override
  public boolean hasError(ClientHttpResponse response) throws IOException {
    // ...
  }
}
 
// [...]
 
public static void main(String args[]) {
  RestTemplate restTemplate = new RestTemplate();
  restTemplate.setErrorHandler(new MyErrorHandler());
}

Interceptor를 통한 로깅

SpringBoot

@Bean
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {
    return restTemplateBuilder
            .requestFactory(() -> new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()))
            .setConnectTimeout(Duration.ofMillis(5000)) // connection-timeout
            .setReadTimeout(Duration.ofMillis(5000)) // read-timeout
            .additionalMessageConverters(new StringHttpMessageConverter(Charset.forName("UTF-8")))
            .build();
}

참조