[Spring] @GetMapping 과 @PostMapping

히태하태·2021년 11월 2일
0

Spring

목록 보기
1/1

@GetMapping @PostMapping

Spring 4.3 이후에 추가된 Get/Post 요청 전용 매핑 어노테이션.

원래 Spring은 @RequestMapping 어노테이션으로만 HTTP Request 모든 Url 을 매핑할 수 있었다. 아래와 같이 처리 할 수 있었다.

@RequestMapping(value = "/test", method = RequestMethod.GET)
public Test getTest() {
}

@RequestMapping(value = "/test", method = RequestMethod.POST)
public Test getTest(Test test) {
}
@RequestMapping(value = "/test/{id}", method = RequestMethod.GET)
public Test getTest(@PathVariable("id") String id) {
}

4.3버전 이후로 추가된 HTTP Request 어노테이션을 사용함으로써 attribute 를 더이상 사용 할 필요 없어졌다.

@GetMapping(value = "/test")
public Test getTest() {
}
@PostMapping(value = "/test")
public Test setTest(Test test) {
}
@GetMapping(value = "/test/{id}")
public Test getTest(@PathVariable("id") String id) {
}

내부적으로 보면 @PostMapping 어노테이션은 @RequestMapping(method = RequestMethod.POST) 어노테이션의 shortcut 이다. 정확히 일치하게 동작하며, 사용 편의성을 위해 만들어졌다고 보면된다.

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RequestMapping(method = RequestMethod.POST)
public @interface PostMapping {
	//code
}

참고

https://howtodoinjava.com/spring5/webmvc/controller-getmapping-postmapping/

profile
시작이 반이다. 일단 시작해보자.

0개의 댓글