-
스프링의 새로운 HTTP 클라이언트 - RestClientSpring 2023. 8. 1. 22:37
스프링 프레임워크 6.1 M2 에서 RestClient 가 추가되었다.
RestClient 는 스프링의 새로운 동기식 HTTP 클라이언트로 WebClient API 와 비슷한 방식으로 사용되며 RestTemplate 의 인프라를 제공한다.
RestTemplate API 는 오래되기도 했고 사용법도 깔끔하지 않아서
새로 작성하는 코드에서는 WebClient 를 위주로 사용했는 데
사실 WebClient 하나를 사용하자고 spring-boot-starter-webflux 의존성을 넣는 것이 영 찝찝했다. (논블로킹으로 하는 것도 아니면서)
이런 와중 WebClient 의 세련된 API 와 비슷한 방식으로 사용할 수 있는 RestClient 가 나온 것은 매우 반가운 소식이 아닐 수 없다.
현재는 Spring Boot 3.2 M1 에서 포함되어 있는 데 빠르게 정식 버전으로 올라왔으면 하는 바람이다.
간단한 예시로 사용법을 비교해보았다.
Rest Template
val restTemplate = RestTemplate() val response = restTemplate.getForEntity("http://localhost:8080/echo?message=Hello", String::class.java) println(response.body)
Rest Client
val restClient = RestClient.create() val result = restClient.get() .uri("http://localhost:8080/echo?message=Hello") .retrieve() .body(String::class.java) println(result)
WebClient 를 사용해봤다면 사용하기 어렵지 않을 것 같다.
RestTemplate 로 RestClient 를 생성할 수 있다고 하니 전환할때도 편리할 것 같다.
자세한 내용은 https://spring.io/blog/2023/07/13/new-in-spring-6-1-restclient 에서 설명하고 있다.
반응형'Spring' 카테고리의 다른 글
Spring, Kotlin - Interceptor (0) 2021.11.25 spring - 문자 인증 시스템 만들기 (0) 2019.03.17 Spring - db(오라클) 설정 properties 파일 이용하기 (0) 2019.02.28 Spring - security 암호화 하기 (0) 2019.02.26 Spring - 트랜잭션 처리하기 (0) 2019.02.25