@RequestMapping
클라이언트 요청(url)에 맞는 클래스나 메소드와 매핑하기 위해 사용하는 어노테이션이다.
@RequestMapping 속성
value : 요청받을 url 설정
(value만 주어질 경우 생략 가능)
• @RequestMapping(value="/home") ▶ "/home" 요청
@RequestMapping(value = "/home")
public String home() {
return "home"
}
• @RequestMapping(value={"/home ", "/index"}) ▶ "/home" 과 "/index" 요청
@RequestMapping(value = {"/home", "/index"})
public String homeIndex() {
return "home and index";
}
• @RequestMapping(value="/home/*.do") ▶ "/home" 으로 시작하고 ".do" 로 끝나는 요청
@RequestMapping(value = "/home/*.do")
public String home() {
return "ok";
}
method : 어떤 요청을 받을지 정의(GET, POST, PUT, DELETE 등)
@RequestMapping(method=RequestMethod.GET) | ▶ GET 방식(조회) |
@RequestMapping(method=RequestMethod.POST) | ▶ POST 방식(삽입) |
@RequestMapping(method=RequestMethod.PUT) | ▶ PUT 방식(수정) |
@RequestMapping(method=RequestMethod.DELETE) | ▶ DELETE 방식(삭제) |
@Controller
public class HomeController {
@RequestMapping(value = "/home", method = RequestMethod.GET)
public String get(...) {
...
}
@RequestMapping(value = "/home", method = RequestMethod.POST)
public String post(...) {
...
}
@RequestMapping(value = "/home", method = RequestMethod.PUT)
public String put(...) {
...
}
@RequestMapping(value = "/home", method = RequestMethod.DELETE)
public String delete(...) {
...
}
}
content type
• @RequestMapping(consumes="application/json") ▶ 요청 content가 JSON 임
consumes는 필수가 아니라 꼭 지정할 필요가 없다.
• @RequestMapping(produces="application/json") ▶ 응답 content가 JSON 임
produces는 필수가 아니고 따로 지정해주지 않아도 application/json 으로 기본 값이 전달되거나 와일드카드 값이 전달된다.
@RequestMapping ▶ @GetMapping, @PostMapping, @PutMapping, @DeleteMapping
@Controller
public class HomeController {
@RequestMapping(value = "/home", method = RequestMethod.GET)
public String get(...) {
...
}
@RequestMapping(value = "/home", method = RequestMethod.POST)
public String post(...) {
...
}
@RequestMapping(value = "/home", method = RequestMethod.PUT)
public String put(...) {
...
}
@RequestMapping(value = "/home", method = RequestMethod.DELETE)
public String delete(...) {
...
}
}
위와 같은 코드를 다음과 같이 수정할 수 있다.
@Controller
public class HomeController {
@GetMapping("/home") // @GetMapping(value = "/home")
public String get(...) {
...
}
@PostMapping("/home")
public String post(...) {
...
}
@PutMapping("/home")
public String put(...) {
...
}
@DeleteMapping("/home")
public String delete(...) {
...
}
}
또한 @RequestMapping을 클래스 단위에 두어 공통적인 url을 설정할 수 있다.
@Controller
@RequestMapping("/home")
public class HomeController {
@GetMapping()
public String get(...) {
...
}
@PostMapping()
public String post(...) {
...
}
@PutMapping()
public String put(...) {
...
}
@DeleteMapping()
public String delete(...) {
...
}
}
Request의 조건에 따라 호출을 다르게 하도록 설정할 수 있다.
• 특정 파라미터 조건 매핑
URL에 특정 파라미터가 존재할 때만 호출되도록 매핑한다.
다음 예제는 /mapping-param? mode=debug같이 mode=debug가 존재할 때만 호출된다.
@GetMapping(value = "/mapping-param", params = "mode=debug")
public String mappingParam() {
log.info("mappingParam");
return "ok";
}
▶ params="mode",
▶ params="! mode"
▶ params="mode=debug"
▶ params="mode!=debug" (! = )
▶ params = {"mode=debug", "data=good"}
• 특정 헤더로 추가 매핑
Request 헤더에 특정 조건이 존재할 때만 호출되도록 매핑한다.
다음 예제는 /mapping-header로 요청이 왔을 때, 헤더에 mode=debug가 존재할 때만 호출된다.
@GetMapping(value = "/mapping-header", headers = "mode=debug")
public String mappingHeader() {
log.info("mappingHeader");
return "ok";
}
▶ headers="mode",
▶ headers="! mode"
▶ headers="mode=debug"
▶ headers="mode!=debug" (! = )
참고
https://docs.spring.io/spring-framework/reference/web/webmvc/mvc-controller/ann-requestmapping.html
https://sharonprogress.tistory.com/174
https://velog.io/@brightbell212/Spring-MVC-%EA%B5%AC%EC%A1%B0%EC%99%80-%EB%8F%99%EC%9E%91%EC%9B%90%EB%A6%AC-Controller-RequestMapping
'Spring' 카테고리의 다른 글
[Spring] @PathVariable (0) | 2024.09.23 |
---|---|
[Spring] @NoArgsConstructor, @AllArgsConstructor, @RequiredArgsConstructor (0) | 2024.09.20 |
[Spring] @Controller, @RestController (0) | 2024.09.16 |
[Spring] @Transactional 안 되는 경우 (0) | 2024.09.13 |
[Spring] Swagger 적용(Springboot 3.x) (0) | 2024.09.11 |