카테고리 없음

내일배움캠프 31일차 - RestTemplate이란 무엇일까?

본성 2024. 8. 28. 21:00
반응형

Spring프레임워크 익숙해질 때까지

 

gogo 

 

scv처럼 무지성으로 해보자!!!


 

RestTemplate이란 무엇일까?

 

RestTemplate

 

1. Spring RestTemplate는 어떤 것을 유용하게 사용하려고 우리는 사용할까?

 

2. RestTemplate에 알아보자!

 

3. RestTemplat은 HTTP 요청을 수행하는 동기 클라이언트로, JDK, Apache HttpComponents 등과 같은 기본 HTTP 클라이언트 라이브러리를 통해 간단한 템플릿 메서드 API를 노출합니다.

 

4. RestTemplate은 덜 빈번한 경우를 지원하는 일반화된 메서드 HttpURLConnection외에도 HTTP 메서드별 (ex. GET, POST, DELETE, PUT) 일반적인 시나리오에 대한 템플릿을 제공합니다.

 

5. RestTemplate은 일반적으로 공유 구성 요소로 사용됩니다. 그러나 구성은 동시 수정을 지원하지 않으므로 일반적으로 시작 시 구성이 준비됩니다.

 

6. 필요한 경우 시작 시 다르게 구성된 여러 RestTemplate 인스턴스를 만들 수 있습니다. 이러한 인스턴스는 ClientHttpRequestFactory HTTP 클라이언트 리소스를 공유해야 하는 경우 동일한 기본을 사용할 수 있습니다.

7. RestTemplate의 특징으로 

 · Spring 3.0 부터 지원하는 Spring의 HTTP 통신 템플릿

 · HTTP 요청 후 JSON, XML, String 과 같은 응답을 받을 수 있는 템플릿

 · Blocking I/O 기반의 동기방식을 사용하는 템플릿

 · RESTful 형식에 맞추어진 템플릿

 · Header, Content-Tpye등을 설정하여 외부 API 호출

 · Server to Server 통신에 사용

 

이렇게 특징을 가지고 있다.

 

8. RestTemplate 동작원리를 알아보자!

 

 

1.) 애플리케이션 내부에서 REST API에 요청하기 위해 RestTemplate의 메서드를 호출한다.

 

2.) RestTemplate은 MessageConverter를 이용해 java object를 request body에 담을 message(JSON etc.)로 변환한다. 메시지 형태는 상황에 따라 다름
 
3.) ClientHttpRequestFactory에서 ClientHttpRequest을 받아와 요청을 전달한다.
 
4.) 실질적으로 ClientHttpRequest가 HTTP 통신으로 요청을 수행한다.
 
5.) RestTemplate이 에러핸들링을 한다.
 
6.) ClientHttpResponse에서 응답 데이터를 가져와 오류가 있으면 처리한다.
 
7.) MessageConverter를 이용해 response body의 message를 java object로 변환한다.
 
8.) 결과를 애플리케이션에 돌려준다.
※ RestTemplate은 통신 과정을 ClientHttpRequestFactory(ClientHttpRequest, ClientHttpResponse)에 위임합니다. ClientHttpRequestFactory의 실체는 HttpURLConnection, Apache HttpComponents, HttpClient와 같은 HTTP Client
 
 

 

 
 

9.  이렇게 Spring RestTemplate을 통해 더욱 다양하고 효율적이게 코딩을 해보자!


https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html

 

RestTemplate (Spring Framework 6.1.12 API)

postForLocation Create a new resource by POSTing the given object to the URI template, and return the value of the Location header. This header typically indicates where the new resource is stored. URI Template variables are expanded using the given URI va

docs.spring.io

https://blog.naver.com/hj_kim97/222295259904

 

[Spring]스프링 RestTemplate

스프링 RestTemplate - RestTemplate란?  - RestTemplate의 특징 - RestTemplate 동작 원리 -...

blog.naver.com

<출처>


오늘의 느낀 점

 

1. 항상 크게 생각하자

 

2. 그래도 세상은 돌아간다.

 

3. '운'은 멀까?

 

just do it

반응형