[英]How to convert the POJO which consist with list of objects
public class CustomerAddress {
private Customer customer;
//I think the problem is in hear becouse jackson does not know how to serialize this object list
private List<Address> address;
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public List<Address> getAddress() {
return address;
}
public void setAddress(List<Address> address) {
this.address = address;
}
}
public class Address{
private Integer id;
private Customer customer;
private AddressType addressType;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public AddressType getAddressType() {
return addressType;
}
public void setAddressType(AddressType addressType) {
this.addressType = addressType;
}
}
public class Customer {
private Integer id;
private String firstName;
private String middleName;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getMiddleName() {
return middleName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
}
我從數據庫表單中獲取數據,然后像這樣將其發送回頁面
CustomerAddress customerAddress = customerAddressService.getCustomerAddress(22);
Map<String, Object> map = getMapCustomerAddress(customerAddress);
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
return mapper.writeValueAsString(map);
這是我的返回地圖的方法
private Map<String, Object> getMapCustomerAddress(CustomerAddress customerAddress) throws IOException {
Map<String, Object> modelMap = new HashMap<String, Object>(3);
modelMap.put("total", 1);
modelMap.put("data", customerAddress);
modelMap.put("success", true);
return modelMap;
}
我得到的錯誤
java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/JsonMappingException$Reference
com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:613)
com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:142)
有誰可以向我展示如何使用傑克遜將該“ CustomerAddress”類轉換為json?
我只是遇到了同樣的問題,它與循環引用有關。 我懷疑您的問題與此處的循環引用有關:
public class Address {
private Integer id;
private Customer customer;
從Address
到Customer
都有一個循環引用,我認為與在CustomerAddress
持有的相同Customer
引用相同:
public class CustomerAddress {
private Customer customer;
以某種方式破壞循環參考,它應該起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.