Post
KO

spring 6 http interface

spring 6 에서 feign과 유사한 http interface가 추가된 것으로 확인되었습니다.

https://www.baeldung.com/spring-6-http-interface

최근, spring을 kotlin을 이용하여 reactive하게 작성하는 방법을 알게되서 coroutine을 적용해서 작성했는데, 다음과 같은 이슈가 발생하였습니다.

class org.springframework.http.ResponseEntity cannot be cast to class reactor.core.publisher.Mono (org.springframework.http.ResponseEntity and reactor.core.publisher.Mono are in unnamed module of loader 'app')
class httpClient { @GetExchange("path") suspend fun call() : ResponseEntity }

이런 형태로 작성했을 때 문제는 class casting exception이 발생된다.

class org.springframework.http.ResponseEntity cannot be cast to class reactor.core.publisher.Mono (org.springframework.http.ResponseEntity and reactor.core.publisher.Mono are in unnamed module of loader 'app')

그리고 suspend를 제거할 경우 아래와 같은 에러가 발생된다.

java.lang.IllegalStateException: block()/blockFirst()/blockLast() are blocking, which is not supported in thread reactor-http-nio-3 at reactor.core.publisher.BlockingSingleSubscriber.blockingGet(BlockingSingleSubscriber.java:117) ~[reactor-core-3.5.3.jar:3.5.3] Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: Error has been observed at the following site(s): ...

그래서 수정하는 방법은 다음과 같다. http interface는 coroutine을 지원하지 않기 때문에 suspend 선언을 제거하고, 호출하는 곳에서 awaitSingle을 이용하여 처리하는 것이다.

class httpClient { @GetExchange("path") fun call() : Mono> } @Service class Service(private val httpClient:HttpClient) { fun call() { httpClient.call().awaitSingle() // business logic.. } }

처음하는거라 제대로 알지 못했고, 지원하는지 여부 부터 조사하고 적용했었어야 했는데, 이렇게 또 배운다.

https://spring.io/blog/2019/04/12/going-reactive-with-spring-coroutines-and-kotlin-flow

Going Reactive with Spring, Coroutines and Kotlin Flow

Level up your Java code and explore what Spring can do for you.

// 24.02.21 추가

예제도 잘 작성되어 있는 Bealdung

https://www.baeldung.com/spring-6-http-interface

[**HTTP Interface in Spring 6 Baeldung**](https://www.baeldung.com/spring-6-http-interface)

Spring has a new web client builder that’s similar to OpenFeign, but is native to Spring. We look at how to build API clients with itm .

This article is licensed under CC BY 4.0 by the author.