0

我有以下简单的 jaxB 类,它采用泛型 E

@XmlAccessorType(XmlAccessType.FIELD)
@XmlTransient
@XmlRootElement(name = "searchResponseBase")
public abstract class SearchResponseBase<E>{

    @XmlElement(type=NameSearchResults.class)
    protected E searchResults;

    public E getSearchResults()
    {
        return searchResults;
    }

    public void setSearchResults(E mSearchResults)
    {
        this.searchResults = mSearchResults;
    }

}

我需要删除对 NameSearchResults 的引用以@XmlElement(type=NameSearchResults.class)使基础实际上是通用的,但如果我这样做了,我会得到错误。

错误

[com.sun.istack.internal.SAXException2: class au.test.nameSearch.NameSearchResults nor any of its super class is known to this context.
javax.xml.bind.JAXBException: class au.test.nameSearch.NameSearchResults nor any of its super class is known to this context.]


这是一个扩展它的类的例子

扩展类

@SuppressWarnings("javadoc")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = {
    "searchRequest",
    "searchResults"
})
@XmlRootElement(name = "searchResponse")
public class SearchResponse extends SearchResponseBase<NameSearchResults> {

    @XmlElement(required = true)
    protected SearchRequest searchRequest;

    public SearchRequest getSearchRequest() {
        return searchRequest;
    }

    public void setSearchRequest(SearchRequest value) {
        this.searchRequest = value;
    }
}

我如何使基类真正通用?

最好我希望我的扩展类以这种格式工作SearchResponse<E> extends SearchResponseBase<E>并将其也用作泛型类型。

如果我按照保罗的建议去做,我可以让课程:

@XmlRootElement(name = "searchResponse")
public class SearchResponse<E extends NameSearchResults> extends SearchResponseBase<E> {

    @XmlElement(required = true)
    protected SearchRequest searchRequest;

    protected E searchResults;

    public SearchRequest getSearchRequest() {
        return searchRequest;
    }

    public void setSearchRequest(SearchRequest value) {
        this.searchRequest = value;
    }

    @Override
    public E getSearchResults() {
        return searchResults;
    }

    @Override
    public void setSearchResults(E mSearchResults) {
        this.searchResults = mSearchResults;
    }  
}

有没有办法可以将 NameSearchResults 排除在外<E extends NameSearchResults>

4

2 回答 2

3

感谢@PaulBellora 的帮助,基类和扩展类都将变为抽象类,然后具有名称实现,如下所示:

根据

@XmlRootElement(name = "searchResponseBase")
public abstract class SearchResponseBase<E>{

    public abstract E getSearchResults();

    public abstract void setSearchResults(E mSearchResults);

}

扩展底座

@XmlRootElement(name = "searchResponse")
public abstract class SearchResponse<E> extends SearchResponseBase<E>{

    public abstract SearchRequest getSearchRequest();

    public abstract void setSearchRequest(SearchRequest value);   
}

名称含义

@XmlRootElement(name = "nameSearchResponse")
public class NameSearchResponse extends SearchResponse<NameSearchResults>{

    @XmlElement(required = true)
    protected SearchRequest searchRequest;

    protected NameSearchResults searchResults;

    @Override
    public NameSearchResults getSearchResults() {
        return searchResults;
    }

    @Override
    public void setSearchResults(NameSearchResults mSearchResults) {
        this.searchResults = mSearchResults;
    }

    @Override
    public SearchRequest getSearchRequest() {
        return searchRequest;
    }

    @Override
    public void setSearchRequest(SearchRequest value) {
        this.searchRequest = value;
    }
}
于 2012-11-06T23:24:47.420 回答
1

我对 JAXB 不熟悉,但是您可以尝试制作getSearchResultssetSearchResults抽象方法,并仅在E解决后才实现它们。例如:

//annotations ommitted
public abstract class SearchResponseBase<E>{

    public abstract E getSearchResults();

    public abstract void setSearchResults(E mSearchResults);
}

//annotations ommitted
public class SearchResponse extends SearchResponseBase<NameSearchResults> {

    @XmlElement(type=NameSearchResults.class)
    protected NameSearchResults searchResults;

    @Override
    public final NameSearchResults getSearchResults() {
        return searchResults;
    }

    @Override
    public final void setSearchResults(NameSearchResults mSearchResults) {
        this.searchResults = mSearchResults;
    }

    ...
}
于 2012-11-06T05:22:52.890 回答