我有以下简单的 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>
?