[Spring] Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException 오류 해결

그린·2023년 6월 29일

Spring

목록 보기
3/4
post-thumbnail

스프링 부트 게시판을 만들면서 포스트맨으로 CRUD를 테스트하면서 오류가 발생했다.

📌코드

BoardController.java 에서 등록된 글을 불러오는 메소드를 테스트하다가 제목과 같은 오류가 발생했다.

@GetMapping("/{id}")
    public BoardResponseDto findById(@PathVariable Long id) {
        return boardService.findById(id);
    }

📌오류 메시지

{
    "timestamp": "2023-06-29T14:26:09.246+00:00",
    "status": 406,
    "error": "Not Acceptable",
    "trace": "org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation...(중략),
    "message": "Could not find acceptable representation",
    "path": "/board/2"
}

HttpMediaTypeNotAcceptableException
요청 핸들러가 허용된 응답을 만들어낼 수 없을 때 발생하는 에러라고 한다.

📌해결

BoardResponseDto 에 @Getter 를 추가해서 오류를 해결했다.

@Getter
public class BoardResponseDto {
    private Long id;
    private String title;
    private String content;
    private int countVisit;
    private LocalDateTime lastModifiedDate;

    public BoardResponseDto(Board board) {
        this.id = board.getId();
        this.title = board.getTitle();
        this.content = board.getContent();
        this.lastModifiedDate = board.getLastModifiedDate();
        this.countVisit = board.getCountVisit();
    }
}

클래스의 변수들이 private로 선언되어 있기 때문에 getter 없으면 응답 내용에 값이 포함되지 않는 문제가 발생한 것이다.

0개의 댓글