Spring Boot | Spring MVC의 간단한 사용법 | URL 매핑
코드 작성
src/main/java/sample/springboot/web/HelloController.java
package sample.springboot.web;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/hello")
public class HelloController {
@RequestMapping(method=RequestMethod.GET)
public String getMethod() {
return "get";
}
@RequestMapping(method=RequestMethod.POST)
public String postMethod1() {
return "post";
}
@RequestMapping(value="/hey", method=RequestMethod.POST)
public String postMethod2() {
return "hey post";
}
}
실행 결과
curl로 테스트한다.
$ curl http://localhost:8080/hello
get
$ curl http://localhost:8080/hello -X POST
post
$ curl http://localhost:8080/hello/hey -X POST
hey post
설명
- @RequestMapping 메서드 (클래스)와 경로를 매핑한다.
- value 속성에 경로를 지정한다.
- method 속성에 HTTP 메소드를 지정한다.