Avoid Jackson serialization on non fetched lazy objects

前端 未结 14 2001
终归单人心
终归单人心 2020-11-22 13:02

I have a simple controller that return a User object, this user have a attribute coordinates that have the hibernate property FetchType.LAZY.

When I try to get this

14条回答
  •  醉梦人生
    2020-11-22 13:28

    This is similar to accepted solution by @rick.

    If you don't want to touch existing message converters configuration you can just declare a Jackson2ObjectMapperBuilder bean like:

    @Bean
    public Jackson2ObjectMapperBuilder configureObjectMapper() {
        return new Jackson2ObjectMapperBuilder()
            .modulesToInstall(Hibernate4Module.class);
    }
    

    Do not forget to add the following dependency to your Gradle file (or Maven):

    compile 'com.fasterxml.jackson.datatype:jackson-datatype-hibernate4:2.4.4'
    

    Useful if you have a spring-boot application and you want to keep the ability to modify Jackson features from application.properties file.

提交回复
热议问题