@RequestMapping
2024. 9. 15. 12:53ㆍ정보처리,전산/springboot
반응형
@RequestMapping은 스프링 프레임워크에서 클라이언트가 서버에 요청을 보낼 때 그 요청을 특정 메서드에 매핑하여 URL, HTTP 메소드, 요청 파라미터, 헤더 등 다양한 조건을 기반으로 요청을 처리하는 메서드를 지정할 수 있다.
@RequestMapping 주요 요소
- value (또는 path):
- URL 패턴을 정의한다.
- 클라이언트가 어떤 URL로 요청할 때 해당 메서드가 호출될지를 결정한다.
- 예: @RequestMapping("/home")는 /home URL에 매핑된다.
- method:
- HTTP 메소드(GET, POST, PUT, DELETE 등)를 지정한다.
- 여러 메소드를 함께 지정할 수도 있다.
- 예: @RequestMapping(value = "/submit", method = RequestMethod.POST)는 POST 요청에만 응답한다.
- 기본적으로는 모든 HTTP 메소드를 허용한다.
- params:
- 요청의 특정 파라미터가 포함되어 있을 때만 매핑된다.
- 예: @RequestMapping(value = "/search", params = "type=product")는 type=product라는 파라미터가 있을 때만 요청을 처리한다.
- headers:
- 특정 HTTP 헤더를 기준으로 요청을 매핑한다.
- 예: @RequestMapping(value = "/api", headers = "content-type=application/json")는 요청 헤더에 content-type이 application/json일 때만 처리된다.
@RestController
public class MyController {
// GET 요청 처리
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello() {
return "Hello, World!";
}
// POST 요청과 특정 파라미터를 처리
@RequestMapping(value = "/submit", method = RequestMethod.POST, params = "action=save")
public String submitForm() {
return "Form submitted!";
}
// 특정 헤더에 따른 요청 처리
@RequestMapping(value = "/api/data", headers = "content-type=application/json")
public String getData() {
return "Data in JSON format";
}
}
반응형
'정보처리,전산 > springboot' 카테고리의 다른 글
@RequestParam (0) | 2024.09.15 |
---|---|
Gradle 의존성 추가 키워드 (0) | 2024.06.27 |
Annotations (0) | 2024.06.21 |
jetbrains IntelliJ HotKey (0) | 2024.06.21 |
GRADLE , MAVEN (0) | 2024.06.21 |