Java

@PostMapping은 사실 @RequestMapping(method = RequestMethod.POST)의 단축 버전

99duuk 2025. 4. 8. 17:56

@PostMapping은 사실 @RequestMapping(method = RequestMethod.POST)의 단축 버전

 

@PostMapping은 Spring 4.3부터 추가된 단축 어노테이션

// 요거
@PostMapping("/upload")

// 이거랑 완전히 같음
@RequestMapping(method = RequestMethod.POST, value = "/upload")

 

 

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RequestMapping(method = RequestMethod.POST)
public @interface PostMapping {
    String[] value() default {};
    ...
}

 

 

@GetMapping @RequestMapping(method = RequestMethod.GET)
@PostMapping ...POST
@PutMapping ...PUT
@DeleteMapping ...DELETE
@PatchMapping ...PATCH

마찬가지

 

 

 


+

같은 URL에 다른 Content-Type 처리하고 싶을 때


@RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public String handleFileUpload(@RequestParam MultipartFile file) {...}

@RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public String handleJsonUpload(@RequestBody UploadDto dto) {...}

 

같은 /upload 라우팅이지만,
Content-Type이 다르면 다른 메서드로 자동 매칭됨

'Java' 카테고리의 다른 글

Wrapper Class  (0) 2025.02.09
객체지향에서 상속과 합성의 장단점  (1) 2025.02.04
리플렉션  (1) 2025.01.12
업캐스팅 다운캐스팅  (0) 2025.01.07
LSP  (3) 2024.12.29