Home > Spring > ๐Ÿƒ[Spring] Controller, DTO, API ๋ช…์„ธ, `@GetMapping`, MIME, `@RequestParam`, ํผ ๋ฐ์ดํ„ฐ, `@RestController`

๐Ÿƒ[Spring] Controller, DTO, API ๋ช…์„ธ, `@GetMapping`, MIME, `@RequestParam`, ํผ ๋ฐ์ดํ„ฐ, `@RestController`
Spring Framework

๐Ÿƒ[Spring] Controller, DTO, API ๋ช…์„ธ, @GetMapping, MIME, @RequestParam, ํผ ๋ฐ์ดํ„ฐ, @RestController

1๏ธโƒฃ Controller.

  • Controller ๋Š” ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ํ•ต์‹ฌ ๊ตฌ์„ฑ ์š”์†Œ๋กœ, ์‚ฌ์šฉ์ž ์š”์ฒญ(URL ์š”์ฒญ)์„ ๋ฐ›์•„ ์ด๋ฅผ ์ฒ˜๋ฆฌํ•œ ํ›„ ์ ์ ˆํ•œ ์‘๋‹ต(๋ทฐ ๋˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜)์„ ์ œ๊ณตํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.
  • ์ด๋Š” ๋ณดํ†ต MVC(Model-View-Controller) ํŒจํ„ด์—์„œ Controller ์— ํ•ด๋‹นํ•˜๋ฉฐ, ํด๋ผ์ด์–ธํŠธ์˜ ์ž…๋ ฅ์„ ๋ฐ›์•„ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง๊ณผ ์ƒํ˜ธ์ž‘์šฉํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ๋ฆ„์„ ๋‹ด๋‹นํ•ฉ๋‹ˆ๋‹ค.

  • Controller๋Š” ์ฃผ๋กœ @Controller ๋˜๋Š” @RestController ์• ๋„ˆํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ์ •์˜ ๋ฉ๋‹ˆ๋‹ค.

1. @Controller ์™€ @RestController ์• ๋„ˆํ…Œ์ด์…˜์˜ ์ฐจ์ด์ .

  • 1. @Controller
    • ์ผ๋ฐ˜์ ์œผ๋กœ ๋ทฐ(View) ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
    • Thymeleaf์™€ ๊ฐ™์€ ํ…œํ”Œ๋ฆฟ ์—”์ง„์„ ์‚ฌ์šฉํ•  ๋•Œ HTML ํŒŒ์ผ์„ ๋ฐ˜ํ™˜ํ•  ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
    • ์˜ˆ: ์‚ฌ์šฉ์ž ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๊ณ  HTML ํŽ˜์ด์ง€๋ฅผ ๋ Œ๋”๋งํ•˜๋Š” ๊ฒฝ์šฐ.
  • 2. @RestController
    • ์ฃผ๋กœ JSON, XML ๋“ฑ ๋ฐ์ดํ„ฐ ํฌ๋งท์œผ๋กœ ์‘๋‹ต์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
    • @Controller ์™€ @ResponseBody ๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๊ณผ ๋™์ผํ•œ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.
    • RESTful API๋ฅผ ๊ตฌํ˜„ํ•  ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

2. ๊ธฐ๋ณธ ์˜ˆ์‹œ.

  • 1. @Controller ๋ฅผ ์ด์šฉํ•œ HTML ํŽ˜์ด์ง€ ๋ฐ˜ํ™˜
@Controller
public class HomeController {
    
    @GetMapping("/home")
    public String homePage(Model model) {
        model.attribute("message", "Welcome to the home page!");
        return "home"; // resource/templates/home.html๋กœ ์—ฐ๊ฒฐ๋จ(Thymeleaf์™€ ๊ฐ™์€ ํ…œํ”Œ๋ฆฟ ์—”์ง„ ์‚ฌ์šฉ)
    }
}
  • 2. @RestController ๋ฅผ ์ด์šฉํ•œ JSON ์‘๋‹ต ๋ฐ˜ํ™˜
@RestController
public class ApiController {
    
    @GetMapping("/api/data")
    public ResponseEntity<String> getData() {
        return new ResponseEntity<>("Hello, this is JSON data!", HttpStatus.OK);
    }
}

3. Controller์˜ ์ฃผ์š” ์—ญํ• .

  • ์š”์ฒญ ๋งคํ•‘
    • URL์„ ํŠน์ • ๋ฉ”์„œ๋“œ์— ๋งคํ•‘ํ•˜์—ฌ ์ ์ ˆํ•œ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.(@GetMapping, @PostMapping, @RequestMapping ๋“ฑ ์‚ฌ์šฉ)
  • ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง ํ˜ธ์ถœ
    • ์„œ๋น„์Šค ๊ณ„์ธต๊ณผ ์ƒํ˜ธ์ž‘์šฉํ•˜์—ฌ ํ•„์š”ํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
  • ๋ชจ๋ธ๊ณผ ๋ทฐ ์ฒ˜๋ฆฌ
    • ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•œ ํ›„ ๋ทฐ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๊ฑฐ๋‚˜ JSON ๋“ฑ์˜ ํ˜•์‹์œผ๋กœ ์‘๋‹ตํ•ฉ๋‹ˆ๋‹ค.

Spring Boot์˜ Controller๋Š” ์ด๋Ÿฌํ•œ ๋ฐฉ์‹์œผ๋กœ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ํ•ต์‹ฌ ํ๋ฆ„์„ ์ œ์–ดํ•˜๋ฉฐ, ์›น ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์ค‘์š”ํ•œ ์—ญํ• ์„ ๋‹ด๋‹นํ•ฉ๋‹ˆ๋‹ค.

2๏ธโƒฃ DTO(Data Transfer Object)

  • DTO(Data Transfer Object) ๋Š” ๊ณ„์ธต ๊ฐ„์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค.
  • ์ผ๋ฐ˜์ ์œผ๋กœ, ๋ฐฑ์—”๋“œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ๋Š” ์—ฌ๋Ÿฌ ๊ณ„์ธต(Controller, Service, Repository ๋“ฑ) ๊ฐ„์— ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ ๋ฐ›๊ฒŒ ๋˜๋Š”๋ฐ, ์ด๋•Œ ์‚ฌ์šฉ์ž๊ฐ€ ์ „๋‹ฌํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด๊ฑฐ๋‚˜ ๊ฐ€๊ณต๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด DTO๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

1. DTO์˜ ์ฃผ์š” ๋ชฉ์ .

  • 1. ๋ฐ์ดํ„ฐ ์บก์Šํ™”
    • DTO๋Š” ํŠน์ • ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ฅผ ์บก์Šํ™”ํ•˜์—ฌ ์™ธ๋ถ€์— ๋…ธ์ถœ๋ฉ๋‹ˆ๋‹ค.
    • ์ด๋กœ ์ธํ•ด ๋ถˆํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ ๋…ธ์ถœ์„ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • 2. ์„ฑ๋Šฅ ์ตœ์ ํ™”
    • ๋Œ€๊ทœ๋ชจ ๋ฐ์ดํ„ฐ๋ฅผ ํ•œ ๋ฒˆ์— ์ „์†กํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค๋Š”, ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋งŒ DTO๋ฅผ ํ†ตํ•ด ์ „์†กํ•˜๋Š” ๊ฒƒ์ด ๋„คํŠธ์›Œํฌ ํŠธ๋ž˜ํ”ฝ ๋ฐ ์„ฑ๋Šฅ์„ ์ตœ์ ํ™”ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค.
  • 3. ๊ณ„์ธต ๋ถ„๋ฆฌ
    • DTO๋Š” ์ฃผ๋กœ ์„œ๋น„์Šค ๊ณ„์ธต์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐ์ž‘ํ•˜๊ณ , ์ด๋ฅผ ์ปจํŠธ๋กค๋Ÿฌ ๊ณ„์ธต์œผ๋กœ ์ „๋‹ฌํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
    • ์ด๋ฅผ ํ†ตํ•ด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๊ฐ ๊ณ„์ธต์ด ๋ช…ํ™•ํ•˜๊ฒŒ ๋ถ„๋ฆฌ๋˜๋ฉฐ, ์œ ์ง€๋ณด์ˆ˜์„ฑ๊ณผ ํ™•์žฅ์„ฑ์ด ํ˜•์„ฑ๋ฉ๋‹ˆ๋‹ค.

2. DTO์™€ ์—”ํ‹ฐํ‹ฐ์˜ ์ฐจ์ด์ .

  • ์—”ํ‹ฐํ‹ฐ(Entity)
    • ์—”ํ‹ฐํ‹ฐ๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ…Œ์ด๋ธ”๊ณผ 1:1๋กœ ๋งคํ•‘๋˜๋ฉฐ, ์ฃผ๋กœ ์˜์†์„ฑ(Persistence)์„ ๋‹ด๋‹นํ•ฉ๋‹ˆ๋‹ค.
    • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€์˜ ์ƒํ˜ธ์ž‘์šฉ์„ ๊ด€๋ฆฌํ•˜๋Š” ์—ญํ• ์„ ํ•˜๋ฉฐ, ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ํฌํ•จํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ž…๋‹ˆ๋‹ค.
  • DTO
    • DTO๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๋ฐ ์ค‘์ ์„ ๋‘๋ฉฐ, ๋ณดํ†ต ์—”ํ‹ฐํ‹ฐ๋ฅผ ๋ณ€ํ™˜ํ•˜๊ฑฐ๋‚˜ ํŠน์ • ๋กœ์ง์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
    • ์—”ํ‹ฐํ‹ฐ์™€๋Š” ๋‹ค๋ฅด๊ฒŒ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์ง์ ‘์ ์œผ๋กœ ๋งคํ•‘๋˜์ง€ ์•Š์œผ๋ฉฐ, ์ „์†กํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋งŒ์„ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค.

3. ์‚ฌ์šฉ ์˜ˆ์‹œ.

  • ๋‹ค์Œ์€ ๊ฐ„๋‹จํ•œ User ์—”ํ‹ฐํ‹ฐ์™€ UserDTO์˜ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค.

  • User ์—”ํ‹ฐํ‹ฐ

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    private String name;
    private String email;
    
    // getters and setters
}
  • UserDTO
public class UserDTO {
    private String name;
    private String email;
    
    // ์ƒ์„ฑ์ž, getters, setters
}

์ด์ฒ˜๋Ÿผ ์—”ํ‹ฐํ‹ฐ๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์—ฐ๊ฒฐ๋œ ๊ฐ์ฒด์ด๊ณ , DTO๋Š” ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๊ฑฐ๋‚˜ ์š”์ฒญ์„ ๋ฐ›์•„์˜ฌ ๋•Œ ์‚ฌ์šฉ๋˜๋Š” ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค.

3๏ธโƒฃ API ๋ช…์„ธ(API Specification)

  • API ๋ช…์„ธ(API Specification) ๋Š” API(Application Programming Interface)์˜ ๋™์ž‘ ๋ฐฉ์‹, ์š”์ฒญ ๋ฐ ์‘๋‹ต ํ˜•์‹, ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋ฉ”์„œ๋“œ, ํŒŒ๋ผ๋ฏธํ„ฐ ๋“ฑ์„ ๋ช…ํ™•ํžˆ ์„ค๋ช…ํ•œ ๋ฌธ์„œ์ž…๋‹ˆ๋‹ค.
  • API ๋ช…์„ธ๋Š” API ์‚ฌ์šฉ์ž(์ฃผ๋กœ ๊ฐœ๋ฐœ์ž)๋“ค์ด API๋ฅผ ์ •ํ™•ํžˆ ์ดํ•ดํ•˜๊ณ  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” ์ค‘์š”ํ•œ ๋ฌธ์„œ์ž…๋‹ˆ๋‹ค.

1. API ๋ช…์„ธ์˜ ์ฃผ์š” ๋‚ด์šฉ.

  • 1. API ๊ฐœ์š”.
    • API๊ฐ€ ์–ด๋–ค ๋ชฉ์ ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€, ์ฃผ๋กœ ์–ด๋–ค ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๋Š”์ง€ ๊ฐ„๋‹จํ•œ ์„ค๋ช…์„ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค.
  • 2. ์—”๋“œํฌ์ธํŠธ(Endpoint)
    • API์˜ ์ ‘๊ทผ ๊ฒฝ๋กœ๋ฅผ ์˜๋ฏธํ•˜๋ฉฐ, ๊ฐ ์—”๋“œํฌ์ธํŠธ๋Š” ํŠน์ • ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
    • ์˜ˆ๋ฅผ ๋“ค์–ด GET /users๋Š” ์‚ฌ์šฉ์ž์˜ ๋ชฉ๋ก์„ ๊ฐ€์ ธ์˜ค๋Š” API ์—”๋“œํฌ์ธํŠธ์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • 3. HTTP ๋ฉ”์„œ๋“œ
    • ๊ฐ ์—”๋“œํฌ์ธํŠธ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” HTTP ๋ฉ”์„œ๋“œ(GET, POST, PUT, DELETE ๋“ฑ)๊ฐ€ ๋ช…์‹œ๋ฉ๋‹ˆ๋‹ค.
    • ์˜ˆ๋ฅผ ๋“ค์–ด, GET /users๋Š” ์‚ฌ์šฉ์ž ๋ชฉ๋ก์„ ์กฐํšŒํ•˜๊ณ , POST /users๋Š” ์ƒˆ ์‚ฌ์šฉ์ž๋ฅผ ์ƒ์„ฑํ•˜๋Š” API๋ฅผ ์˜๋ฏธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • 4. ์š”์ฒญ(Request) ํŒŒ๋ผ๋ฏธํ„ฐ
    • API ์š”์ฒญ ์‹œ ํฌํ•จํ•  ์ˆ˜ ์žˆ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ๋‚˜ ๋ฐ์ดํ„ฐ ํ˜•์‹์„ ๋ช…์‹œํ•ฉ๋‹ˆ๋‹ค.
    • ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ๊ฒฝ๋กœ ๋ณ€์ˆ˜(Path Variables), ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ(Query Parameters), ํ—ค๋”(Headers), ๋˜๋Š” ๋ฐ”๋””(Body)์— ํฌํ•จ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
      • ๊ฒฝ๋กœ ๋ณ€์ˆ˜ : GET /users/{id}์—์„œ {id}๊ฐ€ ๊ฒฝ๋กœ ๋ณ€์ˆ˜ ์ž…๋‹ˆ๋‹ค.
      • ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ : GET /users?status=active์—์„œ status๊ฐ€ ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ์ž…๋‹ˆ๋‹ค.
  • 5. ์š”์ฒญ ๋ณธ๋ฌธ(Request Body)
    • ์ฃผ๋กœ POST๋‚˜ PUT ๊ฐ™์€ ์š”์ฒญ์—์„œ ์‚ฌ์šฉ๋˜๋ฉฐ, JSON, XML ๋˜๋Š” ํผ ๋ฐ์ดํ„ฐ ๋“ฑ์œผ๋กœ ์ „์†กํ•  ๋ฐ์ดํ„ฐ๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
    • ์˜ˆ๋ฅผ ๋“ค์–ด, ์‚ฌ์šฉ์ž๋ฅผ ์ƒ์„ฑํ•˜๋Š” API์—์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ JSON ์š”์ฒญ์ด ๋ณธ๋ฌธ์ด ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
{
    "name": "Jhon Doe",
    "email": "jhon@example.com"
}
  • 6. ์‘๋‹ต(Response)
    • API ์š”์ฒญ์— ๋Œ€ํ•œ ์‘๋‹ต ํ˜•์‹์„ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค.
    • ์„ฑ๊ณต์ ์ธ ์š”์ฒญ์ด๋‚˜ ์‹คํŒจํ–ˆ์„ ๋•Œ์˜ ์‘๋‹ต ์ฝ”๋“œ(์˜ˆ: 200 OK, 404 Not Found)์™€ ํ•จ๊ป˜ ์‘๋‹ต ๋ณธ๋ฌธ(Response Body) ํ˜•์‹๋„ ๋ช…์‹œ๋ฉ๋‹ˆ๋‹ค.
{
    "id": 1,
    "name": "Jhon Doe",
    "email" "jhon@example.com"
}
  • 7. ์ƒํƒœ ์ฝ”๋“œ(Status Code)
    • ๊ฐ ์‘๋‹ต์˜ ๊ฒฐ๊ณผ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” HTTP ์ƒํƒœ ์ฝ”๋“œ๋ฅผ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค.
    • ์ผ๋ฐ˜์ ์ธ ์ƒํƒœ ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.
      • 200 OK : ์„ฑ๊ณต์ ์ธ ์š”์ฒญ.
      • 201 Created : ๋ฆฌ์†Œ์Šค ์ƒ์„ฑ ์„ฑ๊ณต.
      • 400 Bad Request : ์ž˜๋ชป๋œ ์š”์ฒญ.
      • 401 Unauthorized : ์ธ์ฆ ์‹คํŒจ.
      • 404 Not Found : ๋ฆฌ์†Œ์Šค๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Œ
      • 500 Internet Server Error : ์„œ๋ฒ„์—์„œ ๋ฐœ์ƒํ•œ ์˜ค๋ฅ˜
  • 8. ์˜ˆ์‹œ(Examples)
    • ์‹ค์ œ๋กœ ์š”์ฒญ์„ ๋ณด๋‚ด๋Š” ๋ฐฉ๋ฒ•๊ณผ ๊ทธ์— ๋Œ€ํ•œ ์‘๋‹ต์„ ๋ณด์—ฌ์ฃผ๋Š” ์˜ˆ์‹œ๊ฐ€ ํฌํ•จ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • ์˜ˆ๋ฅผ ๋“ค์–ด, curl ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•œ HTTP ์š”์ฒญ ๋ฐฉ๋ฒ• ๋“ฑ์„ ๋ช…์‹œํ•ฉ๋‹ˆ๋‹ค.
curl -X POST https://api.example.com/users \
  -H "Content-Type: application/json" \
  -d '{"name": "John Doe", "email": "john.doe@example.com"}'

2. API ๋ช…์„ธ์˜ ์ค‘์š”์„ฑ.

  • ๊ฐœ๋ฐœ์ž ๊ฐ„์˜ ์˜์‚ฌ์†Œํ†ต
    • API ๋ช…์„ธ๋Š” API๋ฅผ ์ œ๊ณตํ•˜๋Š” ์ธก๊ณผ ์‚ฌ์šฉํ•˜๋Š” ์ธก ๊ฐ„์˜ ์›ํ™œํ•œ ์˜์‚ฌ์†Œํ†ต์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.
    • ๋ช…์„ธ๊ฐ€ ์ •ํ™•ํ• ์ˆ˜๋ก API๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ํ˜ผ๋ž€์ด๋‚˜ ์˜ค๋ฅ˜๊ฐ€ ์ค„์–ด๋“ญ๋‹ˆ๋‹ค.
  • ์ผ๊ด€์„ฑ
    • ๋ชจ๋“  ์—”๋“œํฌ์ธํŠธ์™€ ์š”์ฒญ/์‘๋‹ต ํ˜•์‹์ด ์ผ๊ด€๋˜๊ฒŒ ์„ค๊ณ„๋˜๊ณ  ๊ตฌํ˜„๋  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ค๋‹ˆ๋‹ค.
  • ์œ ์ง€๋ณด์ˆ˜
    • ๋ช…์„ธ๋ฅผ ๊ธฐ์ค€์œผ๋กœ API๋ฅผ ๋ณ€๊ฒฝํ•˜๊ฑฐ๋‚˜ ํ™•์žฅํ•  ๋•Œ, ์ด๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ๊ธฐ์กด ์‚ฌ์šฉ์ž๋“ค์—๊ฒŒ ์˜ํ–ฅ์„ ์ตœ์†Œํ™”ํ•˜๋ฉด์„œ ์‹œ์Šคํ…œ์„ ๊ฐœ์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

3. API ๋ช…์„ธ ๋„๊ตฌ.

API ๋ช…์„ธ๋Š” ๋‹ค์–‘ํ•œ ํ˜•์‹์œผ๋กœ ์ž‘์„ฑ๋  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋Œ€ํ‘œ์ ์ธ ๋„๊ตฌ์™€ ํ‘œ์ค€์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • OpenAPI(Swagger)
    • ๊ฐ€์žฅ ๋„๋ฆฌ ์‚ฌ์šฉ๋˜๋Š” API ๋ช…์„ธ ํ‘œ์ค€์œผ๋กœ, Swagger UI๋ฅผ ํ†ตํ•ด ์‹œ๊ฐํ™”์™€ ํ…Œ์ŠคํŠธ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
  • Postman
    • API ํ…Œ์ŠคํŠธ ๋ฐ ๋ช…์„ธ ์ž‘์„ฑ์„ ์œ„ํ•œ ๋„๊ตฌ๋กœ, ๋‹ค์–‘ํ•œ HTTP ๋ฉ”์„œ๋“œ๋ฅผ ํ…Œ์ŠคํŠธํ•˜๊ณ  ๋ช…์„ธ๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • RAML
    • RESTful API Modeling LAnguage์˜ ์•ฝ์ž๋กœ, REST API ๋ช…์„ธ ์ž‘์„ฑ์„ ์œ„ํ•ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

API ๋ช…์„ธ๋Š” API๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋Š” ๋ชจ๋“  ๊ฐœ๋ฐœ์ž์—๊ฒŒ ํ•„์ˆ˜์ ์ธ ๊ฐ€์ด๋“œ ์—ญํ• ์„ ํ•˜๋ฉฐ, ์ •ํ™•ํ•˜๊ณ  ๋ช…ํ™•ํ•˜๊ฒŒ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค.

4๏ธโƒฃ @GetMapping ์• ๋„ˆํ…Œ์ด์…˜.

  • @GetMapping ์• ๋„ˆํ…Œ์ด์…˜ ์€ Spring Framework์—์„œ HTTP GET ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ์• ๋„ˆํ…Œ์ด์…˜์ž…๋‹ˆ๋‹ค.
  • ์ฃผ๋กœ Spring MVC์—์„œ ์ปจํŠธ๋กค๋Ÿฌ์˜ ๋ฉ”์„œ๋“œ์— ๋ถ™์—ฌ์„œ ํŠน์ • URL๋กœ ๋“ค์–ด์˜ค๋Š” GET ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ๋งคํ•‘ํ•ฉ๋‹ˆ๋‹ค.

1. @GetMapping์˜ ๊ธฐ๋ณธ ๋™์ž‘.

  • Spring Boot ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ๋Š” ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„๋กœ GET ์š”์ฒญ์„ ๋ณด๋‚ผ ๋•Œ ํ•ด๋‹น ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•  ์ปจํŠธ๋กค๋Ÿฌ ๋ฉ”์„œ๋“œ๋ฅผ ์ง€์ •ํ•˜๊ธฐ ์œ„ํ•ด @GetMapping์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
  • ์ด ์• ๋„ˆํ…Œ์ด์…˜์€ ๋‚ด๋ถ€์ ์œผ๋กœ @RequestMapping(method = RequestMethod.GET)๊ณผ ๋™์ผํ•œ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.

2. ์‚ฌ์šฉ ์˜ˆ์‹œ.

๋‹ค์Œ์€ @GetMapping์„ ์‚ฌ์šฉํ•˜์—ฌ ํŠน์ • URL์—์„œ GET ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฐ„๋‹จํ•œ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค.

@RestController
public class UserController {
    
    // '/users' ๊ฒฝ๋กœ์— ๋Œ€ํ•œ GET ์š”์ฒญ ์ฒ˜๋ฆฌ
    @GetMapping("/users")
    public List<User> getAllUsers() {
        // ์„œ๋น„์Šค ๊ณ„์ธต์—์„œ ์‚ฌ์šฉ์ž ๋ชฉ๋ก์„ ๊ฐ€์ ธ์™€ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ
        return userService.getAllUsers();
    }
}

์œ„์˜ ์ฝ”๋“œ์—์„œ

  • @GetMapping("/users")๋Š” /users ๊ฒฝ๋กœ๋กœ ๋“ค์–ด์˜ค๋Š” GET ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
  • ๋ฉ”์„œ๋“œ getAllUsers()๋Š” GET ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋ฉฐ, ๋ณดํ†ต ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
    • ์ด๋•Œ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ JSON ํ˜•์‹์œผ๋กœ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค(Spring Boot์—์„œ๋Š” @RestController ์‚ฌ์šฉ ์‹œ).

3. ์ฃผ์š” ํŠน์ง•.

  • 1. HTTP GET ์š”์ฒญ ์ฒ˜๋ฆฌ.
    • @GetMapping์€ GET ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐ๋งŒ ์‚ฌ์šฉ๋˜๋ฉฐ, ๋‹ค๋ฅธ HTTP ๋ฉ”์„œ๋“œ(POST, PUT, DELETE ๋“ฑ) ์š”์ฒญ์€ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • 2. ๊ฒฝ๋กœ ์ง€์ •.
    • @GetMapping("/path") ํ˜•์‹์œผ๋กœ ๋งคํ•‘ ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • ๊ฒฝ๋กœ์— ๊ณ ์ •๋œ URL๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๊ฒฝ๋กœ ๋ณ€์ˆ˜, ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ ๋“ฑ๋„ ํฌํ•จ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • 3. ๊ฒฝ๋กœ ๋ณ€์ˆ˜ ์‚ฌ์šฉ.
    • ๊ฒฝ๋กœ ๋ณ€์ˆ˜(Path Variable)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋™์ ์œผ๋กœ URL์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
@GetMapping("/users/{id}")
public User getUserById(@PathVariable Long id) {
    return userService.getUserById(id);
}
  • ์œ„ ์ฝ”๋“œ์—์„œ @PathVariable์„ ์‚ฌ์šฉํ•˜์—ฌ URL ๊ฒฝ๋กœ์— ํฌํ•จ๋œ id ๊ฐ’์„ ๋ฉ”์„œ๋“œ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ „๋‹ฌ๋ฐ›์Šต๋‹ˆ๋‹ค.
    • ์˜ˆ๋ฅผ ๋“ค์–ด, /users/1์ด ์š”์ฒญ๋˜๋ฉด id๊ฐ€ 1๋กœ ์„ค์ •๋ฉ๋‹ˆ๋‹ค.
  • 4. ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ ์‚ฌ์šฉ.
    • ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ(Query Parameter)๋ฅผ ์ฒ˜๋ฆฌํ•  ๋•Œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
@GetMapping("/users")
public List<User> getUsersByStatus(@RequestParam String status) {
    return userService.getUsersByStatus(status);
}
  • ์œ„ ์˜ˆ์‹œ์—์„œ @RequestParam์„ ์‚ฌ์šฉํ•˜์—ฌ ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ status๋ฅผ ๋ฉ”์„œ๋“œ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ „๋‹ฌ๋ฐ›์Šต๋‹ˆ๋‹ค.
    • ์˜ˆ๋ฅผ ๋“ค์–ด, /users?status=active๋กœ ์š”์ฒญํ•˜๋ฉด status ๊ฐ’์€ โ€œactiveโ€๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

4. @GetMapping ๊ณผ ๊ด€๋ จ๋œ ์ฃผ์š” ์˜ต์…˜.

  • produces
    • ์‘๋‹ต์œผ๋กœ ์ œ๊ณตํ•˜๋Š” ๋ฐ์ดํ„ฐ์˜ MIME ํƒ€์ž…์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
      • ์˜ˆ๋ฅผ ๋“ค์–ด, JSON, XML ํ˜•์‹์˜ ์‘๋‹ต์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
@GetMapping(value = "/users", produces = "application/json")
public List<User> getAllUsers() {
    return userService.getAllUsers();
}
  • params
    • ํŠน์ • ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ์—๋งŒ ํ•ด๋‹น ๋ฉ”์„œ๋“œ๊ฐ€ ๋งคํ•‘๋˜๋„๋ก ์ œํ•œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
@GetMapping(value = "/users", params = "active")
public List<User> getActiveUsers() {
    return userService.getActiveUsers();
}
  • ์œ„ ์˜ˆ์‹œ์—์„œ active๋ผ๋Š” ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์žˆ์–ด์•ผ๋งŒ ์ด ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

5. @GetMapping VS @RequestMapping

  • @RequestMapping ์„ ์‚ฌ์šฉํ•˜๋ฉด HTTP ๋ฉ”์„œ๋“œ๋ฅผ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
// @RequestMapping
@RequestMapping(value = "/users", method = RequestMethod.GET)
public List<User> getAllUsers() {
    return userService.getAllUsers();
}

// @GetMapping
@GetMapping("/users")
public List<User> getAllUsers() {
    return userService.getAllUsers();
}
  • @GetMapping์€ GET ์š”์ฒญ ์ „์šฉ์œผ๋กœ ๋ณด๋‹ค ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

๋ฆฌํŒฉํ† ๋ง ํฌ์ธํŠธ

  • 1. @RequestMapping ์€ ๋‹ค์–‘ํ•œ HTTP ๋ฉ”์„œ๋“œ(GET, POST, PUT, DELETE ๋“ฑ)๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค๊ณ„๋œ ๋ฒ”์šฉ ์• ๋„ˆํ…Œ์ด์…˜์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ GET ์š”์ฒญ๋งŒ์„ ์ฒ˜๋ฆฌํ•  ๋•Œ๋Š” @GetMapping์„ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • 2. method = RequestMethod.GET ๋Œ€์‹  @GetMapping์„ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ ๋” ์ง๊ด€์ ์ด๊ณ  ๊ฐ„๋‹จํ•˜๊ฒŒ GET ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • 3. @GetMapping์€ ๊ธฐ๋ณธ์ ์œผ๋กœ GET ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋ฏ€๋กœ value ์†์„ฑ์— ๊ฒฝ๋กœ๋งŒ ๋ช…์‹œํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

6. ์š”์•ฝ.

  • ์ฃผ์š” ์—ญํ• 
    • HTTP GET ์š”์ฒญ์„ ํŠน์ • ๋ฉ”์„œ๋“œ์— ๋งคํ•‘ํ•˜์—ฌ ์š”์ฒญ ์ฒ˜๋ฆฌ.
  • ๊ฐ„๊ฒฐํ•œ ๋ฌธ๋ฒ•
    • @RequestMapping์˜ GET ์š”์ฒญ ์ฒ˜๋ฆฌ๋ฅผ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ๋Œ€์ฒด.
  • ์ฃผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๊ณณ
    • ๋ฆฌ์†Œ์Šค ์กฐํšŒ, ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ ๋“ฑ์˜ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•  ๋•Œ ์‚ฌ์šฉ.

5๏ธโƒฃ MIME(Mutipurpose Internet Mail Extensions) ํƒ€์ž….

  • MIME(Mutipurpose Internet Mail Extensions) ํƒ€์ž… ์€ ์ธํ„ฐ๋„ท์—์„œ ์ „์†ก๋˜๋Š” ๋ฐ์ดํ„ฐ์˜ ํ˜•์‹์„ ๋‚˜ํƒ€๋‚ด๋Š” ํ‘œ์ค€์ž…๋‹ˆ๋‹ค.
  • MIME ํƒ€์ž…์€ ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„ ๊ฐ„์— ์ฃผ๊ณ ๋ฐ›๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์–ด๋–ค ํ˜•์‹์ธ์ง€ ์ •์˜ํ•˜์—ฌ, ์ด๋ฅผ ํ†ตํ•ด ํด๋ผ์ด์–ธํŠธ(๋ธŒ๋ผ์šฐ์ € ๋“ฑ)๋Š” ๋ฐ์ดํ„ฐ์˜ ์ฒ˜๋ฆฌ๋ฅผ ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ—์ง€ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

1. MIME ํƒ€์ž…์˜ ๊ตฌ์„ฑ.

MIME ํƒ€์ž…์€ ํฌ๊ฒŒ ๋‘ ๋ถ€๋ถ„์œผ๋กœ ๋‚˜๋‰ฉ๋‹ˆ๋‹ค.

  • ํƒ€์ž…(Type)
    • ๋ฐ์ดํ„ฐ์˜ ์ผ๋ฐ˜์ ์ธ ๋ฒ”์ฃผ๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.(์˜ˆ: text, image, application ๋“ฑ).
  • ์„œ๋ธŒํƒ€์ž…(Subtype)
    • ๋ฐ์ดํ„ฐ์˜ ๊ตฌ์ฒด์ ์ธ ํ˜•์‹์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.(์˜ˆ: html, plain, json, jpeg ๋“ฑ).

ํ˜•์‹์€ /(์Šฌ๋ž˜์‹œ)๋กœ ๊ตฌ๋ถ„๋˜๋ฉฐ, ์˜ˆ๋ฅผ ๋“ค์–ด text/html์€ HTML ๋ฌธ์„œ๋ผ๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.

2. MIME ํƒ€์ž…์˜ ์˜ˆ์‹œ.

  • 1. ํ…์ŠคํŠธ ํ˜•์‹.
    • text/plain
      • ์ผ๋ฐ˜ ํ…์ŠคํŠธ(ASCII ๋˜๋Š” UTF-8๋กœ ์ธ์ฝ”๋”ฉ๋œ ํ…์ŠคํŠธ).
    • text/html
      • HTML ๋ฌธ์„œ.
    • text/css
      • CSS ํŒŒ์ผ.
    • text/javascript
      • JavaScript ํŒŒ์ผ.
  • 2. ์ด๋ฏธ์ง€ ํ˜•์‹.
    • image/jpeg
      • JPEG ์ด๋ฏธ์ง€.
    • image/png
      • PNG ์ด๋ฏธ์ง€.
    • image/gif
      • GIF ์ด๋ฏธ์ง€.
  • 3. ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ ํ˜•์‹.
    • application/json
      • JSON ํ˜•์‹์˜ ๋ฐ์ดํ„ฐ.
    • application/xml
      • XML ๋ฌธ์„œ.
    • appliccation/pdf
      • PDF ํŒŒ์ผ.
    • application/octet-stream
      • ์ผ๋ฐ˜์ ์ธ ๋ฐ”์ด๋„ˆ๋ฆฌ ๋ฐ์ดํ„ฐ.
      • ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ ์‹œ ์ฃผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  • 4. ๋ฉ€ํ‹ฐ๋ฏธ๋””์–ด ํ˜•์‹.
    • audio/mpeg
      • MP3 ์˜ค๋””์˜ค ํŒŒ์ผ.
    • video/mp4
      • MP4 ๋น„๋””์˜ค ํŒŒ์ผ.
  • 5. ๊ธฐํƒ€ ํ˜•์‹.
    • multipart/form-data
      • ์ฃผ๋กœ ํŒŒ์ผ ์—…๋กœ๋“œ๋‚˜ ํผ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋Š” ํ˜•์‹.

3. MIME ํƒ€์ž…์˜ ์—ญํ• .

MIME ํƒ€์ž…์€ ์›น ๋ธŒ๋ผ์šฐ์ €์™€ ๊ฐ™์€ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ๋ฐ›์€ ๋ฐ์ดํ„ฐ์˜ ํ˜•์‹์„ ์ดํ•ดํ•˜๊ณ  ์ ์ ˆํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐ ์ค‘์š”ํ•œ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด

  • text/html
    • ๋ธŒ๋ผ์šฐ์ €๋Š” ์ด MIME ํƒ€์ž…์„ ๋ฐ›์œผ๋ฉด ์ด๋ฅผ HTML ๋ฌธ์„œ๋กœ ํ•ด์„ํ•˜๊ณ  ํ™”๋ฉด์— ๋ Œ๋”๋งํ•ฉ๋‹ˆ๋‹ค.
  • application/json
    • ๋ธŒ๋ผ์šฐ์ €๋Š” ์ด MIME ํƒ€์ž…์„ ๋ฐ›์œผ๋ฉด ์ด๋ฅผ JSON ํ˜•์‹์˜ ๋ฐ์ดํ„ฐ๋กœ ์ธ์‹ํ•˜๊ณ , ๋ณดํ†ต ๊ฐœ๋ฐœ์ž ๋„๊ตฌ์—์„œ ๊ตฌ์กฐํ™”๋œ ํ˜•์‹์œผ๋กœ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.
  • application/octet=stream
    • ๋ฐ”์ด๋„ˆ๋ฆฌ ํŒŒ์ผ(์˜ˆ: ํ”„๋กœ๊ทธ๋žจ ํŒŒ์ผ, ์••์ถ• ํŒŒ์ผ)์„ ๋‹ค์šด๋กœ๋“œํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋ฉฐ, ํด๋ผ์ด์–ธํŠธ๋Š” ์ด ๋ฐ์ดํ„ฐ๋ฅผ ํŒŒ์ผ๋กœ ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

4. MIME ํƒ€์ž… ์ง€์ •.

  • ์„œ๋ฒ„๊ฐ€ ํด๋ผ์ด์–ธํŠธ์— ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ผ ๋•Œ MIME ํƒ€์ž…์„ ์ง€์ •ํ•˜๋Š”๋ฐ, HTTP ์‘๋‹ต ํ—ค๋”์˜ Content-Type ํ•„๋“œ๋ฅผ ํ†ตํ•ด MIME ํƒ€์ž…์ด ์ •์˜๋ฉ๋‹ˆ๋‹ค.

์˜ˆ์‹œ: Content-Type ํ—ค๋”

HTTP/1.1 200OK
Content=Type: application/json

{
    "name": "John",
    "age": 30
}
  • ์œ„ ์˜ˆ์‹œ์—์„œ๋Š” ์„œ๋ฒ„๊ฐ€ JSON ํ˜•์‹์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์‘๋‹ตํ•˜๋ฉฐ, Content-Type: application/json ์„ ํ†ตํ•ด MIME ํƒ€์ž…์„ ๋ช…์‹œํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

5. Spring์—์„œ์˜ MIME ํƒ€์ž… ์ง€์ •.

  • Spring์—์„œ๋Š” @GetMapping ๋˜๋Š” @PostMaping ๊ณผ ๊ฐ™์€ ์• ๋„ˆํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•  ๋•Œ produces ์†์„ฑ์„ ํ†ตํ•ด MIME ํƒ€์ž…์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ์‹œ : JSON ํ˜•์‹์˜ ์‘๋‹ต ์ง€์ •.

@GetMapping(value = "/users", produces = "application/json")
public List<User> getAllUsers() {
    return userService.getAllUsers();
}
  • ์œ„ ์ฝ”๋“œ์—์„œ๋Š” /users๋กœ ๋“ค์–ด์˜ค๋Š” GET ์š”์ฒญ์— ๋Œ€ํ•ด JSON ํ˜•์‹ (application/json)์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์‘๋‹ตํ•˜๋„๋ก ์ง€์ •ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

6. ์š”์•ฝ.

  • MIME ํƒ€์ž… ์€ ์ธํ„ฐ๋„ท์—์„œ ์ „์†ก๋˜๋Š” ๋ฐ์ดํ„ฐ์˜ ํ˜•์‹์„ ๋‚˜ํƒ€๋‚ด๋Š” ํ‘œ์ค€.
  • ์ฃผ๋กœ ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„ ๊ฐ„์— ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ ๋ฐ›์„ ๋•Œ ๋ฐ์ดํ„ฐ ํ˜•์‹์„ ์ง€์ •ํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ด๋ฅผ ์ ์ ˆํžˆ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์คŒ
  • ํ˜•์‹ ์€ ํƒ€์ž…/์„œ๋ธŒํƒ€์ž… ์œผ๋กœ ๊ตฌ์„ฑ๋˜๋ฉฐ, ์˜ˆ๋ฅผ ๋“ค์–ด text/html ์€ HTML ๋ฌธ์„œ๋ฅผ ๋‚˜ํƒ€๋ƒ„.
  • Spring๊ณผ ๊ฐ™์€ ํ”„๋ ˆ์ž„์›Œํฌ์—์„œ๋„ ์‘๋‹ต ํ˜•์‹์— ๋”ฐ๋ผ ์ ์ ˆํ•œ MIME ํƒ€์ž…์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Œ.

6๏ธโƒฃ @RequestParam ์• ๋„ˆํ…Œ์ด์…˜.

  • @RequestParam ์• ๋„ˆํ…Œ์ด์…˜ ์€ Spring MVC์—์„œ HTTP ์š”์ฒญ์˜ ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ, ํผ ๋ฐ์ดํ„ฐ ๋˜๋Š” URL์— ํฌํ•จ๋œ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ฉ”์„œ๋“œ ํŒŒ๋ผ๋ฏธํ„ฐ์— ๋ฐ”์ธ๋”ฉํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  • ์ฃผ๋กœ GET ์š”์ฒญ์—์„œ ๋ญ๋ฆฌ ์ŠคํŠธ๋ง์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜, POST ์š”์ฒญ์—์„œ ํผ ๋ฐ์ดํ„ฐ๋กœ ์ „๋‹ฌ๋œ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

1. ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ•.

  • @RequestParam ์„ ์‚ฌ์šฉํ•˜๋ฉด ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์š”์ฒญํ•œ URL์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์„ ๋ฉ”์„œ๋“œ์˜ ์ธ์ž๋กœ ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • ์˜ˆ๋ฅผ ๋“ค์–ด, ์‚ฌ์šฉ์ž๊ฐ€ GET /users?name=Jhon ๊ณผ ๊ฐ™์ด ์š”์ฒญํ•˜๋ฉด name ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์„ ๋ฉ”์„œ๋“œ์—์„œ ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ์‹œ 1: ๋‹จ์ผ ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ ๋ฐ›๊ธฐ.

@GetMapping("/users")
public String getUserByName(@RequestParam String name) {
    return "Requested user: " + name;
}

์œ„ ์ฝ”๋“œ์—์„œ

  • @RequestParam String name
    • ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ name์˜ ๊ฐ’์„ ๋ฐ›์•„์„œ ๋ฉ”์„œ๋“œ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.
      • ์˜ˆ๋ฅผ ๋“ค์–ด /users?name=Jhon์œผ๋กœ ์š”์ฒญํ•˜๋ฉด name์— "John" ๊ฐ’์ด ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค.

์˜ˆ์‹œ 2: ์—ฌ๋Ÿฌ ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ ๋ฐ›๊ธฐ.

@GetMapping("/users")
public String getUser(@RequestParam String name, @RequestParam int age) {
    return "User: " + name + ", Age: " + age;
}
  • ์ด ์ฝ”๋“œ๋Š” ๋‘ ๊ฐธ์˜ ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ(name๊ณผ age)๋ฅผ ๋ฐ›์Šต๋‹ˆ๋‹ค.
  • ํด๋ผ์ด์–ธํŠธ๊ฐ€ /users?name=Jhon&age=25๋กœ ์š”์ฒญํ•˜๋ฉด name์€ "Jhon", age๋Š” 25๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

3. ์„ ํƒ์  ํŒŒ๋ผ๋ฏธํ„ฐ์™€ ๊ธฐ๋ณธ๊ฐ’.

  • @RequestParam์˜ ์†์„ฑ์„ ์ด์šฉํ•˜๋ฉด ์„ ํƒ์ ์ธ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์—†์„ ๊ฒฝ์šฐ ๊ธฐ๋ณธ๊ฐ’์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ์‹œ 3: ๊ธฐ๋ณธ๊ฐ’ ์„ค์ •.

@GetMapping("/users")
pulbic String getUser(@RequestParam(defaultValue = "Unknown") String name) {
    return "Requested user: " + name;
}
  • ์œ„ ์ฝ”๋“œ์—์„œ ํด๋ผ์ด์–ธํŠธ๊ฐ€ /users๋กœ๋งŒ ์š”์ฒญ์„ ๋ณด๋‚ด๋ฉด, name์˜ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ "Unknown"์ด ์„ค์ •๋ฉ๋‹ˆ๋‹ค.
  • /users?name=John์œผ๋กœ ์š”์ฒญํ•˜๋ฉด name์€ "John" ๊ฐ’์ด ๋ฉ๋‹ˆ๋‹ค.

์˜ˆ์‹œ 4: ํ•„์ˆ˜ ์—ฌ๋ถ€ ์„ค์ •.

  • required ์†์„ฑ์„ ์‚ฌ์šฉํ•˜๋ฉด ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ํ•„์ˆ˜์ธ์ง€ ์„ ํƒ์ ์ธ์ง€ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • ๊ธฐ๋ณธ์ ์œผ๋กœ @RequestParam์€ ํ•„์ˆ˜์ž…๋‹ˆ๋‹ค.
    • ํ•˜์ง€๋งŒ, required = false๋กœ ์„ค์ •ํ•˜๋ฉด ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์—†์–ด๋„ ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
      @GetMapping("/users")
      public String getUser(@RequestParam(required = false) String name) {
        return "Requested user: " + (name != null ? name : "Guest");
      }
      
  • ์ด ๊ฒฝ์šฐ, ํด๋ผ์ด์–ธํŠธ๊ฐ€ name ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š๊ณ  ์š”์ฒญํ•  ๊ฒฝ์šฐ name ๊ฐ’์€ null์ด ๋˜๊ณ , Guest๋กœ ๋Œ€์ฒด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

4. ๋ฐฐ์—ด ๋˜๋Š” ๋ฆฌ์ŠคํŠธ ํŒŒ๋ผ๋ฏธํ„ฐ ์ฒ˜๋ฆฌ.

  • @RequestParam์„ ์‚ฌ์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ ๊ฐ’์„ ํ•œ ๋ฒˆ์— ๋ฐ›์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ์‹œ 5: ๋ฐฐ์—ด๋กœ ํŒŒ๋ผ๋ฏธํ„ฐ ๋ฐ›๊ธฐ.

@GetMapping("/users")
public String getUsersByNames(@RequestParam List<String> names) {
    return "Requested users: " + String.join(", ", names);
}
  • ์œ„ ์ฝ”๋“œ์—์„œ /users?names=John&names=Jane๊ณผ ๊ฐ™์ด ์š”์ฒญํ•˜๋ฉด names๋Š” ["John", "Jane"] ๋ฆฌ์ŠคํŠธ๋กœ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค.

5. ์š”์•ฝ.

  • @RequestParam์€ URL ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ๋‚˜ ํผ ๋ฐ์ดํ„ฐ๋ฅผ ์ปจํŠธ๋กค๋Ÿฌ ๋ฉ”์„œ๋“œ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋งคํ•‘ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  • ํ•„์ˆ˜ ์—ฌ๋ถ€(required), ๊ธฐ๋ณธ๊ฐ’(defaultValue) ์„ค์ •์„ ํ†ตํ•ด ์œ ์—ฐํ•˜๊ฒŒ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์—ฌ๋Ÿฌ ํŒŒ๋ผ๋ฏธํ„ฐ๋‚˜ ๋ฐฐ์—ด/๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋„ ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

7๏ธโƒฃ ํผ ๋ฐ์ดํ„ฐ(Form Data).

  • ํผ ๋ฐ์ดํ„ฐ(Form Data) ๋Š” ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์‚ฌ์šฉ์ž๊ฐ€ ์›น ๋ธŒ๋ผ์šฐ์ €๋ฅผ ํ†ตํ•ด ์„œ๋ฒ„๋กœ ์ œ์ถœํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค.
  • ์ฃผ๋กœ HTML <form> ์š”์†Œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‚ฌ์šฉ์ž ์ž…๋ ฅ์„ ์„œ๋ฒ„๋กœ ์ „์†กํ•˜๋ฉฐ, ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ํ…์ŠคํŠธ, ํŒŒ์ผ, ์„ ํƒํ•œ ์˜ต์…˜ ๋“ฑ์˜ ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

1. ํผ ๋ฐ์ดํ„ฐ์˜ ๊ตฌ์„ฑ.

  • ํผ ๋ฐ์ดํ„ฐ๋Š” ์—ฌ๋Ÿฌ ์ข…๋ฅ˜์˜ ์ž…๋ ฅ ํ•„๋“œ๋ฅผ ํ†ตํ•ด ์ƒ์„ฑ๋˜๋ฉฐ, ์ „์†ก ๋ฐฉ์‹์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ฒŒ ์ฒ˜๋ฆฌ๋ฉ๋‹ˆ๋‹ค.
  • HTML <form> ํƒœ๊ทธ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ž…๋ ฅ ํผ์„ ๋งŒ๋“ค๊ณ , ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์„œ๋ฒ„๋กœ ์ „์†กํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

HTML ํผ์˜ ์˜ˆ์‹œ.

<form action="/submit" method="post">
    <label for="name">Name:</label>
    <input type="text" id="name" name="name">
    
    <label for="age">Age:</label>
    <input type="number" id="age" name="age">
    
    <label for="file">Profile Picture:</label>
    <input type="file" id="file" name="profilePicture">
    
    <button type="submit">Submit</button>
</form>
  • ์ด ํผ์—์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๋ฅผ ์„œ๋ฒ„๋กœ ์ „์†กํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • Name : ํ…์ŠคํŠธ ์ž…๋ ฅ.
    • Age : ์ˆซ์ž ์ž…๋ ฅ.
    • Profile Picture : ํŒŒ์ผ ์—…๋กœ๋“œ.

2. ํผ ๋ฐ์ดํ„ฐ์˜ ์ „์†ก ๋ฐฉ์‹.

1. GET ๋ฉ”์„œ๋“œ

  • ํผ์ด GET ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•  ๋•Œ๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ URL์˜ ์ฟผ๋ฆฌ ์ŠคํŠธ๋ง(Query String)์œผ๋กœ ์ „๋‹ฌ ๋ฉ๋‹ˆ๋‹ค.
  • URL์€ ๋ณดํ†ต ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ˜•์‹์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค.
    • example.com/submit?name=Jhon&age=30
  • ๋ฐ์ดํ„ฐ๋Š” URL์— ํฌํ•จ๋˜๊ธฐ ๋–„๋ฌธ์— ๋ณด์•ˆ์— ์ทจ์•ฝํ•˜๊ณ , ์ „์†ก ๊ฐ€๋Šฅํ•œ ๋ฐ์ดํ„ฐ์˜ ์–‘์ด ์ œํ•œ์ ์ž…๋‹ˆ๋‹ค.
    • ๋”ฐ๋ผ์„œ ๊ฒ€์ƒ‰ ์ฟผ๋ฆฌ๋‚˜ ํ•„ํ„ฐ์™€ ๊ฐ™์€ ๊ฐ„๋‹จํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•  ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
<form action="/submit" method="get">
    <!-- ์ž…๋ ฅ ํ•„๋“œ -->
    <button type="submit">Submit</button>
</form>

2. POST ๋ฉ”์„œ๋“œ

  • POST ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐ์ดํ„ฐ๊ฐ€ HTTP ์š”์ฒญ ๋ณธ๋ฌธ(Body)์— ํฌํ•จ๋˜์–ด ์ „์†ก๋ฉ๋‹ˆ๋‹ค.
    • ์ด ๋ฐฉ์‹์€ URL์— ๋ฐ์ดํ„ฐ๋ฅผ ๋…ธ์ถœํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๋” ์•ˆ์ „ํ•˜๋ฉฐ, ๋Œ€์šฉ๋Ÿ‰ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ํŒŒ์ผ ์—…๋กœ๋“œ๋‚˜ ๋ฏผ๊ฐํ•œ ์ •๋ณด๋ฅผ ์ „์†กํ•  ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
<form action="/submit" method="post">
    <!-- ์ž…๋ ฅ ํ•„๋“œ -->
    <button type="submit">Submit</button>
</form>

3. ์ „์†ก๋˜๋Š” ๋ฐ์ดํ„ฐ ํ˜•์‹.

ํผ ๋ฐ์ดํ„ฐ๊ฐ€ ์ „์†ก๋  ๋•Œ, ๋ธŒ๋ผ์šฐ์ €๋Š” Content-Type ํ—ค๋”๋ฅผ ํ†ตํ•ด ์„œ๋ฒ„์— ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ธ์ฝ”๋”ฉํ–ˆ๋Š”์ง€ ์•Œ๋ ค์ค๋‹ˆ๋‹ค.

ํผ ๋ฐ์ดํ„ฐ์˜ ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹์— ๋”ฐ๋ผ ์„œ๋ฒ„์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ์‹์ด ๋‹ฌ๋ผ์ง‘๋‹ˆ๋‹ค.

  • 1. application/x-www.form-urlencoded
    • ๊ธฐ๋ณธ ํผ ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.
    • ํผ ๋ฐ์ดํ„ฐ๋ฅผ URL ์ธ์ฝ”๋”ฉํ•˜์—ฌ ํ‚ค-๊ฐ’ ์Œ์œผ๋กœ ์ „์†กํ•ฉ๋‹ˆ๋‹ค.
    • ์˜ˆ๋ฅผ ๋“ค์–ด, name=John&age=30์ฒ˜๋Ÿผ ํ‚ค์™€ ๊ฐ’์„ &๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์„œ๋ฒ„๋กœ ์ „์†กํ•ฉ๋‹ˆ๋‹ค.
    • POST ์š”์ฒญ์˜ ๋ณธ๋ฌธ์— ์ด ํ˜•์‹์œผ๋กœ ๋ฐ์ดํ„ฐ๊ฐ€ ํฌํ•จ๋ฉ๋‹ˆ๋‹ค.
    • ์˜ˆ์‹œ
        name=Jhon&age=30
      
  • 2. multipart/form-data
    • ํŒŒ์ผ ์—…๋กœ๋“œ๊ฐ€ ํฌํ•จ๋œ ํผ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋Š” ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.
    • ๋ฐ์ดํ„ฐ์™€ ํŒŒ์ผ์„ ์—ฌ๋Ÿฌ ๋ถ€๋ถ„์œผ๋กœ ๋‚˜๋ˆ„์–ด ์„œ๋ฒ„๋กœ ์ „์†กํ•ฉ๋‹ˆ๋‹ค.
    • ํŒŒ์ผ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ํ…์ŠคํŠธ ํ•„๋“œ๋„ ํ•จ๊ป˜ ์ „์†กํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • ์˜ˆ์‹œ: ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ํŒŒ์ผ๊ณผ ๋ฐ์ดํ„ฐ๋ฅผ ํ•จ๊ป˜ ์ „์†กํ•ฉ๋‹ˆ๋‹ค.
      ```plaintext
      โ€“boundary
      Content-Disposition: form-data; name=โ€nameโ€

    John
    โ€“boundary
    Content-Disposition: form-data; name=โ€profilePictureโ€; filename=โ€profile.jpgโ€
    Content-Type: image/jpeg

    [๋ฐ”์ด๋„ˆ๋ฆฌ ํŒŒ์ผ ๋ฐ์ดํ„ฐ]
    โ€“boundaryโ€“
    ```

  • 3. text/plain
    • ๋ฐ์ดํ„ฐ๋ฅผ ๋‹จ์ˆœํ•œ ํ…์ŠคํŠธ ํ˜•์‹์œผ๋กœ ์ „์†กํ•ฉ๋‹ˆ๋‹ค.
    • ์ฃผ๋กœ API์™€์˜ ๊ฐ„๋‹จํ•œ ํ…์ŠคํŠธ ์ „์†ก์— ์‚ฌ์šฉ๋˜๋ฉฐ, ํผ ๋ฐ์ดํ„ฐ๋กœ๋Š” ์ž˜ ์‚ฌ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

4. ์„œ๋ฒ„์—์„œ ํผ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ.

  • ์„œ๋ฒ„๋Š” ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ „์†กํ•œ ํผ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
    • ์˜ˆ๋ฅผ ๋“ค์–ด, Spring Boot์—์„œ๋Š” @RequestParam ๋˜๋Š” @ModelAttribute ๋“ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ํผ ๋ฐ์ดํ„ฐ๋ฅผ ์‰ฝ๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ์‹œ: Spring์—์„œ ํผ ๋ฐ์ดํ„ฐ ๋ฐ›๊ธฐ.

@PostMapping("/submit")
public String handleForm(@RequestParam String name, @RequestParam int age) {
    // ํผ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ
    return "Name: " + name + ", Age: " + age;
}
  • ์„œ๋ฒ„๋Š” ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋ณด๋‚ธ ํผ ๋ฐ์ดํ„ฐ์—์„œ name๊ณผ age ๊ฐ’์„ ์ถ”์ถœํ•˜์—ฌ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

5. ์š”์•ฝ.

  • ํผ ๋ฐ์ดํ„ฐ๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์›น ๋ธŒ๋ผ์šฐ์ €์˜ ํผ์„ ํ†ตํ•ด ์„œ๋ฒ„๋กœ ์ „์†กํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
  • ํผ ๋ฐ์ดํ„ฐ๋Š” GET ๋˜๋Š” POST ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์ „์†ก๋˜๋ฉฐ, URL ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹์ด๋‚˜ multipart/form-data ๋ฐฉ์‹์œผ๋กœ ์„œ๋ฒ„์— ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค.
  • ์„œ๋ฒ„๋Š” ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„ ์ฒ˜๋ฆฌํ•˜์—ฌ ์‚ฌ์šฉ์ž ์š”์ฒญ์— ๋งž๋Š” ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

8๏ธโƒฃ @RestController ์• ๋„ˆํ…Œ์ด์…˜.

  • @RestController ์• ๋„ˆํ…Œ์ด์…˜ ์€ Spring Framework์—์„œ RESTful ์›น ์„œ๋น„์Šค์˜ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์ •์˜ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์• ๋„ˆํ…Œ์ด์…˜์ž…๋‹ˆ๋‹ค.
  • ์ด ์• ๋„ˆํ…Œ์ด์…˜์€ ์ปจํŠธ๋กค๋Ÿฌ ํด๋ž˜์Šค์— ๋ถ™์ด๋ฉฐ, ์ด ํด๋ž˜์Šค๊ฐ€ JSON์ด๋‚˜ XML๊ณผ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๋ฅผ HTTP ์‘๋‹ต ๋ณธ๋ฌธ์œผ๋กœ ์ง์ ‘ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ํ•ด์ค๋‹ˆ๋‹ค.

1. @RestController์˜ ํŠน์ง•.

1. @Controller + @ResponseBody์˜ ๊ฒฐํ•ฉ.

  • @RestController๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ @Controller์™€ @ResponseBody ์• ๋„ˆํ…Œ์ด์…˜์„ ๊ฒฐํ•ฉํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.
  • @Controller๋Š” Spring MVC์—์„œ View๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ์ „ํ†ต์ ์ธ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์ •์˜ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜์ง€๋งŒ, @RestController๋Š” View๋ฅผ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๊ณ , ๊ฐ์ฒด๋‚˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ง์ ‘ HTTP ์‘๋‹ต ๋ณธ๋ฌธ์œผ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • @ResponseBody๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฐ์ฒด๋ฅผ JSON์ด๋‚˜ XML๋กœ ์ง๋ ฌํ™”ํ•˜์—ฌ HTTP ์‘๋‹ต์˜ ๋ณธ๋ฌธ์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.
  • @RestController๋Š” ํด๋ž˜์Šค ๋ ˆ๋ฒจ์—์„œ ๋ชจ๋“  ๋ฉ”์„œ๋“œ์— @ResponseBody๊ฐ€ ์ž๋™์œผ๋กœ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค.

2. RESTful ์›น ์„œ๋น„์Šค์— ์ ํ•ฉ.

  • @RestController๋Š” ์ฃผ๋กœ RESTful ์›น ์„œ๋น„์Šค ๊ฐœ๋ฐœ์— ์‚ฌ์šฉ๋˜๋ฉฐ, ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ JSON, XML ๋“ฑ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๋„๋ก ์„ค๊ณ„๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
  • ์ „ํ†ต์ ์ธ MVC ํŒจํ„ด์—์„œ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ทฐ ํŽ˜์ด์ง€(HTML ๋“ฑ)๋กœ ์ „๋‹ฌํ•˜์ง€๋งŒ, RESTful ์›น ์„œ๋น„์Šค์—์„œ๋Š” ๋ฐ์ดํ„ฐ ์ž์ฒด(JSON, XML ๋“ฑ)๋ฅผ ์‘๋‹ต์œผ๋กœ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

2. ์˜ˆ์‹œ ์ฝ”๋“œ.

@RestController
public class UserController {
    
    @GetMapping("/users")
    public List<User> getAllUsers() {
        // ์‚ฌ์šฉ์ž ๋ชฉ๋ก์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ์˜ˆ์‹œ
        return userService.getAllUsers();
    }
    
    @PostMapping("/users")
    public User createUser(@RequestBody User user) {
        // ์ƒˆ๋กœ์šด ์‚ฌ์šฉ์ž๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์˜ˆ์‹œ
        return userService.saveUser(user);
    }
}
  • @RestController
    • ์ด ํด๋ž˜์Šค๋Š” RESTful ์ปจํŠธ๋กค๋Ÿฌ์ด๋ฉฐ, ๋ชจ๋“  ๋ฉ”์„œ๋“œ๋Š” JSON ๋˜๋Š” XML๊ณผ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๋ฅผ HTTP ์‘๋‹ต ๋ณธ๋ฌธ์œผ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
    • @GetMapping("/users")
      • GET ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋ฉฐ, List<User> ๊ฐ์ฒด๋ฅผ JSON ํ˜•์‹์œผ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
    • @PostMapping("/users")
      • POST ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋ฉฐ, ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋ณด๋‚ธ User ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„ ์ƒˆ๋กœ์šด ์‚ฌ์šฉ์ž๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ JSON ํ˜•์‹์œผ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

3. @RestController์™€ ์ผ๋ฐ˜ ์ปจํŠธ๋กค๋Ÿฌ(@Controller)์˜ ์ฐจ์ด.

  • 1. ์ฃผ๋œ ๋ชฉ์ .
    • @RestController๋Š” ๋ฐ์ดํ„ฐ(์ฃผ๋กœ JSON, XML)๋ฅผ ์ง์ ‘ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋ฉฐ, API ์„œ๋ฒ„๋ฅผ ๊ตฌ์ถ•ํ•  ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
    • @Controller๋Š” ๋ทฐ(HTML, JSP ํŽ˜์ด์ง€ ๋“ฑ)๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฐ ์ฃผ๋กœ ์‚ฌ์šฉ๋˜๋ฉฐ, ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ํ™”๋ฉด์„ ๋ณด์—ฌ์ค„ ๋•Œ ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค.
  • 2. View ํ•ด์„.
    • @RestController๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์‘๋‹ต ๋ณธ๋ฌธ์— ๋ฐ”๋กœ ์ „๋‹ฌํ•˜๋ฉฐ, JSP๋‚˜ Thymeleaf ๊ฐ™์€ ๋ทฐ ํ…œํ”Œ๋ฆฟ ์—”์ง„์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
    • @Controller๋Š” ๋ทฐ ์ด๋ฆ„์„ ๋ฐ˜ํ™˜ํ•˜๊ณ , Spring MVC๋Š” ์ด ๋ทฐ ์ด๋ฆ„์„ ํ•ด์„ํ•˜์—ฌ JSP๋‚˜ Thymeleaf ๊ฐ™์€ ํ…œํ”Œ๋ฆฟ ์—”์ง„์„ ์‚ฌ์šฉํ•ด ํ™”๋ฉด์„ ๋ Œ๋”๋งํ•ฉ๋‹ˆ๋‹ค.
  • 3. @ResponseBody ํ•„์š” ์—ฌ๋ถ€.
    • @RestController๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ”์„œ๋“œ๋งˆ๋‹ค @ResponseBody๋ฅผ ๋ถ™์ผ ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.
      • ํด๋ž˜์Šค ๋ ˆ๋ฒจ์—์„œ ์ž๋™์œผ๋กœ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค.
    • @Controller๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ, ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ ค๋ฉด ๋ฉ”์„œ๋“œ๋งˆ๋‹ค @ResponseBody๋ฅผ ๋ถ™์—ฌ์„œ ์‘๋‹ต ๋ณธ๋ฌธ์— ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ด๋„๋ก ๋ช…์‹œํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
      • ์ผ๋ฐ˜ ์ปจํŠธ๋กค๋Ÿฌ(@Controller) ์˜ˆ์‹œ
          @Controller
          public class ViewController {
                    
          @GetMapping("/home")
          public String home() {
              // "home.html"์ด๋ผ๋Š” ๋ทฐ๋ฅผ ๋ฐ˜ํ™˜ํ•จ
              return "home";
          }
          }
        
      • ์ด ์ฝ”๋“œ๋Š” โ€œhomeโ€์ด๋ผ๋Š” ๋ทฐ ์ด๋ฆ„์„ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ, Spring์€ home.html ๋˜๋Š” home.jsp๋ฅผ ์ฐพ์•„์„œ ๋ Œ๋”๋งํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

4. ์š”์•ฝ.

  • @RestController๋Š” RESTful ์›น ์„œ๋น„์Šค ๊ฐœ๋ฐœ์— ์‚ฌ์šฉ๋˜๋ฉฐ, ๋ฐ์ดํ„ฐ๋ฅผ JSON์ด๋‚˜ XML ๋“ฑ์˜ ํ˜•์‹์œผ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • @RestController๋Š” @Controller์™€ @ResponseBody์˜ ๊ฒฐํ•ฉ์œผ๋กœ, ๋ฐ์ดํ„ฐ๋ฅผ ์‘๋‹ต ๋ณธ๋ฌธ์— ๋ฐ”๋กœ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ์„ค๊ณ„๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
  • Spring MVC์—์„œ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋ทฐ๋ฅผ ๋ Œ๋”๋งํ•˜๋Š” @Controller ์™€ ๋‹ฌ๋ฆฌ, @RestController๋Š” API ๊ฐœ๋ฐœ์— ๋” ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค.