Spring Boot/Framework

[Springboot] GET API

오늘도개발 2024. 1. 27. 18:26

 

1. Get API 란?

 

 - 리소스를 취득하는 작업

 - CRUD의 R(read)에 해당

 

2. Get 관련 Annotation 

 - RequestMapping(path = "uri" , method = RequestMethod.GET) :

  • RequestMapping 은 get, post, delete, put 등 다양한 방법으로 사용될 수 있기때문에 뒤에 따로 method를 지정해야한다.
  • 예전에 사용하던 방식으로 GetMapping으로 대체할 수 있다.

 

   @GetMapping(path = "/hello") // Get 으로만 동작
    public String getHello() {
        return "hello";
    }

    @RequestMapping(path = "/hi", method = RequestMethod.GET) // 예전 방식
    public String getHi() {
        return "hi";
    }

 

 

 - GetMapping(path = "uri" ) :

  • RequestMapping 보다 간결하게 사용할 수 있다.
  • 값을 받아오는 경우 다음과 같이 받을 수 있다.

  • dto 를 사용하여 다음과 같이 받는 것이 일반적 이다.