@PathVariable은 마지막 “~.com”이 짤린다?
인생 첫 이슈를 받아서(ㅋㅋ) 개발하는 과정에서
@PathVariable로 도메인 문자열을 받아오는데,
jsw.com
과 같이 받아오면 디버거에는 jsw
만 찍히는 현상이 발견됐다.
알아보니…
Spring은 마지막 점 뒤에 있는 모든 것을 파일 확장자로 간주한다고 한다.
(단, PathVariable이 가리키는 {무언가}
가 URI의 마지막에 있는 경우이다!!)
해결 방법
https://recordsoflife.tistory.com/488
위 포스팅에 따르면 해결 방법은 크게 세 가지가 있다.
1. {변수명:.+}
/ {변수명:.*}
@GetMapping("/init-configuration/{domain:.+}")
public boolean initConfiguration(@PathVariable String domain) {
return companyConfigurationService.initCompanyConfiguration(domain);
}
위와 같이 정규식 매핑을 추가하여 @PathVariable의 정의를 수정하는 방법
콜론: 뒤로 정규식을 적을 수 있다.
이 방법이 가장 좋아 보인다.
2. 마지막에 /
추가
@GetMapping("/init-configuration/{domain}/")
public boolean initConfiguration(@PathVariable String domain) {
return companyConfigurationService.initCompanyConfiguration(domain);
}
간단히 마지막에 /
를 추가해도 된다.
하지만 단 하나의 예외 상황을 위해 통일성을 깨는 것은 아닌가… 하는 생각이 든다.
3. 전역 Config
@Configuration
public class CustomWebMvcConfigurationSupport extends WebMvcConfigurationSupport {
@Override
protected PathMatchConfigurer getPathMatchConfigurer() {
PathMatchConfigurer pathMatchConfigurer = super.getPathMatchConfigurer();
pathMatchConfigurer.setUseSuffixPatternMatch(false);
return pathMatchConfigurer;
}
}
전역 수준으로 동작을 변경하는 방법도 있다.
요약
- URI 마지막이
.com
과 같이 끝나면 스프링은 확장자인 줄 알고 짤라버린다. - @PathVariable은
{무언가:.+}
와 같이 해결할 수 있다.
'코딩 > WEB 개발' 카테고리의 다른 글
Spring - WebClient 사용법, 주의사항 (0) | 2023.07.25 |
---|---|
Spring - RestTemplate vs. WebClient (0) | 2023.07.25 |
HTTP Method의 스펙 (0) | 2023.06.28 |
HTTP Method의 멱등성에 대해 (0) | 2023.06.28 |
HTTP Method, PUT과 PATCH의 차이점 (0) | 2023.06.28 |