我正在使用Spring MVC 3并尝试使用我的域对象来返回json响应。问题是由于双向关系和自我关系(员工报告给员工),我收到一个JsonMappingException:无限递归
我尝试使用@JsonIgnore但是spring / jackson仍尝试包含属性(Infinite Recursion with Jackson JSON and Hibernate JPA issue)
知道为什么@JsonIgnore
没有开始?
我知道传输对象或jsonviews是一种更好的方法,但我仍然希望在继续前进之前了解它。
Customer.java
@Entity
@NamedQueries({
@NamedQuery(name="Customer.findByName", query="from Customer cust where cust.customerName like :customerName"),
@NamedQuery(name="Customer.findByAccountNumber", query="from Customer cust where cust.accountNumber = :accountNumber"),
})
public class Customer implements DomainObject {
private Long id;
private String accountNumber;
private String customerName;
private CustomerDerived derived;
private Employee salesRep;
private Set<Order> orders = new HashSet<Order>();
@Id
@GeneratedValue
@Column(name="Id")
public Long getId() { return id; }
public void setId(Long id) { this.id=id; }
@Column(unique=true)
public String getAccountNumber() {return accountNumber;}
public void setAccountNumber(String accountNumber) {this.accountNumber = accountNumber; }
public String getCustomerName() { return customerName; }
public void setCustomerName(String customerName) { this.customerName = customerName;}
@JsonIgnore
@ManyToOne( fetch = FetchType.LAZY)
@JoinColumn(name="salesRepEmployeeId")
public Employee getSalesRep() { return salesRep; }
public void setSalesRep(Employee salesRep) {this.salesRep = salesRep;}
@JsonIgnore
@OneToMany(mappedBy="customer", fetch = FetchType.LAZY)
public Set<Order> getOrders() { return orders; }
public void setOrders(Set<Order> orders) {this.orders = orders; }
@OneToOne
@PrimaryKeyJoinColumn(name="customerId")
public CustomerDerived getDerived() {return derived;}
public void setDerived(CustomerDerived derived) {this.derived = derived;}
}
TestController.java
@Controller
@RequestMapping(value="/test")
public class TestController {
private CustomerDao customerDao;
@Autowired
public TestController(CustomerDao customerDao){
this.customerDao=customerDao;
}
@RequestMapping(value="/getAll", method=RequestMethod.GET)
public String getAll(Model model){
List<Customer> customers = customerDao.findAll();
model.addAttribute("customers", customers);
return "testresult";
}
@RequestMapping(value="/searchByName/{name}", method=RequestMethod.GET )
public @ResponseBody List<Customer> search(@PathVariable String name){
List<Customer> customers = customerDao.findByName(name);
System.out.println("got customers");
return customers;
}
}
堆栈跟踪
SEVERE: Servlet.service() for servlet orderWeb threw exception
org.codehaus.jackson.map.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: com.mike.orderapp.domain.Office["employees"]->org.hibernate.collection.PersistentSet[0]->com.mike.orderapp.domain.Employee_$$_javassist_0["office"]->com.mike.orderapp.domain.Office["employees"]->org.hibernate.collection.PersistentSet[0]->com.mike.orderapp.domain.Employee_$$_javassist_0["office"]->com.mike.orderapp.domain.Office["employees"]->org.hibernate.collection.PersistentSet[0]-
......
>com.mike.orderapp.domain.Employee_$$_javassist_0["office"]->com.mike.orderapp.domain.Office["employees"])
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)
答案 0 :(得分:1)
根据您的异常,看起来循环引用来自您的Office类,此类列表未在此处列出。
我真的会考虑创建那些传输对象。除了使您的JPA模型复杂化之外,您还要将您的域与非常具体的非域类集合耦合。
如果您想更改JSON解析实现,则必须更改域对象。
答案 1 :(得分:0)
使用@jsonManagedRefrence和@JsonBackRefrence, 我的问题由此解决了。