이전 포스팅에서 타 서비스를 호출 하기 위한 몇가지 방법에 대해서 다룬 포스팅이 있다.
자세한 내용은 아래 참고 포스팅 참고 바란다.
하지만 추가로 새로운 방법이 있어서 소개하는 포스팅을 작성한다.
스프링 3.2.2 버전에서 새롭게 추가된 기능인 RestClient 이다.
공식 홈페이지는 아래와 같다.
https://spring.io/projects/spring-boot#overview
Spring Boot
spring.io
간단한 사용법은 아래와 같다.
(post 기준)
..이상 생략 import org.springframework.web.client.RestClient; ..이상 생략 TestBuilderInDto testBuilderInDto = new TestBuilderInDto(); RestClient restClient = RestClient.create(); ResponseEntity<Void> dto = restClient.post() .uri("http://127.0.0.1:8080/test/target") .contentType(MediaType.APPLICATION_JSON) .body(testBuilderInDto) .retrieve() .toBodilessEntity(); ..이하 생략 |
위와 같은 코드로 실행이 되면
@PostMapping("/test/target") public TestBuilderInDto restClientTestTarget(@RequestBody TestBuilderInDto testBuilderInDto) { System.out.println(testBuilderInDto); return null; } |
아래 controller 에서 받아질수가 있다.
기타 사용 예제는 공식 홈페이지에서 더 찾아볼수 있다.
https://docs.spring.io/spring-framework/reference/integration/rest-clients.html
REST Clients :: Spring Framework
WebClient is a non-blocking, reactive client to perform HTTP requests. It was introduced in 5.0 and offers an alternative to the RestTemplate, with support for synchronous, asynchronous, and streaming scenarios. WebClient supports the following: Non-blocki
docs.spring.io
참고 포스팅
https://thenicesj.tistory.com/817
Java 에서 Http 통신 방식 3가지(RestTemplate, WebClient, OpenFeign)
Spring Framework 는 다양하게 Http 요청 방식을 지원하고 통신을 제공한다. 3가지 방식에 대해서 간단히 작성해볼것이다. Http 통신 에 대한 내용은 아래 참고 포스팅을 참고 바란다. 1. RestTemplate RestTemp
thenicesj.tistory.com
https://thenicesj.tistory.com/469
openfeign 이란?
간단하게 소개를 하면 다음과 같다. REST Call을 위해 호출하는 클라이언트를 보다 쉽게 작성할 수 있도록 도와주는 라이브러리 OpenFeign은 동일한 기능을 하는 RestTemplate 대비 interface를 작성하고 an
thenicesj.tistory.com
'IT > Java' 카테고리의 다른 글
[Error] Invalid value type for attribute 'factoryBeanObjectType': java.lang.String (22) | 2024.03.27 |
---|---|
[Error] java.lang.IllegalArgumentException: Illegal character in scheme at index 0: localhost (15) | 2024.03.26 |
Int 형 나누기 결과 소수점 n 자리까지 얻어오는법 (23) | 2024.02.22 |
[JPA] 일부 컬럼만 가져오기 (18) | 2024.02.14 |
[JPA] Could not set field value [POST_INSERT_INDICATOR] value by reflection 에러 (25) | 2024.02.13 |
댓글