Kotlin/Kotlin SpringBoot
[Kotlin SpringBoot - 2] Rest API 요청 만들기 (1)
밥보92
2023. 5. 1. 15:58
반응형
JDK 11, Spring Boot 2.7.8, IntelliJ IDE를 기준으로 작성 |
@RestController 어노테이션이란
/**
* A convenience annotation that is itself annotated with
* {@link Controller @Controller} and {@link ResponseBody @ResponseBody}.
* <p>
* Types that carry this annotation are treated as controllers where
* {@link RequestMapping @RequestMapping} methods assume
* {@link ResponseBody @ResponseBody} semantics by default.
*
* <p><b>NOTE:</b> {@code @RestController} is processed if an appropriate
* {@code HandlerMapping}-{@code HandlerAdapter} pair is configured such as the
* {@code RequestMappingHandlerMapping}-{@code RequestMappingHandlerAdapter}
* pair which are the default in the MVC Java config and the MVC namespace.
*
* @author Rossen Stoyanchev
* @author Sam Brannen
* @since 4.0
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {
...
- 간단한 Controller 클래스를 생성 후 @RestController 어노테이션을 클래스에 선언 후 RestController 어노테이션이 어떻게 구성되어 있는지 살펴보면 위와 같이 구성되어 있는 것을 확인할 수 있음
- 크게 @Controller 어노테이션과 @ResponseBody 어노테이션으로 @RestController 어노테이션이 결합되어 있는 것이 특징인데 여기서 알 수 있는 특징은 @RestController 어노테이션을 사용하면 @Controller 어노테이션을 사용할 때 응답 메서드에서 @ResponseBody 어노테이션을 사용했던 내용을 생략할 수 있다는 것
@RestContorller 어노테이션을 통해 해당 클래스가 프로젝트의 Rest Api 엔트리포인트임을 선언할 수 있음
import org.springframework.web.bind.annotation.*
@RestController
class SimpleController {
@RequestMapping("/simple1")
fun simpleApi() = "ok"
...
}
REST API 요청 만들기
그럼 실제로 @RestController 클래스 내에서 Rest API의 요청을 만들기 위해서는 크게 아래와 같은 어노테이션을 사용할 수 있음
어노테이션 | 특징 |
@RequestMapping | 경로 지정과 함께 어떤 Method를 사용할 지 지정해야 함 (Method를 지정하지 않는다면 모든 Method) |
@GetMapping @PostMapping @PutMapping @DeleteMapping @PatchMapping |
경로 지정만 하면 됨, 명시적으로 어떤 Method로 요청을 받을지 어노테이션 이름에서 나타남 |
A. @RequestMapping
경로만 지정하여 모든 Method에서 사용가능하도록 할 수 있음, 경로를 여러 개 지정 가능
@RequestMapping("경로")
ex)
@RequestMapping("/simple1")
fun simpleApi() = "ok"
@RequestMapping(path = ["경로1", "경로2", ... ])
ex)
@RequestMapping(path = ["/simple1", "/simple2"])
fun simpleApi() = "ok"
특정 Method에서의 사용을 지정할 수 있으며, 여러 Method에 요청이 Mapping 되도록 할 수 있음
@RequestMapping("경로", method = [Method1, Method2, ... ])
ex)
@RequestMapping("/simple1", method = [RequestMethod.GET, RequestMethod.POST])
fun simpleApi() = "ok"
Method가 겹치지 않는다면 동일한 경로에 대해서 각각 다른 함수에 Mapping이 가능
@RestController
class SimpleController {
@RequestMapping("/simple1", method = [RequestMethod.GET])
fun simpleApi() = "GET"
@RequestMapping("/simple1", method = [RequestMethod.POST])
fun simpleApi2() = "POST"
}
> 복수의 Method로 하나의 함수에 Mapping하여 처리하는 Api인 경우 @RequestMapping 어노테이션으로 처리가 유리
B. @{Method}Mapping
어노테이션에서 사용할 Mehtod가 정의된 상태 경로만 지정하여 요청을 Mapping할 수 있음
@GetMapping("경로")
@PostMapping("경로")
@PutMapping("경로")
@DeleteMapping("경로")
@PatchMapping("경로")
ex)
@RestController
class SimpleController {
@PostMapping("/simple1")
fun simplePost() = "ok"
@PutMapping("/simple2")
fun simplePut() = "ok"
@GetMapping("/simple2")
fun simpleGet() = "ok"
@DeleteMapping(path = ["/simple1","/simple2"])
fun simpleDelete() = "ok"
}
> @RequestMapping에 의해 형태가 간단하며 복수의 Method에서 처리되는 요청인 경우 명시적인 처리에 유리
반응형