728x90
Controller.class
public class HelloController {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String helloGet(...) {
...
}
@RequestMapping(value = "/hello", method = RequestMethod.POST)
public String helloPost(...) {
...
}
@RequestMapping(value = "/hello", method = RequestMethod.PUT)
public String helloPut(...) {
...
}
@RequestMapping(value = "/hello", method = RequestMethod.DELETE)
public String helloDelete(...) {
...
}
}
Spring 개발 시 특정 URL로 요청(Request)을 보내면 Controller에서 어떠한 방식으로 처리할지 정의합니다.
이때 들어온 요청을 특정 method와 매핑하기 위해 사용하는 어노테이션이 바로 @RequestMapping입니다.
- @RequestMapping은 Controller단에서 사용되는데, DispatcherServlet이 Controller 파일을 찾고, 논리적 주소가 매핑된 Method를 찾기 위해서는 @Controller와 @RequesetMapping이 작성되어야 합니다.
- URL와 Controller의 method 매핑을 설정하는 어노테이션
- URL 이외에도 다양한 속성을 지정 가능
- URL 템플릿 기능을 이용하면 URL속의 값을 쉽게 얻을 수 있음
@RequestMapping(value="주소", method=RequestMethod.전송방식)
- value는 요청받을 URL을 설정하게 됩니다.
- method는 어떤 요청으로 받을지 정의합니다. (GET, POST, PUT, DELETE 등등)
@GetMapping
@PostMapping
[ HTTP 주요 메서드 ]
- GET - Server의 리소스를 조회하고자 할 때 - (CRUD에서 R(Read) - 읽기)
- POST - Server의 리소스를 생성하고자 할 때 - (CRUD에서 C(Create) - 생성)
- PUT - Server의 리소스를 수정하고자 할 때 - (CRUD에서 U(Update) - 수정)
- PATCH - Server의 리소스를 일부 수정하고자 할 때 - (CRUD에서 U(Update) - 수정)
- DELETE - Server의 리소스를 삭제할 때 - (CRUD에서 D(Delete) - 삭제)
@RequestMapping(value="주소", method=RequestMethod.전송방식)
@RequestMapping(value = "주소", produces = "text/html;charset=UTF-8")
@RequestMapping(value = "주소", produces = "text/plain;charset=UTF-8")
@RequestParam(name ="n", required = false, defaultValue = "22")
@RequestParam("가져올 데이터의 이름") [데이터타입] [가져온데이터를 담을 변수]
값을 안주면 400 에러
도메인 주소창에 값을 넘겨서 확인하자
@PathVariable
@ResponseBody

728x90
'SPING > Spring 정리' 카테고리의 다른 글
[Spring] Spring Boot Maven (0) | 2023.06.25 |
---|---|
[Spring] Thymeleaf 사용하기 application.properties 설정 (0) | 2023.06.25 |
[Spring] Spting JAXB2 관련 @Annotation(어노테이션) (0) | 2023.06.14 |
[Spring] Spring @ Annotation(어노테이션) (0) | 2023.06.13 |
[Spring] @Bean, @Configuration 사용방법과 뜻 (0) | 2023.06.13 |