๊ฐ์ธ ํ๋ก์ ํธ๋ฅผ ๋ง๋ค์ด๋ณด๋ฉด์ ๋๊ฐ๋ง ์๊ณ ์ฐ๋ ๊ฒ ์ฐธ ๋ง๋ค๋ ๊ฑธ ๋๊ผ๋ค.
๋ด ์ฌ์ ์ ๋๊ฐ๋ง ์๋ค๋ ๊ฑด ๋ง์ด ์๋์ง ๐ฉโ๐ปโ ์์ฃผ์์ฃผ ๊ธฐ์ด์ ์ด๊ณ ์ธ์ธํ ๊ฒ๋ ๋ชจ๋ ์ ๋ฆฌ ~!!
(๊ฐ ํ์ ํผ๋๋ฐฑ ํด์ฃผ์ ์ ์ง์ฅ ์ฃผ์๋๊ป ๋ฌดํ๊ฐ์ฌ๋ฅผ ๋๋ฆฝ๋๋ค)
@RestController
@Controller + @ResponseBody ๊ฐ ๊ฒฐํฉ๋ ํํ์ ์ด๋ ธํ ์ด์ ์ด๋ค. @RestController๋ฅผ ์ฌ์ฉํ๋ฉด ํด๋์ค ํ์ ๋ฉ์๋์ @ResponseBody ์ด๋ ธํ ์ด์ ์ ๋ฌ์์ฃผ์ง ์์๋ ๋ฌธ์์ด์ด๋ JSON์ ๋ฆฌํดํ ์ ์๊ฒ๋๋ค.
@ApiOperation("์ปคํผ ๋ฉ๋ด ๋ชฉ๋ก ์กฐํ API")
@GetMapping("")
public List<MenuResponseDto> show() {
List<Coffee> coffeeList = menuService.show();
List<MenuResponseDto> response = new ArrayList<>();
for (Coffee c : coffeeList) {
response.add(new MenuResponseDto(c));
}
return response;
}
๊ฒฐ๊ณผ๋ ์ด๋ ๊ฒ ๋ฆฌ์คํธ๋ฅผ ์๋์ผ๋ก jsonํํ๋ก ๋ฐํํด์ฃผ๋ ๊ฑธ ๋ณผ ์ ์๋ค.
[
{
"id": 1,
"coffeeName": "Americano",
"price": 4500
},
{
"id": 2,
"coffeeName": "Cappuccino",
"price": 5000
},
{
"id": 3,
"coffeeName": "Coldbrew",
"price": 5500
},
{
"id": 4,
"coffeeName": "Latte",
"price": 5000
},
{
"id": 5,
"coffeeName": "Espresso",
"price": 3000
}
]
@ResponseEntity
์ฒ์์ ์ด๋ ๊ฒ ๋ฆฌํด๊ฐ์ ResponseEntity๋ก ์ฃผ์๋ค.
ํ์ง๋ง ๋จ์ํ list๋ฅผ ๋ด๊ธฐ๋ง ํ์ฌ ๋ฆฌํดํ๋ ๊ผด๋ก ์ ์์ค์ ๋์ผํ ๊ฒฐ๊ณผ๋ฅผ ๋ํ๋ธ๋ค.
@ResponseEntity๋ ์ํ์ฝ๋๋ฅผ ์ ์กํ๊ณ ์ถ์ ๋ฐ์ดํฐ์ ํจ๊ป ๋ณด๋ด ์ข ๋ ์ธ๋ฐํ ์ ์ด๊ฐ ํ์ํ ๊ฒฝ์ฐ ์ฌ์ฉํ๋ค. ์ด ๋ฉ์๋์์๋ ๊ทธ๋ฅ ๋ฆฌ์คํธ๋ง ๋ฟ๋ ค์ฃผ๊ธฐ ๋๋ฌธ์ ๊ตณ์ด๊ตณ์ด์๋ค.
ํค๋๋ HTTP ์ํ์ฝ๋์ ๋ฐ์ดํฐ๋ฅผ ํจ๊ป ์ฎ์ด ๋ณด๋ผ ๋ ์ฌ์ฉํ์!
@ApiOperation("์ปคํผ ๋ฉ๋ด ๋ชฉ๋ก ์กฐํ API")
@GetMapping("")
public ResponseEntity show() {
List<Coffee> coffeeList = menuService.show();
List<MenuResponseDto> response = new ArrayList<>();
for (Coffee c : coffeeList) {
response.add(new MenuResponseDto(c));
}
return ResponseEntity.ok(response);
}