gpt4 book ai didi

java - Spring MVC、域对象和@JsonIgnore

转载 作者:行者123 更新时间:2023-11-30 11:46:41 25 4
gpt4 key购买 nike

我正在使用 Spring MVC 3 并尝试使用我的域对象返回 json 响应。问题是由于双向关系和 self 关系(员工向员工报告),我收到 JsonMappingException:无限递归

我尝试使用@JsonIgnore,但 spring/jackson 仍然尝试包含属性(Infinite Recursion with Jackson JSON and Hibernate JPA issue)

知道为什么 @JsonIgnore 没有启动吗?

我知道传输对象或 jsonviews 是更好的方法,但我仍然想在继续之前深入了解这一点。

客户.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;}

}

测试 Controller .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)

最佳答案

根据您的异常情况,循环引用似乎来自您的 Office 类,但未在此处列出。

我真的会考虑创建那些传输对象。除了使您的 JPA 模型复杂化之外,您还将您的域耦合到一组非常具体的非域类。

如果您想更改 JSON 解析实现,则必须更改域对象。

关于java - Spring MVC、域对象和@JsonIgnore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9657492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com