본문 바로가기
삽질

FAIL_ON_EMPTY_BEANS 에러

by mozzi329 2022. 8. 30.
728x90
Type definition error: 
[simple type, class com.codestates.order.dto.OrderCoffeeResponseDto]; 
nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: 
No serializer found for class com.codestates.order.dto.OrderCoffeeResponseDto and no properties discovered to create BeanSerializer 
(to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)

OrderCoffeeResponseDto를 직렬화하기 위한 프로퍼티가 존재하지 않아 FAIL_ON_EMPTY_BEANS 에러가 발생했다. 해당 에러에 대해 구글링을 해보았다.

 

방안 1, 접근 제어자 변경

private를 public 접근 제어자로 변경하면 된다고 하는데 dto 필드를 public 접근 제어자로 바꾼다는 건 이상하다고 생각해 해당 방법은 pass 했다.

 

방안 2, @JsonIgnoreProperties(ignoreUnknown=true)

@JsonIgnoreProperties(ignoreUnknown=true)

필드 위에 해당 주석을 달아 해당 필드가 비어있더라도 무시하는 옵션이다. 근데 이것도 특정 필드만 문제가 있는 게 아니라 빈 자체를 만들 수 없기 때문에 pass 했다.

 

방안 3, @Getter 애너테이션 누락..........ㅎ

import lombok.AllArgsConstructor;

@AllArgsConstructor
public class OrderCoffeeResponseDto {
    private long coffeeId;
    private String korName;
    private String engName;
    private int price;
    private int quantity;
}

결국에는 내 실수였다. Dto 클래스인데 @Getter 애너테이션을 빠트렸다. Dto를 정리하면서 @Getter는 무조건 필요하다고 정리까지 해놨는데 그새 까먹었다... 사실 유닛에서도 누락이 되어있어서 아마 생각없이 따라 치다가 에러가 발생한 것 같다. 앞으로는 좀 더 생각하면서 코드를 작성해야겠다. 

 

해결

import lombok.AllArgsConstructor;
import lombok.Getter;

@AllArgsConstructor
@Getter
public class OrderCoffeeResponseDto {
    private long coffeeId;
    private String korName;
    private String engName;
    private int price;
    private int quantity;
}

다음과 같이 @Getter 애너테이션을 추가하니 주문 조회가 잘된다.

 

 

굿

댓글