스프링 mvc에서 특정 요청을 처리하는 핸들러를 명시할 때 사용하는 어노테이션을 제공한다.
이를 사용하면 손쉽게 특정 요청을 처리하도록 설정할 수 있고, 간결해서 가독성에도 좋다.
name
: 해당 언노테이션에게 별칭을 붙이기 위해 사용
value
: 처리할 uri 패턴들(String 배열)을 기입.
ex.
RequestMapping(value={"/spring", "/mvc"})
RequestMapping("/spring/{name}")
RequestMapping("/spring/*")
RequestMapping("/spring/**")
RequestMapping("/spring/?")
여기서
?: matches one character
*: matches zero or more characters within a path segment
**: match zero or more path segments
path
: value와 같은 목적
method
: 특정 http method만 요청을 처리하고자 하고자 할 때 사용
ex.
RequestMapping(value={"/spring", "/mvc"}, method=RequestMethod.POST): post 요청만 처리 가능
RequestMapping(value={"/spring", "/mvc"}, method={RequestMethod.POST, RequestMethod.GET}): get, post 요청을 처리가능
params
: 특정 파라미터가 존재하는 경우만 요청을 처리하고자 할 때 사용
ex.
RequestMapping(value="hello", params="id"): /hello?id=123 처리 가능
RequestMapping(value="hello", params="id=1234"): /hello?id=123 처리 불가능, /hello?id=1234 처리 가능
headers
: 특정 헤더가 존재하는 경우만 요청을 처리하고자 할 때 사용
ex.
RequestMapping(value="hello", headers = "content-type"): content-type헤더가 있는 요청만 처리 가능
RequestMapping(value="hello", headers = "content-type=text/*"): content-type헤더가 있고 value가 text/* (와일드카드)인 요청만 처리간능
consumes
: 특정 타입의 바디를 가지는 요청만을 처리하고자 할 때 사용
@RequestMapping(value="/spring", consumes=MediaType.APPLICATION_JSON_VALUE)
위와 같이 요청을 받고자 할 때, Content-Type=application/xml로 요청을 하면
응답코드 415(Unsupported Media Type)을 받게 된다.
produces
: 응답의 타입을 특정 타입으로 설정하고자 할 때 사용
@RequestMapping(value="/spring", produces=MediaType.APPLICATION_JSON_VALUE)
위와 같이 요청을 받고자 할 때, Accept=application/xml로 요청을 하면
응답코드 406(Not Acceptable)을 받게 된다.
참고:
'Spring' 카테고리의 다른 글
RequestParam (0) | 2019.07.02 |
---|---|
ResponseEntity (0) | 2019.06.30 |
HandlerMapping (0) | 2019.06.27 |
Resource Handler (0) | 2019.06.24 |
DispatcherServlet (0) | 2019.06.09 |