Spring WebFlux 자동설정에서는 DomainClassConverter를 등록하지 않는다

강동훈·2022년 7월 2일
0

결론

Spring WebFlux를 사용할 경우 DomainClassConverter가 빈으로 등록되지 않는다.

위의 한 줄이 이 글의 주제이므로 DomainClassConverter와 Spring WebFlux가 무엇인지는 레퍼런스의 원문을 인용해서 간단히 소개만하고, 원인이 무엇인지에 대해서도 간단히 살펴본다.

DomainClassConverter

Converter to convert arbitrary input into domain classes managed by Spring Data CrudRepositorys. The implementation uses a ConversionService in turn to convert the source type into the domain class' id type which is then converted into a domain class object by using a CrudRepository.

예시를 살펴보자.

@GetMapping("/memos/{id}") // 예시 /memos/1
fun getMemo(@PathVariable("id") Memo memo) { // id 값을 Memo 객체로 받기
	return memo;
}

부트 프로젝트의 경우 Spring WebMvc, Spring Data Jpa를 사용하면 자동설정에 의해 DomainClassConverter가 빈으로 등록이 되고, 별도의 설정없이 위와 같은 코드를 사용할 수 있다.

Spring WebFlux

The original web framework included in the Spring Framework, Spring Web MVC, was purpose-built for the Servlet API and Servlet containers. The reactive-stack web framework, Spring WebFlux, was added later in version 5.0. It is fully non-blocking, supports Reactive Streams back pressure, and runs on such servers as Netty, Undertow, and Servlet 3.1+ containers.

DomainClassConverter는 누가 등록할까

DomainClassConverterSpringDataWebConfiguration에서 등록한다. 이 설정은 @EnableSpringDataWebSupport 어노테이션을 붙이면 적용될 수 있다. 다음은 이 어노테이션에 대한 설명이다.

Annotation to automatically register the following beans for usage with Spring MVC.

 * DomainClassConverter - to allow usage of domain types managed by Spring Data repositories as controller method arguments bound with PathVariable or RequestParam.
 * ...

이 어노테이션이 하는 일은 SpringDataWebAutoConfiguration 자동설정을 통해서도 할 수 있다. 다시 말해, 부트 프로젝트의 경우 SpringDataWebAutoConfiguration이 적용되면 DomainClassConverter가 등록이 되는데, 이 자동설정은 웹 애플리케이션의 타입이 서블릿이고, WebMvcConfigurer가 클래스패스에 존재해야 적용된다. 하지만 WebFlux를 사용하는 프로젝트의 경우 웹 애플리케이션의 타입이 리액티브이며, WebMvcConfigurer가 클래스패스에 존재하지 않는다. (WebFluxConfiguer가 클래스패스에 존재한다.)

왜 WebFlux에서 빠졌을까?

어떤 이유가 있을 것이다. 그러나 아직 왜 WebFlux에서는 빠졌는지는 모르겠다.

profile
안녕하세요, 강동훈입니다.

0개의 댓글