我有以下简单的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>
?
答案 0 :(得分:3)
感谢@PaulBellora的帮助,base和extend类都会变得抽象,然后有一个Name implimentation,就像这样:
<强>基强>
@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;
}
}
答案 1 :(得分:1)
我不熟悉JAXB,但您可以尝试制作getSearchResults
和setSearchResults
抽象方法,并仅在解析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;
}
...
}