PathPattern에 대한 몇가지 예시입니다.
5313
설명
?: 한 문자 일치
/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
{*...}또는**패턴 요소 다음에는 다른 패턴 데이터를 사용할 수 없습니다.
답변 1
MVC 패턴을 정확히 익힐려면 어떻게 해야할까요?
0
364
1
선생님 조언 부탁드립니다.
0
293
0
WebFlux를 실무에 적용하기 전에 고민이 있습니다.
0
316
0
커리큘럼 고민
0
373
1
스프링 백엔드 개발 로드맵
0
459
1
Spring 공부 어떤 강의 순서로 듣는게 좋은가요?
0
684
1
프로젝트와 강의 우선 순위 관련 질문드립니다!
1
472
1
코틀린 개발자로 취업하게 되면서 고민이 생겼습니다.
0
411
1
강의 구입에 관한 질문입니다(설연휴 할인 관련)
0
388
1
학습 방향에 고민이 있습니다.
0
504
1
관리자권한으로 실행 자체가 뜨지 않으면 어떻게 해야할까요?
0
402
1
스프링 선수학습이 필요한가요?
1
514
1





