美文网首页
hibernate对象在Jackson中序列化失败

hibernate对象在Jackson中序列化失败

作者: 田文健 | 来源:发表于2019-04-26 15:45 被阅读0次

hibernate的实体对象是代理过的,应为有些懒加载过程,当用Jackson序列化时会失败,这时候可以在依赖中加入

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

如过是自定义的objectMapping,那么需要

ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

        //解决hibernate代理对象无法序列化的问题
        om.registerModule(new Hibernate5Module());

相关文章

网友评论

      本文标题:hibernate对象在Jackson中序列化失败

      本文链接:https://www.haomeiwen.com/subject/mcbfnqtx.html