사용자 도구

사이트 도구


springframework:resttemplate

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
springframework:resttemplate [2015/01/26 15:12]
kwon37xi [POST에서 파라미터 보내기]
springframework:resttemplate [2022/01/14 15:43] (현재)
kwon37xi
줄 1: 줄 1:
 ====== Spring RestTemplate ====== ====== Spring RestTemplate ======
   * [[https://spring.io/blog/2009/03/27/rest-in-spring-3-resttemplate|Rest in Spring 3 RestTemplate]]   * [[https://spring.io/blog/2009/03/27/rest-in-spring-3-resttemplate|Rest in Spring 3 RestTemplate]]
 +  * [[http://www.baeldung.com/rest-template|Spring RestTemplate Tutorial]]
 +  * [[http://www.baeldung.com/spring-rest-template-list|Get and Post Lists of Objects with RestTemplate | Baeldung]]
 +  * [[https://www.baeldung.com/spring-rest-template-builder|Configure a RestTemplate with RestTemplateBuilder | Baeldung]]
 +  * [[https://howtodoinjava.com/spring-boot2/resttemplate/|Spring Boot RestTemplate Tutorials - HowToDoInJava]]
 +  * see [[springframework:feign|Fegin]]
  
 ===== HttpEntity ===== ===== HttpEntity =====
줄 31: 줄 36:
 </code> </code>
  
 +===== Exception 처리 =====
 +  * [[https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/client/ResponseErrorHandler.html|ResponseErrorHandler]]를 사용하여 예외를 일관성 있게 처리할 수 있다. [[https://stackoverflow.com/questions/38093388/spring-resttemplate-exception-handling|rest - Spring Resttemplate exception handling]]
  
 +<code java>
 +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());
 +}
 +</code>
 +  * 기본 구현체인 [[https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/client/DefaultResponseErrorHandler.html|DefaultResponseErrorHandler]]를 상속하여 필요한 메소드만 override 해도 된다.
 +
 +===== Interceptor를 통한 로깅 =====
 +  * [[http://eclipse4j.tistory.com/282|Spring RestTemplate 에서 로그 처리]]<code java>
 +// ClientHttpRequestInterceptor 인터페이스를 로깅을 남기게 구현해서 넣어주면 된다.
 +
 +RestTemplate restTemplate = new RestTemplate();
 +restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory())); // body 스트림을 소모해버리지 않게 복제
 +restTemplate.setInterceptors(Lists.newArrayList(new RestTemplateLoggingInterceptor())); // RestTemplateLoggingInterceptor는 로그를 남기는 구현. body를 읽어버림.
 +</code>
 +
 +===== SpringBoot =====
 +  * with [[springframework:springboot|SpringBoot]]
 +  * ''org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration''
 +  * ''RestTemplateBuilder'' 가 존재하지 않을 경우 생성해준다.
 +  * ''RestTemplate'' 자체는 생성해주지 않고 Builder 만 만들어준다.
 +  * ''RestTemplateCustomizer'', ''RestTemplateRequestCustomizer'' Bean 들을 통해 조정해준다.
 +
 +<code java>
 +@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();
 +}
 +</code>
 +
 +===== 참조 =====
 +  * [[https://www.baeldung.com/spring-resttemplate-logging|Spring RestTemplate Request/Response Logging | Baeldung]]
 +  * [[https://enterkey.tistory.com/275|Spring에서 RestTemplate을 사용하여 REST 기반 서비스 요청과 테스트하기 - Hello World]]
 +  * [[https://luvstudy.tistory.com/78|RestTemplate 응답 log 확인하기]]
 +  * [[https://reflectoring.io/spring-resttemplate/|Complete Guide to Spring RestTemplate]]
springframework/resttemplate.1422252765.txt.gz · 마지막으로 수정됨: 2015/01/26 15:12 저자 kwon37xi