[Playground] Spring API 단건 조회 (feat. 404)

Daeya·약 16시간 전

Playground

목록 보기
7/7

id로 메모 한 건 조회, 없으면 404

GET /api/memos/1
GET /api/memos/99999 → 404

목록(GET /api/memos)은 있고, 한 건만 가져오는 API는 없었다.
수정/삭제는 /{id}가 있었지만 조회는 목록에서만 쓰고 있었다.


1. 단건 조회, 지금 화면에는 필요없지만

목록단건
URLGET /api/memosGET /api/memos/{id}
응답page + items[]MemoVO 하나
없을 때빈 배열404

Vue 화면에는 당장 꽂을 곳이 없어도, REST 기본 패턴이라 API만 만들어 놓았다.
확인은 Postman / 브라우저 Console로만 간단히 할 것.


2. DBeaver에서 쿼리 확인

SELECT ID AS id, TITLE AS title, CONTENT AS content,
       STATUS AS status, CREATED_AT AS createdAt,
       UPDATED_AT AS updatedAt, DEL_YN AS delYn
  FROM MEMO
 WHERE ID = 1
   AND DEL_YN = 'N';

3. Mapper에 selectMemoById

<select id="selectMemoById" parameterType="int" resultType="MemoVO">
    SELECT ID AS "id", TITLE AS "title", ...
      FROM MEMO
     WHERE ID = #{id}
       AND DEL_YN = 'N'
</select>

4. MemoNotFoundException

검증(400)이랑 짝으로, 없는 리소스용 예외를 둔다.

public class MemoNotFoundException extends RuntimeException {
    public MemoNotFoundException(String message) {
        super(message);
    }
}
예외HTTP
MemoValidationException400
MemoNotFoundException404

5. Service에서 null이면 throw

public MemoVO selectMemo(int id) {
    MemoVO memo = memoMapper.selectMemoById(id);
    if (memo == null) {
        throw new MemoNotFoundException("메모를 찾을 수 없습니다");
    }
    return memo;
}

6. Controller

@GetMapping("/{id}")
public MemoVO get(@PathVariable int id) {
    return memoService.selectMemo(id);
}

7. ExceptionHandler → 404

@ExceptionHandler(MemoNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public Map<String, String> handleNotFound(MemoNotFoundException e) {
    Map<String, String> error = new HashMap<>();
    error.put("message", e.getMessage());
    return error;
}

검증 handler랑 같은 클래스에 추가하면 된다.


8. 확인

fetch('/api/memos/1').then(r => r.json()).then(console.log)
// → 200, 메모 객체

fetch('/api/memos/99999').then(async r => console.log(r.status, await r.json()))
// → 404, { message: "메모를 찾을 수 없습니다" }

Vue는 getMemo(id)만 api에 두고, 화면에는 연결하지 않았다.


정리

  • 단건 조회 = GET /{id}
  • 없으면 null 말고 404 예외
  • 400(검증) / 404(없음) 구분

참고

profile
Daeya

0개의 댓글