escape-room

InvalidDefinitionException - No serializer found for class 에러

기매_ 2023. 5. 23. 17:45
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class com.maemae.escaperoom.dto.ThemeListDTO and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.maemae.escaperoom.controller.ThemeController$Result["data"]->org.springframework.data.domain.PageImpl["content"]->java.util.Collections$UnmodifiableRandomAccessList[0])

 

위와 같은 에러가 발생하였다.


구글링을 해보니

스프링 부트에서는 기본적으로 Jackson 라이브러리가 탑재되어있는데 - Serialize 하는 과정에서 접근 제한자가 public이거나 Getter/Setter를 이용하기 때문에, 인스턴스 필드가 private으로 선언되어있으면 JSON 변환 과정에서 문제가 발생하는 것이였다.

 

여기까지 읽고 혹시나 해서 DTO 파일을 다시 봤는데

 적어놓은 줄 알았던 Lombok 어노테이션이 빠져있었다.

 

@Getter
public class ThemeListDTO {

@Getter 를 추가하여 문제 해결 !

 


나는 비교적 간단하게 해결했지만, 혹시 이 방법으로 해결되지 않는다면

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

위 글을 참고하여 @JsonProperty, @JsonAutoDetect, setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY) 등으로 해결해보자 .. !