Spring Controller에서 특히 REST API에서 특수문자를 허용해야 하는 경우가 있습니다.
(특정 버전 이후에는 특수문자가 그냥 허용된다고 합니다.)
예를 들어서 다음과 같은 경우입니다.
문자가 잘리는 예시
1. 요청 URL
GET http://localhost:8080/test/yjh5369@tistory.com
2. Controller
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
public class TestController {
@GetMapping("/user/{email}")
public String getUserInfo(@PathVariable String email) {
log.info("email:"+email);
return email;
}
}
특수문자를 허용하는 방법
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
public class TestController {
@GetMapping("/user/{email:.+}")
public String getUserInfo(@PathVariable String email) {
log.info("email:"+email);
return email;
}
}
[참고]
https://stackoverflow.com/questions/16332092/spring-mvc-pathvariable-with-dot-is-getting-truncated
728x90
'웹 개발' 카테고리의 다른 글
spring boot의 application.properties에서 @Scheduled의 cron값을 설정하는 방법 (0) | 2022.03.13 |
---|---|
html파일을 local에서 실행할 때 CORS 에러가 발생하는 이유 (0) | 2022.03.02 |
[spring] Json return 시, null 항목 제거하는 방법 (0) | 2022.02.13 |
[java Spring] RestTemplate SSL ignore (0) | 2022.01.26 |
Spring Boot OAuth2 – AuthorizationServer (0) | 2022.01.06 |