Back-end

Rest API?

Spring Boot - What is Rest API?

๐Ÿ’ก Rest๋ž€?

โ€‹

  • ์›น๊ณผ ๊ฐ™์€ ๋ถ„์‚ฐ ํ•˜์ดํผ๋ฏธ๋””์–ด ์‹œ์Šคํ…œ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ํ†ต์‹  ๋„คํŠธ์›Œํฌ ์•„ํ‚คํ…์ฒ˜
  • HTTP์™€ URI์˜ ๋‹จ์ˆœํ•˜๊ณ  ๊ฐ„๊ฒฐํ•œ ์žฅ์ ์„ ๊ณ„์Šนํ•œ Network ์•„ํ‚คํ…์ณ
  • ์ฐธ๊ณ  : 

https://meetup.toast.com/posts/92

 

๐Ÿ’ก Rest๊ฐ€ ๋งŒ๋“ค์–ด์ง„ ๋ชฉ์ 

  • ๊ตฌ์„ฑ์š”์†Œ ์ƒํ˜ธ์ž‘์šฉ์˜ ๊ทœ๋ชจ ํ™•์žฅ์„ฑ
  • ์ธํ„ฐํŽ˜์ด์Šค์˜ ๋ฒ”์šฉ์„ฑ
  • ๊ตฌ์„ฑ์š”์†Œ์˜ ๋…๋ฆฝ์ ์ธ ๋ฐฐํฌ
  • ์ค‘๊ฐ„์  ๊ตฌ์„ฑ์š”์†Œ๋ฅผ ์ด์šฉํ•œ ์‘๋‹ต ์ง€์—ฐ ๊ฐ์†Œ, ๋ณด์•ˆ ๊ฐ•ํ™”, ๋ ˆ๊ฑฐ์‹œ ์‹œ์Šคํ…œ ์ธ์บก์Š๋ ˆ์ด์…˜

Rest API ์„ค๊ณ„ํ•˜๊ธฐ

  • ์ž์› : URI
  • ํ–‰์œ„ : HTTP ๋ฉ”์†Œ๋“œ
  • ํ‘œํ˜„ : ๋ฆฌ์†Œ์Šค์— ๋Œ€ํ•œ ํ‘œํ˜„ (HTTP Message Body)

๐Ÿ’ก URI ์„ค๊ณ„

URI๋Š” URL์„ ํฌํ•จํ•œ ๊ฐœ๋…

URL : ์›น์ƒ์˜ ํŒŒ์ผ ์œ„์น˜๋ฅผ ํ‘œํ˜„ -> http://localahost8080/api/book.pdf

URI : ์›น์— ์žˆ๋Š” ์ž์›์˜ ์ด๋ฆ„๊ณผ ์œ„์น˜๋ฅผ ์‹๋ณ„ -> http://localhost:8080/api/book/1

  • URI ๋Š” ๋ช…์‚ฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ ๋™์‚ฌ๋ฅผ ํ”ผํ•ด์ฃผ์ž. ๋™์‚ฌ๋ฅผ ์“ฐ์ง€ ์•Š๊ณ  HTTP ๋ฉ”์†Œ๋“œ๋กœ ๋Œ€์ฒด๋ฅผ ํ•ด์ค˜์•ผํ•œ๋‹ค.

  • ๋ณต์ˆ˜ํ˜•์„ ์‚ฌ์šฉํ•˜์ž.

  • ์ปฌ๋ ‰์…˜์œผ๋กœ URI๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ์ปฌ๋ ‰์…˜์„ ํ•œ๋ฒˆ ๋” ๊ฐ์‹ผ ์ค‘์ฒฉํ˜•์‹์œผ๋กœ ์‚ฌ์šฉํ•˜์ž -> ์ถ”ํ›„ ์ˆ˜์ •, ํ™•์žฅ ์‹œ ๋ฒˆ๊ฑฐ๋กœ์›€์„ ์—†์•ฐ

__์ค‘์ฒฉ ์ „ __

{
    { 
        books: ...
    },
    {
        stores: ...
    }

}

์ค‘์ฒฉ ํ›„

{
    _embedded: [
        {
            books:...
        },
        {
            stores:...
        },
    ]
}

๐Ÿ’ก ํ–‰์œ„์„ค๊ณ„

  • books๋ฅผ ๊ฐ–๊ณ  ํ–‰์œ„๋ฅผ ์„ค๊ณ„ํ•œ๋‹ค๊ณ  ํ•˜์ž

  • book ๋ชฉ๋ก์„ ๋ณด๊ธฐ ์œ„ํ•ด์„œ๋Š” GET(read)๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•˜๋ฉฐ, book์„ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” POST(create)๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.

  • ์ด๋ ‡๊ฒŒ GET, POST, PUT, DELETE๋ฅผ ์•Œ๋งž๊ฒŒ ๊ธฐ๋Šฅ์— ๋งž์ถฐ ๋งŒ๋“ค์–ด์ค˜์•ผ ํ•œ๋‹ค.

(์˜ˆ์‹œ) books๋ฅผ ๊ฒŒ์‹œํŒ์— ํ‘œํ˜„ํ•  ๋•Œ ํŽ˜์ด์ง•์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฐ’์„ ์ถ”๊ฐ€ํ•ด์ฃผ์ž

JPA์˜ pageable์˜ ํ”„๋กœํผํ‹ฐ๊ฐ’์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

GET http://localhost:8080/api/books?page=0&size=10&sort=desc

 

 

๋ฐ˜์‘ํ˜•

'Back-end' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

DataAccessException์ด๋ž€? (What is DataAccessException?)  (0) 2020.08.24