Home > Spring > πŸƒ[Spring] νšŒμ› κ°€μž… - μœ μ € 생성 API κ°œλ°œν•˜κΈ°.

πŸƒ[Spring] νšŒμ› κ°€μž… - μœ μ € 생성 API κ°œλ°œν•˜κΈ°.
Spring Framework

πŸƒ[Spring] νšŒμ› κ°€μž… - μœ μ € 생성 API κ°œλ°œν•˜κΈ°.

1️⃣ API μ„€κ³„ν•˜κΈ°.

1️⃣ μ‚¬μš©μžλ₯Ό λ“±λ‘ν•˜κΈ° μœ„ν•œ API λͺ…μ„Έ.

  • νšŒμ› κ°€μž… μ›Ή UIκ°€ λ¨Όμ € λ§Œλ“€μ–΄μ Έ μžˆμŠ΅λ‹ˆλ‹€.
    • κ·ΈλŸ¬λ―€λ‘œ ν”„λ‘ νŠΈμ—”λ“œμ—μ„œ μ‚¬μš©ν•œ API, 즉 κΈ°λŠ₯듀을 μ±„μ›Œ λ„£μ–΄μ•Ό ν•©λ‹ˆλ‹€.
      • νšŒμ› κ°€μž…μ„ μœ„ν•œ API λͺ…μ„ΈλŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.
        • HTTP Method : POST
        • HTTP Path : /register
        • Http Body (JSON)
          {
              "username": String (null λΆˆκ°€λŠ₯),
              "email": String (null λΆˆκ°€λŠ₯),
              "password": String (null λΆˆκ°€λŠ₯),
              "confirmPassword": String (null λΆˆκ°€λŠ₯)
          }
        
        • κ²°κ³Ό λ°˜ν™˜ X (HTTP μƒνƒœ 200OK 이면 좩뢄함)

πŸ™‹β€β™‚οΈ API(Applicatioon Programming Interface)λž€ λ¬΄μ—‡μΌκΉŒ?

2️⃣ UserController μƒμ„±ν•˜κΈ°.

  • μš°μ„  UserControllerλ₯Ό μƒμ„±ν•œ ν›„ 내뢀에 λ‹€μŒκ³Ό 같이 μ½”λ“œλ₯Ό μƒμ„±ν•΄μ€λ‹ˆλ‹€.
package com.kobe.signUpApp.controller.user;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @PostMapping("/register")
    public void registerUser() {

    }
}

3️⃣ dto νŒ¨ν‚€μ§€ -> user νŒ¨ν‚€μ§€ -> request νŒ¨ν‚€μ§€ -> UserCreateRequest 클래슀 μƒμ„±ν•˜κΈ°

  • μ•„λž˜μ™€ 같이 dto 객체인 userCreateRequest 클래슀λ₯Ό μƒμ„±ν•΄μ€λ‹ˆλ‹€.
package com.kobe.signUpApp.dto.user.request;

public class UserCreateRequest {
    private String username;
    private String email;
    private String password;
    private String confirmPassword;

    public String getUsername() {
        return username;
    }

    public String getEmail() {
        return email;
    }

    public String getPassword() {
        return password;
    }

    public String getConfirmPassword() {
        return confirmPassword;
    }
}

4️⃣ UserController λ‚΄λΆ€ registerUser λ©”μ„œλ“œ λ¦¬νŒ©ν† λ§.

  • POST APIμ—μ„œ λ“€μ–΄μ˜¨ HTTP Bodyλ₯Ό μœ„μ—μ„œ λ§Œλ“  UserCreateRequest 클래슀둜 λ³€ν™˜ν•˜κΈ° μœ„ν•΄μ„œ @RequestBody μ–΄λ…Έν…Œμ΄μ…˜μ„ μ‚¬μš©ν•΄μ€λ‹ˆλ‹€.
package com.kobe.signUpApp.controller.user;

import com.kobe.signUpApp.dto.user.request.UserCreateRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @PostMapping("/register")
    public void registerUser(@RequestBody UserCreateRequest request) {

    }
}
  • μ—¬κΈ°κΉŒμ§€ API의 ꡬ쑰λ₯Ό μž‘μ•˜μŠ΅λ‹ˆλ‹€.

5️⃣ μ‹€μ œ μœ μ €κ°€ μ €μž₯λ˜λ„λ‘ κ΅¬ν˜„ν•˜κΈ°.

  • μœ μ €λ₯Ό μ €μž₯ν•˜κΈ° μœ„ν•˜μ—¬ UserλΌλŠ” 객체λ₯Ό λ§Œλ“€μ–΄μ„œ μ €μž₯ν•  것 μž…λ‹ˆλ‹€.
    • λ‹€μŒκ³Ό 같은 과정을 κ±°μ³μ„œ λ§Œλ“€μ–΄ λ³Ό 것 μž…λ‹ˆλ‹€.
      • λ¨Όμ €, domain νŒ¨ν‚€μ§€ 내뢀에 userλΌλŠ” νŒ¨ν‚€μ§€λ₯Ό λ§Œλ“€μ–΄ μ€λ‹ˆλ‹€.
      • user νŒ¨ν‚€κΈ° 내뢀에 User 클래슀λ₯Ό λ§Œλ“€μ–΄ μ€λ‹ˆλ‹€.
          @PostMapping("/register")
          public void registerUser(@RequestBody UserCreateRequest request) {
          }  
        
      • μœ„ APIκ°€ μ‚¬μš©λ˜μ„œ μœ μ €κ°€ μ €μž₯λ˜μ–΄μ•Ό ν•œλ‹€λ©΄ User 객체λ₯Ό λ§Œλ“€μ–΄μ„œ μ‹€μ œ λ¦¬μŠ€νŠΈμ— μ €μž₯을 ν•  것 μž…λ‹ˆλ‹€.
  • User의 κ²½μš°μ—λŠ” λ‹€μŒκ³Ό 같은 ν•„λ“œκ°€ ν¬ν•¨λ©λ‹ˆλ‹€.
    • username
    • email
    • password
    • confirmPassword
package com.kobe.signUpApp.domain.user;

public class User {
    private String username;
    private String email;
    private String password;
    private String confirmPassword;

    public User(
            String username,
            String email,
            String password,
            String confirmPassword
    ) {
        if (username == null || username.isBlank()) {
            throw new IllegalArgumentException(String.format("잘λͺ»λœ username(%s)이 λ“€μ–΄μ™”μŠ΅λ‹ˆλ‹€.", username));
        } else if (email == null || email.isBlank()) {
            throw new IllegalArgumentException(String.format("잘λͺ»λœ email(%s)이 λ“€μ–΄μ™”μŠ΅λ‹ˆλ‹€.", email));
        } else if (password == null || password.isBlank()) {
            throw new IllegalArgumentException(String.format("잘λͺ»λœ password(%s)이 λ“€μ–΄μ™”μŠ΅λ‹ˆλ‹€.", password));
        } else if (confirmPassword == null || confirmPassword.isBlank()) {
            throw new IllegalArgumentException(String.format("잘λͺ»λœ confirmPassword(%s)이 λ“€μ–΄μ™”μŠ΅λ‹ˆλ‹€.", confirmPassword));
        }


        this.username = username;
        this.email = email;
        this.password = password;
        this.confirmPassword = confirmPassword;
    }
}
  • User 객체λ₯Ό λ§Œλ“€ λ•Œ, μƒμ„±μž(Constructor)λ₯Ό 톡해 ν•„λ“œμ— 값을 λ„£μ–΄μ€λ‹ˆλ‹€.
    • 이 λ•Œ, null이 λ“€μ–΄ μ™€μ„œλŠ” μ•ˆλ˜λŠ” ν•„λ“œλ“€μ„ μœ„ν•΄ 쑰건문을 μ‚¬μš©ν•˜μ—¬ null 값이 듀어왔을 λ•Œ IllegalAragumentException μ˜ˆμ™Έλ₯Ό λ˜μ§‘λ‹ˆλ‹€(throw).
      • 즉, null 값이 λ“€μ–΄μ˜¬ 경우 μ˜ˆμ™Έκ°€ λ˜μ Έμ§€λ―€λ‘œ User 객체 μžμ²΄κ°€ μƒμ„±λ˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμ— μ €μž₯도 λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.
      • 그리고 μ˜ˆμ™Έλ₯Ό λ˜μ§€λŠ” κ³Όμ •μ—μ„œ μ–΄λ–€ 값이 λ“€μ–΄μ™”λŠ”μ§€ μ•Œλ €μ£Όλ©΄ μ’‹κΈ° λ•Œλ¬Έμ— String.format을 μ‚¬μš©ν•˜μ—¬ μ˜ˆμ™Έμ˜ λ©”μ„Έμ§€λ‘œ 담을 수 μžˆκ²Œλ” μ²˜λ¦¬ν–ˆμŠ΅λ‹ˆλ‹€.

6️⃣ User 클래슀λ₯Ό μΈμŠ€ν„΄μŠ€ν™” μ‹œμΌœ μ €μž₯되게 λ§Œλ“€κΈ°.

  • UserController 클래슀 μ•ˆμ— λ‹€μŒκ³Ό 같이 μ½”λ“œλ₯Ό μž‘μ„±ν•΄μ€λ‹ˆλ‹€.
package com.kobe.signUpApp.controller.user;

import com.kobe.signUpApp.domain.user.User;
import com.kobe.signUpApp.dto.user.request.UserCreateRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class UserController {

    private final List<User> users = new ArrayList<>();

    @PostMapping("/register")
    public void registerUser(@RequestBody UserCreateRequest request) {
        users.add(new User(request.getUsername(), request.getEmail(), request.getPassword(), request.getConfirmPassword()));
    }
}
  • λ¨Όμ € private final List<User> users = new ArrayList<>(); 둜 리슀트λ₯Ό λ§Œλ“€μ–΄ μ€λ‹ˆλ‹€.
  • registerUserλΌλŠ” ν•¨μˆ˜κ°€ 호좜되면, 즉 POST/registerUserκ°€ 호좜될 경우 users.add(new User(request.getUsername(), request.getEmail(), request.getPassword(), request.getConfirmPassword())); κ°€ μ‹€ν–‰ λ©λ‹ˆλ‹€.
    • Userλ₯Ό λ§Œλ“€λ•ŒλŠ” APIλ₯Ό 톡해 λ“€μ–΄μ˜¨ μœ μ €μ΄λ¦„, 이메일, λΉ„λ°€λ²ˆν˜Έ, ν™•μΈμš© λΉ„λ°€λ²ˆν˜Έλ₯Ό 집어 λ„£μ–΄μ€λ‹ˆλ‹€
      • 즉, μœ„ μ½”λ“œλŠ” β€œAPIλ₯Ό 톡해 λ“€μ–΄μ˜¨ μœ μ €μ΄λ¦„, 이메일, λΉ„λ°€λ²ˆν˜Έ, ν™•μΈμš© λΉ„λ°€λ²ˆν˜Έλ₯Ό 집어 λ„£λŠ” μ½”λ“œμž…λ‹ˆλ‹€.”

7️⃣ μš”μ•½.

  • POST/registerUserκ°€ 호좜될 경우, UserController 내뢀에 μžˆλŠ” registerUser ν•¨μˆ˜κ°€ μ‹€ν–‰λ©λ‹ˆλ‹€.
    • 이 λ•Œ, JSON ν˜•μ‹μœΌλ‘œ HTTP Body에 username, email, password, confirmPasswordκ°€ λ“€μ–΄μ˜€λ©΄ UserCreateRequest의 객체의 κ°’μœΌλ‘œ λ§΅ν•‘λ˜κ³  public void registerUser(@RequestBody UserCreateRequest request)의 requestλŠ” μƒˆλ‘œμš΄ Userλ₯Ό λ§Œλ“œλŠ”λ° μ‚¬μš©λ©λ‹ˆλ‹€.
      • μ΄λ ‡κ²Œ μƒˆλ‘­κ²Œ λ§Œλ“œλŠ”λ° μ‚¬μš©λœ User κ°μ²΄λŠ” λ¦¬μŠ€νŠΈμ— μ €μž₯λ©λ‹ˆλ‹€.
        • registerUser ν•¨μˆ˜κ°€ μ˜ˆμ™Έ 없이 λλ‚˜κ²Œ λœλ‹€λ©΄ 200 OKλ₯Ό λ°˜ν™˜ν•˜κ²Œ λ©λ‹ˆλ‹€.