프로그래밍/JAVA

@RequestMapping 어노테이션

ITJun 2024. 5. 9. 21:37
반응형
  • 모든 HTTP 메소드(GET, POST, PUT, DELETE 등)에 대해 사용할 수 있어 높은 유연성 제공
  • 하나의 메소드에서 여러 HTTP 메소드를 처리하도록 설정 가능 (예: method = {RequestMethod.GET, RequestMethod.POST})
  • 하나의 URL에 여러 HTTP 메소드를 처리해야 할 때.
  • 복잡한 요청 매핑 조건을 설정해야 할 때.
  • RequestMethod.??? 을 명시적으로 사용해야한다
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String handleGet() {
    // GET 요청 처리
    return "Handled GET";
}

@RequestMapping(value = "/test2", method = RequestMethod.POST)
public String handlePost() {
    // POST 요청 처리
    return "Handled POST";
}

@RequestMapping(value = "/test3", method = RequestMethod.DELETE)
public String handleDelete() {
    // DELETE 요청 처리
    return "Handled DELETE";
}

@RequestMapping(value = "/test4", method = RequestMethod.PUT)
public String handlePut() {
    // PUT 요청 처리
    return "Handled PUT";
}

@RequestMapping(value = "/example", method = {RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT, RequestMethod.DELETE})
public String handleMultipleRequests() {
    // 이 메소드는 GET, POST, PUT, DELETE 요청 모두 처리
    return "Handled GET, POST, PUT, and DELETE";
}

 

반응형