[英]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.