2013-07-29 45 views
0
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; 
    } 
} 

即時得到從數據庫表中的數據聽取併發送回頁面這樣如何,其由具有對象的列表中POJO轉換

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使用jackson

+0

檢查是否包含jackson庫,如果使用spring mvc,則您的dispatcher-servlet.xml文件具有。 –

+0

你我已經做了 – user2567005

+0

你在使用Maven嗎?你在你的項目類路徑中具有哪些JAR –

回答

1

我剛剛遇到了同樣的問題,它與循環引用有關。我懷疑你的問題是關係到循環引用在這裏:

public class Address { 

private Integer id;  
private Customer customer; 

有從AddressCustomer,我以爲是同一Customer參考在CustomerAddress舉行一個循環引用:

public class CustomerAddress { 
private Customer customer; 

以某種方式打破循環引用,它應該工作。

相關問題