[ Spring ] @RequestMapping Annotation

2024. 2. 17. 16:19· BACK-END/└ Spring Boot

환경 : Spring Tool Suite4

 

[ @RequestMapping ] 

@RequestMapping은 스프링 프레임워크에서 컨트롤러 클래스 또는 메서드에 URL 매핑을 지정하는 데 사용되는 어노테이션입니다. 이 어노테이션을 사용하면 클라이언트의 HTTP 요청이 특정한 URL에 매핑되는 메서드를 실행할 수 있습니다.

 

즉, 어떤 URL로부터의 요청에 대해 어떤 메서드가 처리를 할지 지정하는 역할.


[ 사용 예 ]

 

  - 전체코드

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

@Controller
@RequestMapping("/example")
public class ExampleController {

    @RequestMapping("/hello")
    @ResponseBody
    public String hello() {
        return "Hello, World!";
    }
}

    (+) @RequestMapping("/example")ExampleController 클래스에 대한 URL 매핑을 지정

    (+) @RequestMapping("/hello")/example/hello URL에 대한 매핑

 

   - URL

localhost:8080/example/hello

    (+) hello() 메서드가 실행되어 "Hello, World!" 가 반환되는 것을 확인할 수 있음


 

(추가) @ResponseBody

@ResponseBody는 메서드 반환 값이 HTTP 응답 본문으로 전송됨을 나타낸다. 

 

 

 

 

 

 

 

 

개인 공부 기록용입니다:)

728x90

'· BACK-END > └ Spring Boot' 카테고리의 다른 글

[ Spring MVC ] MVC Patterns  (1) 2024.03.25
[ Spring ] @Configuration Annotation  (0) 2024.03.24
[ Spring ] @RequestParam Annotation  (0) 2024.02.17
[ Spring ] @ModelAttribute Annotation  (0) 2024.02.15
[ Spring ] @Autowired Annotation  (0) 2024.02.14