【问题标题】:jackson serialize lazy object HttpMessageNotWritableException & LazyInitializationExceptionjackson 序列化惰性对象 HttpMessageNotWritableException & LazyInitializationException
【发布时间】:2017-06-19 16:43:10
【问题描述】:

这是一个后续问题:

Spring Hibernate FetchType LazyInitializationException even when not calling association

我已尝试实施此解决方案,但没有成功。我想知道我是否在某些地方犯了错误......

Avoid Jackson serialization on non fetched lazy objects

applicationContext.xml

<context:annotation-config />
<context:component-scan base-package="com.app"></context:component-scan>

<tx:annotation-driven />

<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="objectMapper">
                <bean class="com.app.service.HibernateAwareObjectMapper" />
            </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

<!-- Hibernate server settings -->

HibernateAwareObjectMapper

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

public class HibernateAwareObjectMapper extends ObjectMapper {

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

pom.xml

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>4.3.11.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>4.3.2.Final</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.8.6</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.8.6</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.8.6</version>
</dependency>

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

</dependency>

我仍然收到以下错误

Hibernate: select this_.person_id as person_i1_1_0_, this_.age as age2_1_0_, this_.name as name3_1_0_ from person this_
[Person [person_id=1, name=eric, age=11]]
Jun 20, 2017 12:37:29 AM org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver handleHttpMessageNotWritable
WARNING: Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: failed to lazily initialize a collection of role: com.app.person.Person.phones, could not initialize proxy - no Session (through reference chain: java.util.HashMap["results"]->java.util.ArrayList[0]->com.app.person.Person["phones"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException:

非常感谢任何见解或建议...

最后一件事,我不想将它标记为 JsonIgnore,因为它永远不会被序列化。有时我需要检索 Lazy 对象。

谢谢

【问题讨论】:

    标签: java spring hibernate jackson


    【解决方案1】:

    解决这个问题有两种方法。第一个是如果您在此请求中不需要此信息,请使用 @JsonIgnore 忽略电话。 第二种方法是在关闭交易之前加载手机。为此,您可以使用Hibernate.initialize(person.getPhones())。或者您可以在集合上使用@OneToMany(fetch = FetchType.EAGER) 获取此集合。

    【讨论】:

    • 不能做 Eager,因为我不希望手机一直在取。不能做@JsonIgnore,因为我需要时间来检索惰性对象。不需要电话对象,这就是我使用 Lazy 的原因....感谢您的尝试
    • 你可以在需要加载手机时使用 Hibernate.initialize(person.getPhones())。
    • 我不想只加载手机
    • 所以使用@JsonIgnore注解
    猜你喜欢
    • 2014-11-12
    • 2014-03-09
    • 2017-10-24
    • 1970-01-01
    • 2016-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多