配置Jackson在Spring Boot中省略延迟加载属性 [英] Configure Jackson to omit lazy-loading attributes in Spring Boot

查看:466
本文介绍了配置Jackson在Spring Boot中省略延迟加载属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您正在使用SpringBoot,理想情况下,您应该已经拥有 Hibernate4Module
否则添加这个依赖关系

 < dependency> 
< groupId> com.fasterxml.jackson.datatype< / groupId>
< artifactId> jackson-datatype-hibernate4< / artifactId>
< version> 2.5.3< / version>
< /依赖关系>

接下来创建一个名为HibernateAwareObjectMapper的类或者任何你想命名的类:



包含以下内容:

  import com.fasterxml.jackson.databind.ObjectMapper; 
import com.fasterxml.jackson.datatype.hibernate4.Hibernate4Module;
$ b $ public class HibernateAwareObjectMapper extends ObjectMapper {

public HibernateAwareObjectMapper(){
registerModule(new Hibernate4Module());
}
}

对于不同版本的Hibernate,参考这些Hibernate模块:

  //用于Hibernate 4.x:
mapper.registerModule(new Hibernate4Module());
//或者,对于Hibernate 5.x
mapper.registerModule(new Hibernate5Module());
//或者,对于Hibernate 3.6
mapper.registerModule(new Hibernate3Module());

现在您需要通过消息Converter注册您的 HibernateAwareObjectMapper 。为此,创建一个扩展扩展WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter的Config类。 (如果你已经有一个只是下一步)。

现在使用HibernateObjectMapper注册MessageConverter:

  @Override 
public void configureMessageConverters(List< HttpMessageConverter<>>转换器){
List< MediaType> supportedMediaTypes = new ArrayList<>();
supportedMediaTypes.add(MediaType.APPLICATION_JSON);
supportedMediaTypes.add(MediaType.TEXT_PLAIN);
MappingJackson2HttpMessageConverter转换器= new MappingJackson2HttpMessageConverter();
converter.setObjectMapper(new HibernateAwareObjectMapper());
converter.setPrettyPrint(true);
converter.setSupportedMediaTypes(supportedMediaTypes);
converters.add(转换器);
super.configureMessageConverters(转换器);
}

Viola !!!这应该够了。
这是用于Spring Boot Web应用程序的纯java(no-xml)方法。



如果您想添加,请随时发表评论回答。


In spring boot mvc project with pure java configuration how to configure Jackson to omit lazy load Attributes

解决方案

If you are using SpringBoot, ideally you should already have Hibernate4Module. Else add this dependency

    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-hibernate4</artifactId>
        <version>2.5.3</version>
    </dependency>

Next create a class called "HibernateAwareObjectMapper" or whatever you want to name it:

with following contents:

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.hibernate4.Hibernate4Module;

    public class HibernateAwareObjectMapper extends ObjectMapper {

        public HibernateAwareObjectMapper() {
            registerModule(new Hibernate4Module());
        }
    }

for different versions of Hibernate, refer to these Hibernate modules:

// for Hibernate 4.x:
mapper.registerModule(new Hibernate4Module());
// or, for Hibernate 5.x
mapper.registerModule(new Hibernate5Module());
// or, for Hibernate 3.6
mapper.registerModule(new Hibernate3Module());

Now you need to register your HibernateAwareObjectMapper through a message Converter. For this create a Config class that extens extends WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter. (If you already have one just follow the next step).

Now register the MessageConverter using HibernateObjectMapper :

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters){
    List<MediaType> supportedMediaTypes=new ArrayList<>();
    supportedMediaTypes.add(MediaType.APPLICATION_JSON);
    supportedMediaTypes.add(MediaType.TEXT_PLAIN);
    MappingJackson2HttpMessageConverter converter=new MappingJackson2HttpMessageConverter();
    converter.setObjectMapper(new HibernateAwareObjectMapper());
    converter.setPrettyPrint(true);
    converter.setSupportedMediaTypes(supportedMediaTypes);
    converters.add(converter);
    super.configureMessageConverters(converters);
}

Viola !!! That should be enough. This is the pure-java (no-xml) way of doing this for a spring boot web app.

Feel free to comment if you want to add to Answer.

这篇关于配置Jackson在Spring Boot中省略延迟加载属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆