본문 바로가기

Various Error

[Spring Cloud/API Gateway] Spring Reactive Web

Spring Cloud의 API Gateway의 의존성을 추가하면 자동으로 Spring Reactive Web이 추가가 되는데 여기서 Spring Web을 추가하면 충돌이 발생

 

Spring Web서블릿 기반의 블로킹 I/O를 사용

Spring Reactive Web(예: spring-cloud-starter-gateway)은 넌블로킹 I/O를 사용

*************************** APPLICATION FAILED TO START ***************************

Description:

Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway.

Action:

Please set spring.main.web-application-type=reactive or remove spring-boot-starter-web dependency.

 

따라서, Spring Web 의존성이 없으면 HttpServletRequest, HttpServletResponse, Cookie 등을 사용할 수 없게 되는데

HttpServletRequest, HttpServletResponse는 ServerWebExchange으로 대체하고

Cookie는 RequestCookie, ResponseCookie 대체

public void addJwtToCookie(String token, ServerWebExchange exchange) {
    ResponseCookie cookie = ResponseCookie.from(AUTHORIZATION_HEADER, token)
            .path("/")
            .httpOnly(true)
            .build();
    exchange.getResponse().addCookie(cookie);
}