小编典典

配置Jackson以省略Spring Boot中的延迟加载属性

spring-boot

在具有纯Java配置的Spring Boot MVC项目中,如何配置Jackson省略延迟加载属性


阅读 413

收藏
2020-05-30

共1个答案

小编典典

我使用springboot和hibernamte5.0.x。有用!

1. pom.xml

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-hibernate5</artifactId>
    <version>2.8.4</version>
</dependency>

2. Webconfig

@Configuration
public class WebConfig implements WebMvcConfigurer {

    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        for (HttpMessageConverter converter : converters) {
            if (converter instanceof org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) {
                ObjectMapper mapper = ((MappingJackson2HttpMessageConverter) converter).getObjectMapper();
                mapper.registerModule(new Hibernate5Module());
                // replace Hibernate4Module() with the proper class for your hibernate version.
            }
        }
    }
}
2020-05-30