TIL

TIL) Spring Boot에서 GET API생성

tmddnr3503 2024. 10. 29. 21:00

0. Spring Boot에서 GET API를 생성하고 controller에 적용하기

1. GetController작성

 GetController를 작성 할 때, 클래스위에 다음과 같은 어노테이션을 작성합니다.

@RestController
@RequestMapping("/api/v1/get-api")

 

 @RestController는 이 클래스가 controller라는 것을 명시해줍니다. 또한 @RequestMapping("/api/v1/get-api")는 이 api로 들어오는 API 주소를 명시해 준 것입니다. 즉, 다음 주소로 접속할 수 있습니다.

http://localhost:8080/api/v1/get-api

 

 이 클래스에서 get 메소드를 매핑해 주는 방법으로는 2가지 방법이 있습니다.

@RequestMapping(value = "/requestmapping", method = RequestMethod.GET)
@GetMapping(value = "/getmapping")

 

 위 두가지 방법의 차이는 @GetMapping을 이용하면 method변수의 사용없이 간단하게 get 메소드를 선언할 수 있다는 것입니다.

 

2. GetController에서 한개의 변수받기

 매소드의 매개변수를 어노테이션 주소로 받아 올 수 있습니다.

@GetMapping(value = "/variable1/{variable}")
public String getVariable1(@PathVariable String variable){
  return variable;
}

이때, @GetMapping의 {variable}과 @PathVariable의 variable의 변수명이 같아야합니다. 다른 방법으로는 다음과 같은 코드를 작성할 수 있습니다.

@GetMapping(value = "/variable2/{variable}")
public String getVariable2(@PathVariable("variable") String var){
  return var;
}

 

 @GetMapping은 변하지 않고 {variable}을 사용합니다. 하지만, @PathVariable("variable")로 이름을 같게 만들어준다면 위에서 @PathVariable("variable") String var에서 처럼 변수명을 다르게 생성할 수 있습니다.

 

3. GetController에서 여러개의 변수받기

 GetController에서 여러개의 변수를 받는 방법은 3가지 정도가있습니다.

@GetMapping(value = "/request1")
public String getRequestParam1(
    @RequestParam String name,
    @RequestParam String email,
    @RequestParam String organization){
  return name + " " + email + " " + organization;
}

 

 위와 같이 @RequestParam 어노테이션으로 여러개의 매개변수를 대입받을 수 있습니다. 이때 매개변수가 들어오는 방식은 다음과 같습니다.

http://localhost:8080/api/v1/get-api/request1?
name=flature&
email=thinkground.flature@gmail.com&
organization=thinkground

 

이때, 변수의 이름에 맞게 대입을 해주어야합니다.

 

@RequestParam을 한개만 사용하여 변수를 받는 방법은 다음과 같습니다.

@GetMapping(value = "/request2")
public String getRequestParam2(@RequestParam Map<String, String> params){
  StringBuilder sb = new StringBuilder();

  params.entrySet().forEach(entry -> {
    sb.append(entry.getKey() + " : " + entry.getValue() + "\n");
  });
  return sb.toString();
}

 

 @RequestParam어노테이션에 변수를 Map으로 설정하여 매개변수를 받을 수 있습니다. 이때, 다음과 같은 방법으로 params를 대입할 수 있습니다.

http://localhost:8080/api/v1/get-api/request2?key1=value1&key2=value2

 

 이때, Map<String, String> params에 다음과 같이 대입이 됩니다.

Map key  value
    key1 value1
    key2 value2

 

이때, 2개의 변수를 대입하여 사용할 수 있습니다.

 

 마지막으로 DTO를 이용한 방법입니다. 이 방법을 이용하기 위해 DTO를 선언해야합니다. 

public class MemberDTO{
  private String name;
  private String email;
  private String organization;
  @Override
  public String toString() {
    return "MemberDTO{" +
        "name='" + name + '\'' +
        ", email='" + email + '\'' +
        ", organization='" + organization + '\'' +
        '}';
  }
  
  getter, setter 설정
}

 

위 와같이 DTO를 선언한 후에 DTO를 이용하여 name, email, organization을 대입할 수 있습니다.

@GetMapping(value = "/request3")
public String getRequestParam3(MemberDTO memberDTO){
  //return this.name + " " + this.email + " " + this.organization;
  return memberDTO.toString();
}

 

 아래의 주소로 매개변수를 대입할 수 있습니다.

http://localhost:8080/api/v1/get-api/request3?
						name=flature&
                                                email=thinkground.flature@gmail.com&
                                                organization=thinkground

 

위와 같은 주소로 DTO에 name, email, organization을 대입할 수 있습니다.

 

위의 설명한 방법으로 GetController를 이용하여 정보를 받아올 수 있습니다.

 

2. 내일 해야하는 것

  • Spring 강의 듣기
  • api 명세서 작성하기
  • 과제 erd작성하기
  • CRUD를 이용할 수 있는 SQL 쿼리문 작성하기(Create, Insert, Select, Update, Delete)