inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

스프링 MVC 2편 - 백엔드 웹 개발 활용 기술

스프링 인터셉터 - 요청 로그

PathPattern에 대한 몇가지 예시입니다.

5313

김회민
2

설명

PathPattern 공식 문서

?: 한 문자 일치

*: 경로(/) 안의 모든 문자 일치

**: 하위 경로 모든 문자 일치

{spring}: spring 이라는 변수로 캡처

{*spring}: 하위 경로 끝까지 spring변수에 캡쳐

{spring:[a-z]+}: 정규식 이용

 

예제 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

{*...} 또는 ** 패턴 요소 다음에는 다른 패턴 데이터사용할 수 없습니다.

spring mvc

답변 1

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