[org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation]

pickylemon·2024년 5월 6일

Exception 모음

목록 보기
8/31

[2024-05-06 13:52:35:60820] [http-nio-8080-exec-1] WARN  o.s.w.s.m.s.DefaultHandlerExceptionResolver - (Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation]
[2024-05-06 13:52:35:60820] [http-nio-8080-exec-1] DEBUG o.s.web.servlet.DispatcherServlet - (Completed 406 NOT_ACCEPTABLE

상황

  • RestController에서 응답 객체를 반환하는 상황에서 406(Not Acceptable) 발생
  • Jackson 라이브러리도 이미 있고,
  • ajax 헤더에 "Accept" : "application/json" 까지 되어 있었던 상황
@ResponseBody
@GetMapping("/forum/notice/{boardTypeSeq}/{boardSeq}/vote.do")
public VoteResponse vote(
			@PathVariable("boardSeq") int boardSeq, 
			@PathVariable("boardTypeSeq") int boardTypeSeq,
			@RequestParam("isLike") String isLike,
			@RequestParam("thumb") boolean thumb,
			HttpServletRequest request) {
            
            ....
            
    return new VoteResponse(code, thumb);
}

원인

  • 반환하려는 응답 객체에 Getter가 없어서 생긴 일.
  • 객체를 JSON응답으로 만들 때 jackson은 객체의 Getter를 사용하기 때문에 getter가 반드시 있어야 한다.

해결

@Getter를 추가(또는 Getter코드 추가) 해서 해결하였다.

@Getter
@AllArgsConstructor
public class VoteResponse {
	...
}

[참고]

profile
안녕하세요

0개의 댓글