| 기능 | Method | URL | Request | Response |
|---|---|---|---|---|
| 메모 생성 | POST | /api/memos | { "title": string, "content": string } | 성공 시 201 Created { "id": Long, "title": string, "content": string } |
| 메모 전체 조회 | GET | /api/memos | 1. 성공 시 200 OK [ { "id": Long, "title": string, "content": string }, ... ] 2. 없으면 200 OK와 비어있는 배열 응답 [] | |
| 메모 단건 조회 | GET | /api/memos/{id} | 1. 성공 시 200 OK { "id": Long, "title": string, "content": string } 2. 실패 시 404 Not Found 해당 식별자의 메모가 존재하지 않는 경우 | |
| 메모 수정 (덮어쓰기) | PUT | /api/memos/{id} | { "title": string, "content": string } | 1. 성공 시 200 OK { "id": Long, "title": string, "content": string } 2. 실패 시 - 404 Not Found 해당 식별자의 메모가 존재하지 않는 경우 - 400 Bad Requset 필수 값이 없는 경우 |
| 메모 제목 수정 | PATCH | /api/memos/{id} | { "title": string } | 1. 성공 시 200 OK { "id": Long, "title": string, "content": string } 2. 실패 시 - 404 Not Found 해당 식별자의 메모가 존재하지 않는 경우 - 400 Bad Requset 필수 값이 없는 경우 |
| 메모 삭제 | DELETE | /api/memos/{id} | 1. 성공 시 200 OK 2. 실패 시 404 Not Found 해당 식별자의 메모가 존재하지 않는 경우 |
요구사항
201 CREATEDAPI 구현
@RestController
@RequestMapping("/memos")
public class MemoController {
private final Map<Long, Memo> memoList = new HashMap<>();
@PostMapping
public ResponseEntity<MemoResponseDto> createMemo(@RequestBody MemoRequestDto dto) {
Long memoId = memoList.isEmpty() ? 1 : Collections.max(memoList.keySet()) + 1;
Memo memo = new Memo(memoId, dto.getTitle(), dto.getContents());
memoList.put(memoId, memo);
return new ResponseEntity<>(new MemoResponseDto(memo), HttpStatus.CREATED);
}
}
ResponseEntity<>: 상태 코드를 같이 반환하기 위해 사용
ResponseEntity<>사용 시 모든 응답을ResponseEntity로 통일하는 게 더욱 좋음
요구사항
200 OKAPI 구현
@RestController
@RequestMapping("/memos")
public class MemoController {
private final Map<Long, Memo> memoList = new HashMap<>();
@GetMapping
public List<MemoResponseDto> findAllMemos() {
// 리스트 초기화
List<MemoResponseDto> responseList = new ArrayList<>();
// MashMap<Memo> -> 전체 조회 -> List<MemoResponseDto>
// 1. 반복문 사용
for (Memo memo : memoList.values()) {
MemoResponseDto responseDto = new MemoResponseDto(memo);
responseList.add(responseDto);
}
// 2. 스트림 사용
// responseList = memoList.values().stream()
// .map(MemoResponseDto::new)
// .toList();
return responseList;
}
}
@GetMapping: 아무것도 없으면@RequestMapping의 URL이 Mapping됨
요구사항
200 OK404 NOT FOUNDAPI 구현
@RestController
@RequestMapping("/memos")
public class MemoController {
private final Map<Long, Memo> memoList = new HashMap<>();
@GetMapping("/{id}")
public ResponseEntity<MemoResponseDto> findMemoById(@PathVariable Long id) {
Memo memo = memoList.get(id);
// 해당 식별자를 가진 메모가 없으면 404 NOT FOUND 반환
if (memo == null) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<>(new MemoResponseDto(memo), HttpStatus.OK);
}
}
요구사항
200 OK404 NOT FOUNDAPI 구현
@RestController
@RequestMapping("/memos")
public class MemoController {
private final Map<Long, Memo> memoList = new HashMap<>();
@PutMapping("/{id}")
public ResponseEntity<MemoResponseDto> updateMemoById(
@PathVariable Long id,
@RequestBody MemoRequestDto dto
) {
Memo memo = memoList.get(id);
// 해당 식별자를 가진 메모가 없으면 404 NOT FOUND 반환
if (memo == null) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
// 제목이나 내용이 없으면 400 BAD REQUEST 반환
if (dto.getTitle() == null || dto.getContents() == null) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
memo.update(dto);
return new ResponseEntity<>(new MemoResponseDto(memo), HttpStatus.OK);
}
}
요구사항
400 BAD REQUEST200 OK404 NOT FOUNDAPI 구현
@RestController
@RequestMapping("/memos")
public class MemoController {
private final Map<Long, Memo> memoList = new HashMap<>();
// 일부 수정이기 때문에 @PatchMapping 사용
@PatchMapping("/{id}")
public ResponseEntity<MemoResponseDto> updateTitle(
@PathVariable Long id,
@RequestBody MemoRequestDto dto
) {
Memo memo = memoList.get(id);
// 해당 식별자를 가진 메모가 없으면 404 NOT FOUND 반환
if (memo == null) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
// 내용이 있거나, 제목이 null이면 400 BAD REQUEST 반환
if (dto.getTitle() == null || dto.getContents() != null) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
memo.updateTitle(dto);
return new ResponseEntity<>(new MemoResponseDto(memo), HttpStatus.OK);
}
}
200 OK404 NOT FOUND@RestController
@RequestMapping("/memos")
public class MemoController {
private final Map<Long, Memo> memoList = new HashMap<>();
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteMemo(@PathVariable Long id) {
// memoList의 key가 id를 포함하고 있다면 삭제
if (memoList.containsKey(id)) {
memoList.remove(id);
return new ResponseEntity<>(HttpStatus.OK);
}
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
스프링 입문 - 5주차