Skip to content

Instantly share code, notes, and snippets.

@syakuis
Last active May 17, 2019 14:15
Show Gist options
  • Save syakuis/3ff066c040d21b18daa62857bbf937e9 to your computer and use it in GitHub Desktop.
Save syakuis/3ff066c040d21b18daa62857bbf937e9 to your computer and use it in GitHub Desktop.
Spring data jpa 밴더 Hibernate 5 사용시 List 결과를 Spring MVC Jackson json 으로 직렬화하지 못하는 경우

1차 오류

org.springframework.orm.jpa.JpaSystemException: HHH000142: Bytecode enhancement failed: .LocalWebUserEntity; nested exception is org.hibernate.HibernateException: HHH000142: Bytecode enhancement failed: .web.localweb.domain.LocalWebUserEntity

원인: constructor 가 private 여서 발생한 문제.

해결

@NoArgsConstructor(access = AccessLevel.PRIVATE)

to

@NoArgsConstructor(access = AccessLevel.PROTECTED)

2차 오류

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.ArrayList[0]->.localweb.domain.LocalWebUserEntity$HibernateProxy$gpLnupz6["$$_hibernate_interceptor"])

원인: 단반향으로 주인이 지연 처리로 설정되어 있는 경우로 Jackson 에서 직렬화 하지못하는 걸로 판단

해결

jackson 기본 라이브러리에서 해결하지 못해 플러그인 사용

https://github.com/FasterXML/jackson-datatype-hibernate

public class HibernateAwareObjectMapper extends ObjectMapper {
    public HibernateAwareObjectMapper() {
        registerModule(new Hibernate5Module());
    }
}

@Bean
public HttpMessageConverter<Object> createJsonHttpMessageConverter() {
    MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
    jackson2HttpMessageConverter.setObjectMapper(new HibernateAwareObjectMapper());
    return jackson2HttpMessageConverter;
}

참고 : https://stackoverflow.com/questions/34334202/could-not-write-json-no-serializer-found-for-class-org-hibernate-proxy-pojo-jav

관련 해결법 전부 안됨.

@JsonIgnoreProperties(ignoreUnknown = true)

@JsonBackReference

@JsonManagedReference

@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})

spring.jackson.serialization.fail-on-empty-beans=false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment