JAXB在SpringREST中使用返回域对象的ArrayList时出错

时间:2011-12-10 10:19:49

标签: spring annotations jaxb

我正在尝试在Spring RESTful webservice中使用JAXB。 我的代码如下:

 @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

1 个答案:

答案 0 :(得分:0)

你无法注释getter / setter和字段,你需要决定其中一个。