gpt4 book ai didi

java - 创建抽象通用 jaxb 类

转载 作者:行者123 更新时间:2023-12-01 18:58:20 26 4
gpt4 key购买 nike

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


这是扩展它的类的示例

扩展类(class)

@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> 的格式工作。并将其用作泛型类型。

如果我按照保罗的建议去做,我可以让类(class)达到:

@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;
}
}

关于java - 创建抽象通用 jaxb 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13244652/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com