Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: No acceptable representation]
에러 발생
- 장바구니 조회 기능을 만드는 와중에 처음보는 406 에러가 발생
- 서비스의 로직을 잘못 작성한 것인지 의심을 하였지만, Service 로직 종료 후 Controller에 반환된 Dto를 출력했을 때 정상적으로 나옴
- return을 하는 과정에서 어떠한 에러가 발생한 것으로 의심
@GetMapping("/list/{memberId}")
public ResponseEntity<WishlistResponseDto> list(@PathVariable(name = "memberId") Long memberId,
@RequestParam(name = "pageNo", defaultValue = "1") int pageNo,
@RequestParam(name = "size",defaultValue = "10")int size){
WishlistResponseDto wishlistResponseDto = wishlistService.list(memberId, pageNo - 1, size);
System.out.println(wishlistResponseDto);
return wishlistResponseDto != null ? ResponseEntity.status(HttpStatus.OK).body(wishlistResponseDto) :
ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
발생 원인
- 구글링 결과 HttpMediaTypeNotAcceptableException는 클라이언트의 Accept 헤더와 서버의 응답 형식이 일치하지 않을 때 발생한다고 함
- 즉, [클라이언트의 요청 타입 != 서버 반환 미디어 타입]이 일치하지 않았기에 발생했다는 의미로 해석
- WishlistController에는 @RestController가 선언이 되어있고, 모든 메서드는 @ResponseBody로 응답을 하게 됨
- @RestController는 JSON 형식으로 반환하려고 시도하지만 wishlistResponseDto에 Getter가 없기 때문에 Jackson이 JSON을 직렬화 할 수 없게 되었을 때 해당 예외가 발생
해결 방법
- Getter 추가 시 해결 완료
@NoArgsConstructor
@AllArgsConstructor
@Getter
public class WishlistResponseDto {
private Long wishlistId;
private int totalPrice;
private List<GameInfo> wishlist;
}
'Various Error' 카테고리의 다른 글
[IntelliJ / FeignClient] 의존성 추가 후 어노테이션 인식 불가 (0) | 2024.08.22 |
---|---|
[Eureka] Eureka Client의 UnsatisfiedDependencyException 에러 (0) | 2024.08.20 |
[JSON] InvalidDefinitionException (0) | 2024.08.19 |
[Spring Data JPA] PropertyReferenceException 에러 (0) | 2024.08.16 |
[Lombok] @Getter 어노테이션 에러 (0) | 2024.08.09 |