더북(TheBook)

5.3.3 메서드에 @RequestMapping 적용

5.3.1절에서는 클래스 수준의 @RequestMapping을 설명했습니다. 이 절에서는 메서드 수준의 @RequestMapping을 알아보겠습니다.

웹에서 사용자가 요청한 URL에 대해 컨트롤러 안의 어떤 메서드를 처리할지 선언하여 사용하는 경우를 메서드 수준의 @RequestMapping이라고 합니다. 그리고 메서드 수준의 @RequestMapping에서 method 속성의 기본값은 GET 방식이므로 생략할 수 있습니다.

다음은 메서드에 @RequestMapping을 적용한 메서드 수준의 @RequestMapping 예입니다. 웹 요청 URL이 http://localhost:8080/.../exam03이면 컨트롤러인 Example03Controller 클래스의 requestMethod() 메서드에 매핑되어 요청을 처리합니다. 이는 앞서 살펴본 클래스 수준의 @RequestMapping을 적용한 결과와 동일합니다.

메서드에 @RequestMapping 적용

package com.springmvc.chap05;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class Example03Controller {

    @RequestMapping(value="/exam03", method=RequestMethod.GET)
    public void requestMethod() {
        System.out.println("@RequestMapping 예제입니다.");
        System.out.println("웹 요청 URL은 /exam03 입니다.");
    }
}
신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.