반응형
스프링5에 존재하는 @PathVariable은
@GetMapping과 @PostMapping을 통해 URL맵핑을 이용할때 사용되는 어노테이션이다.
예를 들면 아래와 같은 코드가 있다고 하자.
@RestController
public class CustomController {
@GetMapping("/example/{firstValue}/{secondValue}")
public void example(@PathVariable("firstValue") String firstValue,
@PathVariable("secondValue") String secondValue) {
// ...
}
}
예를들어 접근 URL이 /example/hi.hi2/choi.123 이라고 치면
@PathVariable("firstValue") 를 사용하여 본래 firstValue가 있던 자리에 존재하는 hi.hi2를 String firstValue변수에
저장하게된다.
간단 설명은 여기서 끝, 오늘 알려 드리고자 하는것은
위처럼 파라매터들을 가져 온다면 가장 끝 파라매터 즉, 점이 포함 된 요청 URI의 끝을 매핑 할 때 마지막 점이 잘린다.
Ex) /example/hi.hi2/choi.123 로 접근한다면
@PathVariable("secondValue") String sec의 결과는
sec의 내용은 choi.123이 아닌 choi가 된다.
해결방법
1. 매개변수 마지막에 :.+를 추가
@GetMapping("/example/{firstValue}/{secondValue:.+}")
2. 매개변수 마지막 괄호 다음 / 추가
@GetMapping("/example/{firstValue}/{secondValue}/")
3. WebMvcConfigurationSupport상속후 설정파일 수정하기
@Configuration
public class CustomWebConfiguration extends WebMvcConfigurationSupport {
@Bean
public RequestMappingHandlerMapping
requestMappingHandlerMapping() {
RequestMappingHandlerMapping handlerMapping
= super.requestMappingHandlerMapping();
handlerMapping.setUseSuffixPatternMatch(false);
return handlerMapping;
}
}
이 글이 많은 사람들에게 도움이 되길 바라며...
요즘 글을 잘안썼네요 좀더 분발!
반응형
'Spring' 카테고리의 다른 글
| [Spring][Security][Oauth] 간단 정리 (0) | 2024.02.20 |
|---|---|
| [Spring] @Transactional 주의사항, Rollback이 안돼요 (0) | 2022.03.19 |
| Spring - @Controller @repository @service를 알아보자 (0) | 2019.03.12 |
| Spring - @Component을 알아보자 (0) | 2019.03.12 |
| Spring - @Bean을 알아보자 (0) | 2019.03.12 |