[Spring] Rest-swagger - GET(2)

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

Spring

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

๐Ÿฅ— Controller.java

3) PathVariable์„ ํ™œ์šฉํ•œ GET ์š”์ฒญ

  // @PathVariable์„ ํ™œ์šฉํ•œ GET ์š”์ฒญ
  // http://localhost8090/api/v1/:bookName
  @GetMapping("/{bookName}")
  public String getBookWithName(@PathVariable String bookName){
    return bookName;
  }

์ฝ”๋“œ ์ถ”๊ฐ€

[๊ฒฐ๊ณผ]

์„œ๋ฒ„ ๊ป๋‹ค๊ฐ€(ctrl+c) ๋‹ค์‹œ ํ‚จ ํ›„,

http://localhost:8090/api/v1/๊น€๋นต๋นต์€๋ˆ„๊ตฌ์ธ๊ฐ€ ์ž…๋ ฅ, Send ํด๋ฆญ

Response์— ๊น€๋นต๋นต์€๋ˆ„๊ตฌ์ธ๊ฐ€ ์ถœ๋ ฅ๋๋‹ค !


4) DTO ๊ฐ์ฒด ๋ฐ˜ํ™˜ํ•˜๋Š” GET ์š”์ฒญ

  // DTO(Data Transfer Object) ๊ฐ์ฒด ๋ฐ˜ํ™˜ํ•˜๋Š” GET ์š”์ฒญ
  // http://localhost:8090/api/v1/book/dto?name=์ฑ…์ด๋ฆ„&author=์ €์ž
  @GetMapping("/book/dto")
  public BookDto getBookDto(BookDto bookDto){
    return bookDto;
  }

์ฝ”๋“œ ์ถ”๊ฐ€

rest - ํด๋” ๋ช… : model ์ถ”๊ฐ€

model - ํŒŒ์ผ ๋ช… : BookDto.java ์ƒ์„ฑ

private์œผ๋กœ name, author ๋ณ€์ˆ˜ ์„ ์–ธ

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

Generate Getters and Setters ํด๋ฆญ

name, author ํด๋ฆญ ํ›„ ํ™•์ธ

Getter, Setter๊ฐ€ ์ƒ์„ฑ๋˜์—ˆ๋‹ค!

[๊ฒฐ๊ณผ]

์„œ๋ฒ„ ๊ป๋‹ค๊ฐ€(ctrl+c) ๋‹ค์‹œ ํ‚จ ํ›„,

http://localhost:8090/api/v1/book/dto?name=๊ฐœ๋ฏธ&author=๋ฒ ๋ฅด๋ฒ ๋ฅด ์ž…๋ ฅ ํ›„, Send ํด๋ฆญ

Body ๋ถ€๋ถ„์— DTO ๊ฐ์ฒด๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค !

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

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