상세 컨텐츠

본문 제목

Spring MVC @RequestMapping - 요청받기

프레임워크/Spring Boot

by 최승호 2022. 5. 19. 12:00

본문

https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#mvc-ann-requestmapping

 

Web on Servlet Stack

Spring Web MVC is the original web framework built on the Servlet API and has been included in the Spring Framework from the very beginning. The formal name, “Spring Web MVC,” comes from the name of its source module (spring-webmvc), but it is more com

docs.spring.io

 

@RequestMapping 어노테이션은

컨트롤러에서 HTTP 요청에 대해 URL, 요청방식, 요청 파라미터, 헤더, 미디어 타입 등의 정보를 담고있다. 클래스에 선언하여 공통의 경로를 설정할 수 있고 메소드에 선언하여 엔드포인트에서 실행할 부분을 지정할 수 있다.

 

@RequestMapping의 하위 어노테이션

  • @GetMapping
  • @PostMapping
  • @PutMapping
  • @DeleteMapping
  • @PatchMapping

속성값

속성 의미(예시)
name  매핑에 이름을 지정한다.
value  URI 경로 혹은 패턴을 매핑한다.

ex)
value = "/resources/ima?e.png" - 하나의 문자 일치

value = "/resources/*.png" - 0개 이상의 문자 일치
value = "/resources/**" - 모든 경로 일치
value = "/projects/{project}/versions" - 변수로 가져옴
value = "/projects/{project:[a-z]+}/versions" - 정규표현식을 사용하여 변수로 가져옴
path  value와 동일
method  지정된 http 요청 메소드만 처리한다.

ex)
method = RequestMethod.POST
method = {RequestMethod.POST, RequestMethod.GET})
params 지정된 파라미터와 일치하는것만 처리한다.

ex)
params = "myParam" - myParam이라는 파라미터가 있을 경우만 처리
params = "!myParam" - myParam이라는 파라미터가 없을 경우만만 처리
params = "myParam=myValue" myParam 팔미터의 값이 myValue일 경우만 처리
headers 지정된 헤더가 있을 경우만 처리한다.

ex)
headers = "myHeader=myValue
headers = "content-type=text/*
consumes 요청의 Content-Type이 일치하는 것만 처리한다.

consumes = "text/plain"
consumes = {"text/plain", "application/*"}
consumes = MediaType.TEXT_PLAIN_VALUE
produces Accept 요청 헤더가 일치하는 것만 처리한다.

produces = "text/plain"
produces = {"text/plain", "application/*"}
produces = MediaType.TEXT_PLAIN_VALUE
produces = "text/plain;charset=UTF-8"

 

 

 

 

 

관련글 더보기

댓글 영역