2011-12-10 66 views
0

我想在Spring REST風格的web服務中使用JAXB。 我的代碼如下:JAXB在SpringREST中使用返回域對象的ArrayList時出錯

@RequestMapping(value = "/countries", 
     method = RequestMethod.GET, 
     headers="Accept=application/xml, application/json") 
public @ResponseBody CountryList getCountry() { 
    logger.debug("Provider has received request to get all persons"); 

    // Call service here 
    CountryList result = new CountryList(); 
    result.setData(countryService.getAll()); 

    return result; 
} 

的CountryList.java類的樣子:

@XmlRootElement(name="countries") 
public class CountryList { 

@XmlElement(required = true) 
public List<Country> data; 

@XmlElement(required = false) 
public List<Country> getData() { 
    return data; 
} 

public void setData(List<Country> data) { 
    this.data = data; 
} 
} 

的Country.java樣子:

@XmlRootElement(name="country") 
public class Country { 

    private Calendar createdDt; 
    private String updatedBy; 
    private String createdBy; 
    private Long id; 
    private String countryName; 
    private Calendar updatedDt; 

// getters and setters for all attributes goes here 

} 

現在,當我訪問方法getCountry(),我得到以下異常

Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions 
Class has two properties of the same name "data" 
    this problem is related to the following location: 
     at public java.util.List com.cisco.bic.services.model.CountryList.getData() 
     at com.cisco.bic.services.model.CountryList 
    this problem is related to the following location: 
     at public java.util.List com.cisco.bic.services.model.CountryList.data 
     at com.cisco.bic.services.model.CountryList 

會有人知道爲什麼會出現這個錯誤。我在註釋部分做了什麼錯誤?

請幫忙。

問候 Saroj

+0

我們能否看到您的彈簧配置? –

+0

我發現這篇文章在過去非常有用:http://java-diaries.blogspot.com/2011/02/restful-webservices-with-spring.html –

+0

嗨,亞歷克斯,謝謝你的回覆。我已經按照鏈接http://java-diaries.blogspot.com/2011/02/restful-webservices-with-spring.html中給出的示例更改了註釋,並且它工作得非常好。 – user1061771

回答

0

不能標註兩個的getter/setter和領域,你需要在其中的一個決定。

相關問題