TIL - (스프링+리액트) 특정 주소만 작동하지 않는 상황

2023.11.22 (WED)


'/', '/test'는 정상작동 하지만, '/test/test'는 정상작동하지 않는 오류가 생겼다.
  • 기존 controller
    @Controller
    public class WebController implements ErrorController {
    
      @RequestMapping({"/{path:[^\\.]*}"})
      public String redirect() {
          return "forward:/";
      }
    }
    

리다이렉트의 추가 수정이 필요하다.

@Controller
public class WebController implements ErrorController {

    @RequestMapping({"/{path:[^\\.]*}"})
    public String redirect() {
        return "forward:/";
    }

    @RequestMapping({"/**/{path:^(?!.*\\.).*}"})
    public String deepRedirect() {
        return "forward:/";
    }
}

근데 이후, 아래와 같은 오류가 발생하였다.

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2023-11-22 09:16:38.850 ERROR 30399 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   :

APPLICATION FAILED TO START

Description:

Invalid mapping pattern detected: /**/{path:^(?!..).}
^
No more pattern data allowed after {*...} or ** pattern element

Action:

Fix this pattern in your application or switch to the legacy parser implementation with 'spring.mvc.pathmatch.matching-strategy=ant_path_matcher'.

해당 오류는 스프링부트에서 제공하는 기본 경로 패턴 매칭 알고리즘은 복잡한 정규 표현식을 지원하지 않기 때문에 발생하는 오류이다.
이를 해결하기 위해서는 스프링 부트의 설정을 변경하여, 이전 버전의 경로 매칭 알고리즘(AntPathMatcher)을 사용하면 된다.

spring.mvc.pathmatch.matching-strategy=ant_path_matcher