简体   繁体   中英

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;
    }
}

im getting the data from the DB Form hear and send it back to the page like this

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);

This is my method which return a 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;
}

Error that i'm getting

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)

can any body show me how to convert this "CustomerAddress" class in to json using jackson

I just had the same problem and it was related to a circular reference. I suspect your problem is related to the circular reference here:

public class Address {

private Integer id;    
private Customer customer;

There is a circular reference from the Address back to the Customer , which I assume is the same Customer reference held at CustomerAddress :

public class CustomerAddress {
private Customer customer;

Break the circular reference somehow and it should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM