[Spring] Rest-swagger - POST

์ง„์ฃผยท2022๋…„ 3์›” 18์ผ
0

Spring

๋ชฉ๋ก ๋ณด๊ธฐ
11/29

๐Ÿฅ— Controller.java

PostMapping ์‚ฌ์šฉ

  // POST methods

  // http://localhost:8090/api/v1/book
  @PostMapping("/book")
  public String insertBook(@RequestBody BookDto bookDto){
    BookDto book = bookDto;

    return book.toString();
  }

  @PostMapping("/book2")
  public BookDto insertBook2(@RequestBody BookDto bookDto){
    BookDto book = bookDto;

    return book;
  }

์ฝ”๋“œ ์ถ”๊ฐ€ ํ›„, ์„œ๋ฒ„ ์žฌ์‹คํ–‰ (RestApplication.java์—์„œ F5 ํด๋ฆญ)

GET์—์„œ POST๋กœ ๋ณ€๊ฒฝ

{
  "name" : "๊น€๋นต๋นต์€ ๋ˆ„๊ตฌ์ธ๊ฐ€",
  "author" : "๊น€์ˆœ์ž"
}

body๋ถ€๋ถ„์— JSON ํ˜•ํƒœ๋กœ name, author ๋ฐ์ดํ„ฐ ์ž…๋ ฅ ํ›„ S end

Response 200 = ์ •์ƒ ์‹คํ–‰๋์Œ์„ ์˜๋ฏธํ•œ๋‹ค.

ํ˜„์žฌ ์ด ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋œ ๊ฒƒ,

return book.toString()์ด๋ฏ€๋กœ ์ฃผ์†Œ๊ฐ’์„ return ํ•œ๋‹ค.

๋•Œ๋ฌธ์— Body๋ถ€๋ถ„์— com.example.rest.model.BookDto@179f53dd ์ด ์ถœ๋ ฅ๋œ ๊ฒƒ์ด๋‹ค.


toString() ํ•ด๊ฒฐ ๋ฐฉ๋ฒ• ?

BookDto์—์„œ ์šฐํด๋ฆญ - ์†Œ์Šค ์ž‘์—… ํด๋ฆญ

Generate toString() ํด๋ฆญ

์ฝ”๋“œ๊ฐ€ ์ถ”๊ฐ€๋œ๋‹ค.

์„œ๋ฒ„ ๊ป๋‹ค๊ฐ€ ์žฌ์‹คํ–‰ ํ›„


Send ๋‹ค์‹œ ๋ณด๋‚ด๊ธฐ

Response 200

Body์— BookDto [author=๊น€์ˆœ์ž, name=๊น€๋นต๋นต์€ ๋ˆ„๊ตฌ์ธ๊ฐ€] ์ถœ๋ ฅ๋œ๋‹ค.

profile
์ง„์ฃผ์˜ ์ฝ”๋”ฉ์ผ๊ธฐ

0๊ฐœ์˜ ๋Œ“๊ธ€