URL 주소 간소화 👉 RESTFul Service
@PathVariable
어노테이션 값으로 {템플릿 변수}
사용{템플릿 변수}
와 동일한 이름을 갖는 파라미터를 @PathVariable
어노테이션을 이용한 요청처리 메소드에 추가PathVariable.jsp
<a href="user/jo.do">요청1</a><p/>
<a href="user/ybbi.do">요청2</a><p/>
<a href="user/kim.do">요청3</a><p/>
<a href="cafe/kim.do">요청4</a>
PathVariableController.java
@Controller
public class PathVariableController {
@RequestMapping({"/{type}/{id}.do"})
public void test(String type, String id) {
}
}
요청주소가 다 다르면(PathVariable.jsp 참고) 요청 주소별로 메소드를 다 만들어야 하지만
@PathVariable
어노테이션을 활용하면 효율적으로 코드를 작성할 수 있다.
그러나, 유연하다는 장점이 있지만 {type}/{id}.do
경로의 페이지가 존재하면
내가 원하지 않은 요청까지 처리(페이지 실행) 된다는 단점이 있다.
@RequestMapping({"/{type}/{id}.do"})
public void test(String type, String id) {}
해당 메소드에 어노테이션 없이 파라미터만 적어 준다면
URL 주소는 간소화 되지 않고 쿼리스트링 형식으로 출력이 된다.
그러므로 반드시 {템플릿 변수}
와 동일한 이름을 갖는 파라미터를
@PathVariable
어노테이션을 이용한 요청처리 메소드에 추가 해주어야 한다.
@RequestMapping({"/{type}/{id}.do"})
public void test(@PathVariable String type, @PathVariable String id) {
System.out.println("type = " + type);
System.out.println("id = " + id);
}
http://localhost:8080/controller/user/jo.do
요청시 파라미터 확인