GET 요청 처리 어노테이션

마라탕천재 ㅣ 2024. 7. 7. 12:29

1. @RestController

@RestController는 Spring MVC에서 RESTful 웹 서비스의 컨트롤러를 정의할 때 사용한다.

이 어노테이션은 클래스가 HTTP 요청을 처리하고 JSON 또는 XML 형태로 응답을 반환할 수 있게한다.

내부적으로 @Controller와 @ResponseBody를 합친 역할을 한다.

@RestController
@RequestMapping("/api")
public class GetController {
    // 컨트롤러의 메소드들이 정의될 위치
}

 

2. @RequestMapping

@RequestMapping은 클래스나 메소드 수준에서 요청 URL을 매핑하는 데 사용된다.

클래스 수준에서는 공통 URL 경로를 정의하고, 메소드 수준에서는 특정 HTTP 메소드(GET, POST 등)와 세부 경로를 정의한다.

@RestController
@RequestMapping("/api")
public class GetController {
    // "/api" 경로에 대한 요청을 처리하는 메소드들이 정의될 위치
}

 

3. @GetMapping

@GetMapping은 HTTP GET 요청을 특정 메소드에 매핑하는 역할을 한다.

@RequestMapping(method = RequestMethod.GET)의 축약형이다.

@GetMapping("/request1")
public String getRequestParam(@RequestParam Map<String, String> param) {
    // GET 요청을 처리하는 메소드
}