URL 맨 뒤 슬래시 (trailing slash)
@GetMapping("/user") 라는 URL을 하나 만들었을 경우
http://test.com/user/ 로 요청 하면 페이지를 찾을 수 없는 오류가 발생한다.
spring boot 기준 3 이전까지는 기본적으로 매칭해주었으나, 3 부터는 매칭되지 않는 설정이 기본이다.
해결방법
trailing slash path까지 작성
@GetMapping(path={"/user", "/user/"})
누락되면 매칭 오류가 발생할 수 있음WebMvcConfigurer 설정
setUseTrailingSlashMatch을 true로 주어 매칭되도록 설정
1
2
3
4
5
6
7
8
9
10
11
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
configurer.setUseTrailingSlashMatch(true);
}
}
다만 위 옵션은 deprecated 되어 있으므로 추후 사라질수 있음
- Filter 이용
필터를 등록하여 요청 url 맨 뒤에/가 존재하는 경우/를 지워주는 필터 등록
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Component
public class ServletRequestWrapperFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequestWrapper requestWrapper = new HttpServletRequestWrapper((HttpServletRequest) request) {
@Override
public String getRequestURI() {
String requestURI = super.getRequestURI();
if (StringUtils.endsWith(requestURI, "/")) {
return StringUtils.removeEnd(requestURI, "/");
}
return requestURI;
}
};
chain.doFilter(requestWrapper, response);
}
}
현재 가장 적합한 방법은 Filter를 등록하는 방법 같음
This post is licensed under CC BY 4.0 by the author.