Framework/Spring Boot

[Error] com.fasterxml.jackson.databind.exc.InvalidDefinitionException

잔망루피 2023. 2. 1. 19:30
반응형

⭐ 상황

@Builder
public class CommentResponse {
    private String id;

    private String username;

    private String content;

    private Like like;

    private Instant createdAt;
}
List<CommentResponse> responseList = commentRepository.findAllByPostId(post_id).stream()
        .map(comment -> CommentResponse.builder()
                .id(comment.getId())
                .username(comment.getUsername())
                .content(comment.getContent())
                .like(comment.getLike())
                .createdAt(comment.getCreatedAt())
                .build())
        .collect(Collectors.toList());

Comment 객체를 DTO인 CommentResponse로 변환하는 코드

 

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class com.postservice.dto.response.CommentResponse and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.postservice.dto.response.Response["result"]->java.util.ArrayList[0])

CommentResponse의 필드가 다 private로 설정되어있다.

접근 할 수 없으니까 문제가 생긴다.

 

⭐ 해결

DTO에 lombok의 @Getter, @Setter 어노테이션을 추가했다.

 

 

참고 👇

https://steady-hello.tistory.com/90

 

[jackson, issue] 이슈 해결 - No serializer found for class

어떤 이슈? 급하게 테스트해볼 이슈가 있어서 간단하게 객체를 만들고 이 객체를 json string으로 변환해야 할 일이 있었습니다. 그래서 테스트용 class를 생성하고 이를 ObjectMapper의 writeValueAsString()

steady-hello.tistory.com

 

 

 

반응형