带有 codehaus jackson 休息服务的 glassfish jersey 中的 Hibernate 惰性初始化器问题
Hibernate lazy initilizer issue in glassfish jersy with codehus jackson rest service
我正在使用 codehus-Jackson 为 json 数据开发 glashfish 球衣休息服务。
但是我得到了 Hibernate 惰性初始化异常,因为我为所有子 类 添加了 lazy=false。但它给出了无限递归错误。所以我在反向映射属性上添加了 JsonIgnore。但它没有检测到。我的配置如下。
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.koderzlab.lawman;org.codehaus.jackson.jaxrs</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
我的堆栈跟踪如下
at org.codehaus.jackson.map.ser.BeanSerializer.serializeFields(BeanSerializer.java:189)
at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:142)
at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:430)
at org.codehaus.jackson.map.ser.BeanSerializer.serializeFields(BeanSerializer.java:175)
at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:142)
at org.codehaus.jackson.map.ser.ContainerSerializers$CollectionSerializer.serializeContents(ContainerSerializers.java:442)
at org.codehaus.jackson.map.ser.ContainerSerializers$CollectionSerializer.serializeContents(ContainerSerializers.java:383)
at org.codehaus.jackson.map.ser.ContainerSerializers$AsArraySerializer.serialize(ContainerSerializers.java:142)
at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:430)
at org.codehaus.jackson.map.ser.BeanSerializer.serializeFields(BeanSerializer.java:175)
at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:142)
at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:430)
at org.codehaus.jackson.map.ser.BeanSerializer.serializeFields(BeanSerializer.java:175)
at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:142)
at org.codehaus.jackson.map.ser.ContainerSerializers$CollectionSerializer.serializeContents(ContainerSerializers.java:442)
at org.codehaus.jackson.map.ser.ContainerSerializers$CollectionSerializer.serializeContents(ContainerSerializers.java:383)
虽然您的 Hibernate 问题可能不会因此得到解决,但看起来您正在扫描错误的 Jackson 包名称 类。
For Jackson 2.x,一般搭配Jersey 2.x,需要扫入包 com.fasterxml.jackson.jaxrs
而不是 org.codehaus.jackson.jaxrs
.
将 web.xml
中的部分更改为以下内容:
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.koderzlab.lawman;com.fasterxml.jackson.jaxrs</param-value>
</init-param>
另请参阅:
- Jersey JAX-RS, Hibernate and LazyInitializationException
- Hibernate org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role:
- LazyInitializationException in Hibernate and Jersey
我刚刚添加了 codehusjackson 1.9.2 jar,现在它可以识别 @JsonBack 引用。但是在每个 pojo class 我们需要添加
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
我正在使用 codehus-Jackson 为 json 数据开发 glashfish 球衣休息服务。 但是我得到了 Hibernate 惰性初始化异常,因为我为所有子 类 添加了 lazy=false。但它给出了无限递归错误。所以我在反向映射属性上添加了 JsonIgnore。但它没有检测到。我的配置如下。
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.koderzlab.lawman;org.codehaus.jackson.jaxrs</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
我的堆栈跟踪如下
at org.codehaus.jackson.map.ser.BeanSerializer.serializeFields(BeanSerializer.java:189)
at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:142)
at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:430)
at org.codehaus.jackson.map.ser.BeanSerializer.serializeFields(BeanSerializer.java:175)
at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:142)
at org.codehaus.jackson.map.ser.ContainerSerializers$CollectionSerializer.serializeContents(ContainerSerializers.java:442)
at org.codehaus.jackson.map.ser.ContainerSerializers$CollectionSerializer.serializeContents(ContainerSerializers.java:383)
at org.codehaus.jackson.map.ser.ContainerSerializers$AsArraySerializer.serialize(ContainerSerializers.java:142)
at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:430)
at org.codehaus.jackson.map.ser.BeanSerializer.serializeFields(BeanSerializer.java:175)
at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:142)
at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:430)
at org.codehaus.jackson.map.ser.BeanSerializer.serializeFields(BeanSerializer.java:175)
at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:142)
at org.codehaus.jackson.map.ser.ContainerSerializers$CollectionSerializer.serializeContents(ContainerSerializers.java:442)
at org.codehaus.jackson.map.ser.ContainerSerializers$CollectionSerializer.serializeContents(ContainerSerializers.java:383)
虽然您的 Hibernate 问题可能不会因此得到解决,但看起来您正在扫描错误的 Jackson 包名称 类。
For Jackson 2.x,一般搭配Jersey 2.x,需要扫入包 com.fasterxml.jackson.jaxrs
而不是 org.codehaus.jackson.jaxrs
.
将 web.xml
中的部分更改为以下内容:
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.koderzlab.lawman;com.fasterxml.jackson.jaxrs</param-value>
</init-param>
另请参阅:
- Jersey JAX-RS, Hibernate and LazyInitializationException
- Hibernate org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role:
- LazyInitializationException in Hibernate and Jersey
我刚刚添加了 codehusjackson 1.9.2 jar,现在它可以识别 @JsonBack 引用。但是在每个 pojo class 我们需要添加
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})