REST (2)

· โ˜• 4 min read · ๐Ÿ‘€... views

์ง€๋‚œ ํฌ์ŠคํŒ… ์—์„œ๋Š” REST ์˜ ๊ฐœ์š”์™€ ๋กœ์ด ํ•„๋”ฉ์ด ๋งํ•˜๋Š” 6๊ฐ€์ง€ ์ œ์•ฝ์‚ฌํ•ญ ๊ทธ๋ฆฌ๊ณ , ๋ฆฌ์ฒ˜๋“œ์Šจ ์„ฑ์ˆ™๋„๋ผ๋Š” ๊ฒƒ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ดค์Šต๋‹ˆ๋‹ค.

๊ฐœ๋… ์œ„์ฃผ๋กœ ์•Œ์•„๊ฐ”๋Š”๋ฐ์š”. ์ด๋ฒˆ์—๋Š” ์•ฝ๊ฐ„(?) ์‹ค์ „ ์œ„์ฃผ๋กœ ์–ด๋–ค ๊ฒƒ๋“ค์ด ์žˆ๋Š”์ง€ ์•Œ์•„๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

1. REST ํ˜• API ๋””์ž์ธ

1. ๋ฆฌ์†Œ์Šค ๋ช…๋ช…

  • RESTํ˜• API๋Š” ์‚ฌ์šฉํ•˜๋Š” ํด๋ผ์ด์–ธํŠธ ์ž…์žฅ์—์„œ ๋ดค์„ ๋•Œ ์˜๋ฏธ๊ฐ€ ๋ถ„๋ช…ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ๋ฆฌ์†Œ์Šค๋ฅผ ์–ด๋–ค ๋ช…์‚ฌ๋กœ ํ•  ๋•Œ์—” ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๊ฐ€ ์–ด๋–ป๊ฒŒ ์ƒ๊ฒผ๋Š”์ง€, ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์–ด๋–ค ์‹์œผ๋กœ ์‚ฌ์šฉํ•  ๊ฐ€๋Šฅ์„ฑ์ด ํด์ง€ ๋ฏธ๋ฆฌ ํŒŒ์•…์„ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. (์„ค๊ณ„๋ฅผ ์ž˜ ํ•ด์•ผ๊ฒ ์ฃ ..?)

2. ๋™์‚ฌ๊ฐ€ ์•„๋‹Œ ๋ช…์‚ฌ๋กœ

  • ๋ฆฌ์†Œ์Šค๋ช…์€ ๋™์‚ฌ๊ฐ€ ์•„๋‹ˆ๋ผ ๋ช…์‚ฌ๋กœ ์‹œ์ž‘ํ•˜๋ฉฐ, ๋ฆฌ์†Œ์Šค๋Š” ๋ฆฌ์†Œ์Šค๋ฅผ ํ•ด์•ผํ•  ์ผ์€ HTTP ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค.
  • ex) ์ „์ฒด ์‚ฌ์šฉ์ž๋ฅผ ์˜๋ฏธํ•˜๋Š” ๋ช…์‚ฌ users ์™€ ์‚ฌ์šฉ์ž ์‹๋ณ„ ID ๋ฅผ ํ•ฉ์ณ /users/1234 ์™€ ๊ฐ™์ด ํ‘œํ˜„
  • ๋ฆฌ์†Œ์Šค๋ฅผ ์ •ํ•œ ๋‹ค์Œ์—” HTTP ๋ฉ”์†Œ๋“œ๋กœ ํ•˜๊ณ ์‹ถ์€ ๊ฑธ ์ •ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. (ex. ์กฐํšŒ๋ผ๋ฉด GET, ์ƒ์„ฑ์€ POST)

3. ์ž๊ธฐ ์„œ์ˆ ํ˜•์œผ๋กœ

  • ๋ฆฌ์†Œ์Šค๋Š” ์ž์‹ ์„ ๊ฐ€์žฅ ์ž˜ ๋‚˜ํƒ€๋‚ด๋Š” ๋ช…์‚ฌ๋กœ ๋ช…๋ช…
  • URI๋‚˜ HTTP๋ฉ”์†Œ๋“œ๋ฅผ ์ฒ˜์Œ ๋ดค์„ ๋•Œ ์–ด๋Š ๋ฆฌ์†Œ์Šค๋ฅผ ์–ด๋–ป๊ฒŒ ํ•˜๊ฒ ๋‹ค๋Š” ๊ฒƒ์ธ์ง€ ๋ฐ”๋กœ ๋ˆˆ์— ๋“ค์–ด์˜ค์ง€ ์•Š์œผ๋ฉด REST ํ˜• URI ๋ผ๊ณ  ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

4. ๋‹จ์ˆ˜ ์•„๋‹Œ ๋ณต์ˆ˜ํ˜•์œผ๋กœ

  • ๋ฆฌ์†Œ์Šค๋ช…์€ ๋ฐ์ดํ„ฐ ์ง‘ํ•ฉ์„ ๋‚˜ํƒ€๋‚ด๊ธฐ ๋•Œ๋ฌธ์— ๋ณต์ˆ˜ํ˜•์œผ๋กœ ํ‘œ๊ธฐํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. (ex. ์œ ์ €๋“ค = users)
  • ๋ณต์ˆ˜ํ˜• ๋ช…์‚ฌ๋Š” ์„œ๋น„์Šค์˜ ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๊ณ  ID๋Š” ๊ทธ๋Ÿฐ ๋ฐ์ดํ„ฐ ์ค‘์— ํ•œ ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚ด (ex. users/1234)

5. HTTP ๋ฉ”์†Œ๋“œ

  • HTTP1.1 ๋ช…์„ธ์—๋Š” 8๊ฐœ์˜ HTTP ๋ฉ”์†Œ๋“œ๊ฐ€ ์กด์žฌํ•˜์ง€๋งŒ, ์ž์ฃผ ์“ฐ์ด๋Š” ๊ฒƒ์€ GET, POST, PUT, DELETE

5-1. GET

  • ์กฐํšŒํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋กœ ๋ฉฑ๋“ฑ์„ฑ์„ ๋ณด์žฅํ•ฉ๋‹ˆ๋‹ค. (100๋ฒˆ์„ ํ˜ธ์ถœํ•ด๋„ 100๋ฒˆ ๊ฐ™์€ ๊ฒฐ๊ณผ)
  • ์š”์ฒญ ์„ฑ๊ณต ์‹œ 200 (OK) ์‘๋‹ต ์ฝ”๋“œ๋ฅผ ๋ณด๋‚ด๋ฉฐ, ์—†์œผ๋ฉด 404 (not found), ํ˜•์‹์ด ์ž˜๋ชป๋˜๋ฉด 400 (bad request) ์ž…๋‹ˆ๋‹ค.
  • GET users/1234/posts ์˜ URI ๋Š” 1234 ์‚ฌ์šฉ์ž์˜ ๋ชจ๋“  ํฌ์ŠคํŠธ๋ฅผ ์กฐํšŒํ•˜๋Š” URI ์ž…๋‹ˆ๋‹ค.

5-2. POST

  • ์ฃผ์–ด์ง„ ์ปจํ…์ŠคํŠธ์—์„œ ๋ฆฌ์†Œ์Šค๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“œ๋Š” ๋ฉ”์†Œ๋“œ์ž…๋‹ˆ๋‹ค.
  • ์ƒˆ ๋ฆฌ์†Œ์Šค๋ฅผ ์ƒ์„ฑํ•œ ๋’ค ์ปจํ…์ŠคํŠธ์— ๋ฌถ๊ณ  ID ๋ฅผ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค.
  • ์ƒˆ๋กœ ์ถ”๊ฐ€ํ•  ์ •๋ณด๋ฅผ ๋ณธ๋ฌธ์— ๋„ฃ๊ณ  ์š”์ฒญ์„ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. (ex. POST users/1234/posts)

5-3. PUT

  • ์•Œ๋ ค์ง„ ๋ฆฌ์†Œ์Šค๋ฅผ ์ˆ˜์ •ํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
  • ์š”์ฒญ ๋ณธ๋ฌธ์— ๋ฆฌ์†Œ์Šค์˜ ์ˆ˜์ • ํ›„ ๋ฒ„์ „์ด ์žˆ๊ณ , HTTP ์‘๋‹ต์ฝ”๋“œ 200 ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.(ex. PUT users/1234)
  • ์ˆ˜์ •ํ•  ๋‚ด์šฉ๋งŒ ๋ณด๋‚ด๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ˆ˜์ •ํ•  ๋‚ด์šฉ์„ ํฌํ•จํ•œ ๋ฆฌ์†Œ์Šค ํ‘œํ˜„ํ˜•์„ ํ†ต์งธ๋กœ ๋ณด๋ƒ…๋‹ˆ๋‹ค.

5-4. DELETE

  • ๋ฆฌ์†Œ์Šค๋ฅผ ์‚ญ์ œํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. (ex. DELETE /users/1234)
  • ์‚ญ์ œ๊ฐ€ ์„ฑ๊ณตํ•˜๋ฉด 200 ์„ ์‚ญ์ œํ•  ๋ฆฌ์†Œ์Šค๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด 400์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

2. REST ์˜ˆ์‹œ

์‚ฌ์šฉ์ž(users) ๋ฆฌ์†Œ์Šค์™€ ํŒ”๋กœ์›Œ(follows) ๋Œ€ํ•ด์„œ REST ์˜ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์„ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

  • ์‚ฌ์šฉ์ž๋ฅผ ์ƒˆ๋กœ ๋“ฑ๋ก

    POST /users

  • ์‚ฌ์šฉ์ž ์กฐํšŒ

    GET /users/1234

    GET /users (๋ชจ๋“  ์‚ฌ์šฉ์ž ์กฐํšŒ)

  • ์‚ฌ์šฉ์ž ์ •๋ณด ์ˆ˜์ •

    PUT /users/1234

  • ์‚ฌ์šฉ์ž ์‚ญ์ œ

    DELETE /users/1234

    DELETE /users (๋ชจ๋“  ์‚ฌ์šฉ์ž ์‚ญ์ œ)

  • 1234 ์‚ฌ์šฉ์ž์˜ ์ „์ฒด ํŒ”๋กœ์›Œ ์กฐํšŒ

    GET /users/1234/followers

  • 1234 ์‚ฌ์šฉ์ž์˜ ํŒ”๋กœ์›Œ๋กœ ๋“ฑ๋ก

    POST /users/1234/followers

  • 1234 ์‚ฌ์šฉ์ž๋ฅผ ํŒ”๋กœ์›Œํ•œ 4567 ์˜ ์ •๋ณด

    GET /users/1234/followers/4567

    OR

    GET /users/4567

  • 4567์ด 1234์˜ ํŒ”๋กœ์›Œ ํ•ด์ œ

    DELETE /users/1234/followers/4566

3. HATEOAS

HATEOAS ๋Š” ๋ฆฌ์ฒ˜๋“œ์Šจ ์„ฑ์ˆ™๋„ ๋ชจ๋ธ ์ค‘ ๊ฐ€์žฅ ๋†’์€ ๋‹จ๊ณ„์˜ REST ๊ตฌํ˜„์ฒด์ž…๋‹ˆ๋‹ค.

์˜ˆ์‹œ๋กœ GET /posts ๋ผ๋Š” URI ๋ฅผ ํ†ตํ•ด ์ž์‹ ์ด ๋ณผ ์ˆ˜ ์žˆ๋Š” ๊ฒŒ์‹œ๋ฌผ์— ๋Œ€ํ•œ ๋ฆฌ์†Œ์Šค๋ฅผ ์š”์ฒญํ•œ๋‹ค๋ผ๊ณ  ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
{
  "posts" : [
    {
      "id" : 123,
      "title" : "๊ธฐ์ƒ์ถฉ",
      "content" "์ด๋Ÿฌ์ €๋Ÿฌํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค."
      "links" : [
      	{
      		"rel" : "self",
          "href" : "/posts/123"
          "method" : "GET"
        },
        {
          "rel" : "replies",
          "href" : "/posts/123/replies"
          "method" : "GET"
        },
    		{
          "rel" : "recommends",
          "href" : "/rest/posts/123/recommends"
          "method" : "GET"
        },
    		{
          "rel" : "owner",
          "href" : "/rest/posts/123/users"
          "method" : "GET"
        }
      ]
    }
  ]
}

์ด๋Ÿฐ ํ˜•ํƒœ๊ฐ€ ๋‚˜์˜ต๋‹ˆ๋‹ค.

๊ฐ๊ฐ ์„ค๋ช…์„ ํ•ด๋ณด๋ฉด, ๊ฒŒ์‹œ๋ฌผ์€ 1๊ฐœ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์•„์ด๋””, ํƒ€์ดํ‹€, ๋‚ด์šฉ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

์—ฌ๊ธฐ๊นŒ์ง€๋งŒ ๋ณด๋ฉด ์ผ๋ฐ˜ ์‘๋‹ต๊ณผ ํฌ๊ฒŒ ๋‹ค๋ฅด์ง€ ์•Š์€๋ฐ์š”. ์—ฌ๊ธฐ์„œ links ๊ฐ€ ์ฃผ๋ชฉํ•  ๋งŒํ•œ ์š”์†Œ๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

links ๋ฅผ ๋ณด๋ฉด 3๊ฐœ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. rel ๊ณผ href๋Š” ํ˜„์žฌ์˜ ๋ฆฌ์†Œ์Šค์™€ ์—ฐ๊ฒฐ๋˜๋Š” ๊ด€๊ณ„๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ, method๋Š” ํ•ด๋‹น ๋ฆฌ์†Œ์Šค๋ฅผ ์–ป์œผ๋ ค๊ณ ํ•  ๋•Œ ์‚ฌ์šฉํ•  HTTP ๋ฉ”์†Œ๋“œ ์ž…๋‹ˆ๋‹ค.

์œ„์—์„œ๋Š” self ๋Š” ๋ณธ์ธ, replies ๋Š” ํ•ด๋‹น ๊ธ€์˜ ๋Œ“๊ธ€๋“ค, recommends ๋Š” ์ถ”์ฒœํ•œ ์‚ฌ๋žŒ๋“ค, owner ๋Š” ์ž‘์„ฑ์ž ๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. rel ์— ๋Œ€ํ•ด์„œ๋Š” ํŠน๋ณ„ํžˆ ์ •ํ•ด์ง„ ๊ทœ์•ฝ์€ ์—†์–ด์„œ, ๋ช…ํ™•ํžˆ ์•Œ์•„๋ณผ ์ˆ˜๋งŒ ์žˆ์œผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ด๋Ÿฐ ์‹์œผ๋กœ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋‹ค๋ฅธ ๋ฆฌ์†Œ์Šค๋„ ๋ณผ ์ˆ˜ ์žˆ๋„๋ก ๊ด€๋ จ ๋งํฌ ์ •๋ณด๋ฅผ ํ•จ๊ป˜ ์ฃผ๋ฉด ํž˜๋“ค์ด์ง€ ์•Š๊ณ  API ๋ฅผ ํ™•์žฅํ•  ์ˆ˜ ์žˆ๋Š” ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

๋‹ค๋ฅธ ๊ณณ๋„ ์žˆ๊ฒ ์ง€๋งŒ, ์ œ๊ฐ€ ์ฐพ์€ ๊ณณ์—์„œ๋Š” PayPal ์—์„œ HATEOAS ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌ์ถ•์„ ํ–ˆ๊ณ , ๋งํฌ๋ฅผ ๋ณด๋ฉด ์‰ฝ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

4. ์ •๋ฆฌ

์ด๋ฒˆ์—๋Š” ์ง์ ‘ ์„ค๊ณ„๋ฅผ ํ•  ๋–„๋ฅผ ์ค‘์ ์œผ๋กœ ์˜ˆ์‹œ๋ฅผ ๋“ค์–ด๋ดค๋Š”๋ฐ, ์ ๋‹นํžˆ ์ง€ํ‚ค๋ฉด์„œ๋„ ์•ˆ ์ง€ํ‚ค๋Š” ๋“ฏํ•œ? ๊ทธ๋Ÿฐ ๋Š๋‚Œ์„ ๋ฐ›๋Š” ์š”์ฆ˜์ž…๋‹ˆ๋‹ค.

์ข€ ๋” ์ข‹์€? ๋ช…์พŒํ•œ? ๋ฆฌ์†Œ์Šค ๋ช…์„ ์ฐพ์•„๊ฐ€๋ฉด์„œ ๋งŒ๋“ค์–ด์•ผ ๊ฒ ์Šต๋‹ˆ๋‹ค.

HATEOAS ์˜ ๊ฒฝ์šฐ์—๋Š” ๋“ค์–ด๋ณธ ์ ์€ ์žˆ์ง€๋งŒ, ์‹ค์ œ๋กœ ๋ณธ ์ ์€ ์—†๋„ค์š”. ํด๋ผ์ด์–ธํŠธ ์ž…์žฅ์—์„œ๋Š” ํŽธํ•  ๊ฒƒ ๊ฐ™์•„ ๋ณด์ด์ง€๋งŒ, ์„œ๋ฒ„ ์ž…์žฅ์—์„œ๋Š” ๊ต‰์žฅํžˆ ๊ท€์ฐฎ์„ ๊ฒƒ ๊ฐ™๊ณ , ์–ด์ฐจํ”ผ API ๋ฌธ์„œ๋Š” ์„œ๋กœ ๊ณต์œ  ํ•ด์•ผ ํ•  ํ…๋ฐ…? ๋ผ๋Š” ์ƒ๊ฐ์ด ๋“ญ๋‹ˆ๋‹ค๋งŒ ์ต์ˆ™ํ•ด์ง€๋ฉด ์œ ์ถ”ํ•˜๊ธฐ ํŽธํ•  ๊ฒƒ ๊ฐ™๋‹ค๋Š” ์ƒ๊ฐ์€ ๋“œ๋„ค์š”.

Reference

Share on

snack
WRITTEN BY
snack
Web Programmer


What's on this Page