我有以下简单的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>
吗? 最佳答案
感谢@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;
}
}