我正在使用Spring Roo 1.2.1和Jackson 1.9.7。 在序列化我的类QueueOut与json我得到一个JsonMappingException。
我阅读了以下帖子,但找不到适合我的解决方案:
Infinite Recursion with Jackson
Jackson - serialization of entities with birectional relationships (avoiding cycles)
我不知道为什么JsonIgnore没有在QueueOuts属性的Queue类中工作。我也试过JsonManagedReference但是同样的错误。使用Jackson 2.0.0不是解决方案,因为Spring 3.1不支持它。
有什么建议吗?
这是我的stacktrace的一部分:
Caused by: org.springframework.amqp.support.converter.MessageConversionException: Failed to convert Message content
at org.springframework.amqp.support.converter.JsonMessageConverter.createMessage(JsonMessageConverter.java:181)
at org.springframework.amqp.support.converter.AbstractMessageConverter.toMessage(AbstractMessageConverter.java:51)
at org.springframework.amqp.rabbit.core.RabbitTemplate.convertMessageIfNecessary(RabbitTemplate.java:482)
at org.springframework.amqp.rabbit.core.RabbitTemplate.convertAndSend(RabbitTemplate.java:358)
at org.springframework.amqp.rabbit.core.RabbitTemplate.convertAndSend(RabbitTemplate.java:346)
at de.company.prs.admin.server.amqp.AdminQueueConsumer.onMessage(AdminQueueConsumer.java:52)
at org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter.onMessage(MessageListenerAdapter.java:328)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:505)
... 10 more
Caused by: org.codehaus.jackson.map.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: de.company.prs.admin.server.domain.Queue["queueOuts"]->org.hibernate.collection.PersistentSet[0]->de.company.prs.admin.server.domain.QueueOut["queue"]->de.company.prs.admin.server.domain.Queue["queueOuts"]->org.hibernate.collection.PersistentSet[0]->de.company.prs.admin.server.domain.QueueOut["queue"]->de.company.prs.admin.server.domain.Queue["queueOuts"]->org.hibernate.collection.PersistentSet[0]->de.company.prs.admin.server.domain.QueueOut["queue"]->de.company.prs.admin.server.domain.Queue["queueOuts"]->org.hibernate.collection.PersistentSet[0]->de.company.prs.admin.server.domain.QueueOut["queue"]->de.company.prs.admin.server.domain.Queue["queueOuts"]->org.hibernate.collection.PersistentSet[0]->de.company.prs.admin.server.domain.QueueOut["queue"]->de.company.prs.admin.server.domain.Queue["queueOuts"]->org.hibernate.collection.PersistentSet[0]->de.company.prs.admin.server.domain.QueueOut["queue"]->de.company.prs.admin.server.domain.Queue["queueOuts"]->org.hibernate.collection.PersistentSet[0]->de.company.prs.admin.server.domain.QueueOut["queue"]->de.company.prs.admin.server.domain.Queue["queueOuts"]->org.hibernate.collection.PersistentSet[0]
at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:164)
at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446)
at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150)
at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
这是我的课程: 第一
@RooJavaBean
@RooToString
public class QueueOut {
private String name;
private String description;
@JsonIgnore
@ManyToOne
private GenericQueue genericQueue;
@ManyToOne (fetch = FetchType.EAGER)
private Queue queue;
}
第二
@RooJavaBean
@RooToString
@RooJpaActiveRecord
public class Queue {
private String name;
private String description;
@JsonIgnore
@OneToMany(cascade = CascadeType.ALL, mappedBy = "queue", fetch = FetchType.EAGER)
private Set<QueueOut> QueueOuts = new HashSet<QueueOut>();
}
答案 0 :(得分:3)
你必须在你的getter上使用@JsonIgnore
答案 1 :(得分:1)
不要使用JsonIgnore,从1.9使用JsonManagedReference和JsonBackReference对有问题的属性的getter。您可以在以下位置看到两个很好的示例:
答案 2 :(得分:0)
在getter上使用@JsonIgnore使用@JsonIgnoreProperties完全忽略该属性(value = {“propertyName”})