설명
?: 한 문자 일치
/pages/t?st.htmlYES:
/pages/test.html,/pages/tXst.htmlNO :
/pages/toast.html
*: 경로(/) 안의 모든 문자 일치
/resources/*.pngYES:
/resources/photo.pngNO :
/resources/favority.ico
**: 하위 경로 모든 문자 일치
/resources/**/resources/image.png,/resources/css/spring.css
{spring}: spring 이라는 변수로 캡처
/resources/{path}/resources/robot.txt->path변수에 "robot.txt" 할당@PathVariable("path")로 접근 가능
{*spring}: 하위 경로 끝까지 spring변수에 캡쳐
/items/{*path}/items/1/add->path변수에 "/1/add" 할당
{spring:[a-z]+}: 정규식 이용
/items/{path:[a-z]+}YES:
/items/robotsNO :
/items/123
예제 1 - {*spring}
@GetMapping("/hello/{*name}")
@ResponseBody
public String handleTest(
@PathVariable String name
) {
log.info("name = {}", name);
return name;
}GET http://localhost:8080/hello/path-test
->
name = /path-test
===
GET http://localhost:8080/hello/path-test/other
->
name = /path-test/other
예제 2 - 정규식
@GetMapping("/static/{name:[a-z-]+}-{version:\\d\\.\\d\\.\\d}{ext:\\.[a-z]+}")
@ResponseBody
public String handle(
@PathVariable String name,
@PathVariable String version,
@PathVariable String ext
) {
log.info("name = {}", name);
log.info("version = {}", version);
log.info("ext = {}", ext);
return "/" + name + "/" + version + "/" + ext;
}GET http://localhost:8080/pathtest-1.0.0.jar
->
name = pathtest
version = 1.0.0
ext = .jar
잘못된 사용
@GetMapping("/static/{*fullpath}{name}")
@ResponseBody
public String runtimeError(
@PathVariable String fullpath,
@PathVariable String name
) {
log.info("fullpath = {}", fullpath);
log.info("name = {}", name);
return name;
}
Description:
Invalid mapping pattern detected: /static/{*fullpath}{name}
^
No more pattern data allowed after {*...} or ** pattern element
{*...}또는**패턴 요소 다음에는 다른 패턴 데이터를 사용할 수 없습니다.
좋은 내용 공유 감사합니다^^
답글