2025-04-28
@RestController
๋ฅผ ํ์ฉํ์ฌ ๋ฉ๋ชจ ๊ด๋ฆฌ API ๊ตฌํ.@GetMapping(value = "/getAll", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public List<MemoDto> getAll() {
log.info("GET /rest/memo/getAll");
return memoService.getAllMemo();
}
GET http://localhost:8091/ex08_restController/rest/memo/getAll
@GetMapping(value = "/get/{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<MemoDto> get(@PathVariable int id) {
log.info("GET /memo/get... " + id);
MemoDto dto = memoService.getMemo(id);
if (dto == null) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<>(dto, HttpStatus.OK);
}
GET http://localhost:8091/ex08_restController/rest/memo/get/8889
dto
๊ฐ null
์ด๋ฉด 404 ๋ฐํ.@PostMapping("/post")
public void add(@RequestBody MemoDto dto) throws SQLException {
log.info("POST /memo/add_rest_post.." + dto);
memoService.registraionMemo(dto);
}
POST http://localhost:8091/ex08_restController/rest/memo/post
{
"id": "8890",
"text": "c",
"writer": "c",
"createAt": "2025-04-28T11:34:10"
}
๊ธฐ๋ฅ | Method | URL | ๋น๊ณ |
---|---|---|---|
์ ์ฒด ์กฐํ | GET | /rest/memo/getAll | JSON ๋ฐฐ์ด ๋ฐํ |
๋จ๊ฑด ์กฐํ | GET | /rest/memo/get/{id} | JSON ๋ฐํ / 404 ์ฒ๋ฆฌ |
๋ฉ๋ชจ ๋ฑ๋ก | POST | /rest/memo/post | Body์ JSON ์ ๋ ฅ |
memoService.getMemo
) ๋ก์ง ํ์ธ@RestController
์ @RequestBody
, ResponseEntity
๋ฅผ ํ์ฉํด RESTful API ๊ธฐ๋ณธ ๊ตฌํ.