JAVA Spring
Spring Annotation) @GetMapping, @RequsetMapping 차이
starmk95
2020. 8. 31. 18:45
두 어노테이션 모두 해당 url 입력에 대해 반응하도록 해주는 어노테이션인데
@GetMapping 어노테이션은 @RequestMapping을 특정한 방식으로 사용한 것이라고 할 수 있다.
@GetMapping은 @RequestMapping(method = RequestMethod.GET)과 동일한 기능을 수행한다.
즉, 오른쪽 명령어의 단축어라고 할 수 있다.
풀어말하면 @RequestMapping의 기능으로 해당 url에 대한 요청을 처리하는데, 이를 GET 요청으로 받는다는 것이다.
이를 간단하게 적을 수 있게 @GetMapping이 등장했다.
번외로 @PostMapping, @PutMapping, @DeleteMapping도 있다. (@GetMapping와 같은 원리임)
사용범위에서도 차이가 있는데
@Get/Post/Put/DeleteMapping은 메소드 레벨에만 사용할 수 있지만,
@RequestMapping은 클래스 레벨에도 사용할 수 있다.
@RequestParam("/api") // 클래스 레벨에도 사용 가능
public class HelloController {
// 메소드 레벨에만 사용 가능
@GetMapping("/hello") // web 어플리케이션에서 hello로 들어오면 밑의 메소드를 호출해줌 (Http의 Get 메소드)
public String hello(Model model) {
model.addAttribute("data", "hello!!");
return "hello"; // templates의 hello.html로 연결해줌
}
}