JaxB无法编组,说缺少RootElement但有一个

时间:2012-08-03 03:56:08

标签: java annotations jaxb marshalling

我已经看过围绕这个问题存在的帖子,但由于我是jaxB的新手,我遇到了一些麻烦,我设置了Root元素并声明了我的XML元素。我在这做错了什么?

我收到此错误

org.springframework.oxm.MarshallingFailureException: JAXB marshalling exception; nested exception is javax.xml.bind.MarshalException
 - with linked exception:
[com.sun.istack.internal.SAXException2: unable to marshal type "au.test.Search.ws.model.SearchRequest" as an element because it is missing an @XmlRootElement annotation]

然而,这是我的SearchRequest类

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
        "searchControls",
        "searchCriteria",
        "searchFilters"
})
@XmlRootElement(name = "searchRequest")
public class SearchRequest {

    @XmlElement(required = true)
    protected SearchControls searchControls;
    @XmlElement(required = true)
    protected NameSearchCriteria searchCriteria;
    @XmlElement
    protected NameSearchFilters searchFilters;

    public SearchControls getSearchControls() {
        return searchControls;
    }

    public void setSearchControls(SearchControls value) {
        this.searchControls = value;
    }

    public NameSearchCriteria getSearchCriteria() {
        return searchCriteria;
    }

    public void setSearchCriteria(NameSearchCriteria value) {
        this.searchCriteria = value;
    }

    public NameSearchFilters getSearchFilters() {
        return searchFilters;
    }

    public void setSearchFilters(NameSearchFilters value) {
        this.searchFilters = value;
    }
}

如何设置searchControls,条件和过滤器

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "NameSearchFilters", propOrder = {

})
public class NameSearchFilters {

    protected FilterOperator operator;

    public FilterOperator getOperator() {
        return operator;
    }

    public void setOperator(FilterOperator value) {
        this.operator = value;
    }
}

如何使用org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive

按要求进行解组
public SearchResponse performSearch(SearchRequest searchRequest) {
    searchResponse = (SearchResponse) getWebServiceTemplate()
            .marshalSendAndReceive(searchRequest);            

1 个答案:

答案 0 :(得分:0)

这里的问题是Ant构建创建依赖jar是一个旧版本,需要重做ant构建并修复所有错误。 我正在将Castor转换为Jaxb,因此元素的设置是正确的,因此可以用作参考点。

对于遇到此问题并且不是依赖问题的人来说,问题的最可能原因在于您的编组。