카테고리 없음

Spring Boot @RequestMapping

소리소리솔소리 2023. 3. 14. 11:10

우리는 특정 uri로 요청을 보내면 Controller에서 어떠한 방식으로 처리할지 정의 한다.

이때 들어온 요청을 특정 메서드와 매핑하기 위해 사용하는 것이 @RequestMapping이다.

@RequestMapping에서 가장 많이사용하는 부분은 value와 method이다.

 

value는 요청받을 url을 설정하게 된다.

method는 어떤 요청으로 받을지 정의하게 된다.(GET, POST, PUT, DELETE 등)

@RequestMapping(value = "/hello", method = RequestMethod.GET)

 

 

공통적인 url은 class에 @RequestMapping으로 설정을 해주었다.

그리고 @GetMapping, @PostMapping, @PutMapping, @DeleteMapping으로 간단하게 생략이 가능해졌다.

뒤에 추가적으로 url을 붙이고 싶다면 @GetMapping, @PostMapping, @PutMapping, @DeleteMapping 에 추가적인 url을 작성하면 된다.

 

@RestController
@RequestMapping(value = "/hello")
public class HelloController {    
    @GetMapping("/hi")
    public String helloGetHi(...) {
        ...
    }
}

 

위에있는 helloGetHi에 들어가기 위해서는 /hello/hi로 들어가야 한다.

@RequestMapping은 Class와 Method에 붙일 수 있고 @GetMapping, @PostMapping, @PutMapping, @DeleteMapping들은 Method에만 붙일 수 있다.