使用 XStream 反序列化泛型类型
我正在尝试使用 XStream 反序列化对具有通用类型字段的类的 xml 响应。
例如,我有类 Response
class Response<T extends BaseDTO>{
int id;
T field;
public int getId(){
return id;
}
public T getField(){
return field;
}
}
class B extends BaseDTO{}
class C extends BaseDTO{}
public abstract class XStreamFactory{
public static <T extends BaseDTO> XStream initXStream(Class<T> c){
XStream xstream = new XStream(new DomDriver());
xstream.alias("response-root", Response.class);
if(c.equals(B.class)){
xstream.alias("specific-response-b", B.class);
else
xstream.alias("specific-response-c", C.class);
xstream = addAliases(xstream, c);
return xstream;
}
// we want to process the annotations declared on type c
protected static XStream addAliases(XStream xstream, Class<?> c){
while (c != null) {
xstream.processAnnotations(c);
c = c.getSuperclass();
}
return xstream;
}
}
要反序列化响应,我执行以下操作:
XStream xstream = XStreamFactory.initializeXStream(B.class);
Response<B> resp = (Response<B>) xstream.fromXML(response);
下面是我得到的 xml 响应:
<response-root>
<id></id>
<specific-response-b>
//... content specific for class B
</specific-response-b>
</response-root>
或
<response-root>
<id></id>
<specific-response-c>
//... content specific for class c
</specific-response-c>
</response-root>
上面的代码不会反序列化响应。它引发以下异常:
com.thoughtworks.xstream.converters.ConversionException: Element specific-response-b of type B is not defined as field in type Response
你们能提供有关如何实现我想要的目标的任何指示吗? 谢谢。
I'm trying to use XStream to deserialize a xml response to a class that has a generic type field.
For example I have class Response
class Response<T extends BaseDTO>{
int id;
T field;
public int getId(){
return id;
}
public T getField(){
return field;
}
}
class B extends BaseDTO{}
class C extends BaseDTO{}
public abstract class XStreamFactory{
public static <T extends BaseDTO> XStream initXStream(Class<T> c){
XStream xstream = new XStream(new DomDriver());
xstream.alias("response-root", Response.class);
if(c.equals(B.class)){
xstream.alias("specific-response-b", B.class);
else
xstream.alias("specific-response-c", C.class);
xstream = addAliases(xstream, c);
return xstream;
}
// we want to process the annotations declared on type c
protected static XStream addAliases(XStream xstream, Class<?> c){
while (c != null) {
xstream.processAnnotations(c);
c = c.getSuperclass();
}
return xstream;
}
}
To deserialize the response I do the following:
XStream xstream = XStreamFactory.initializeXStream(B.class);
Response<B> resp = (Response<B>) xstream.fromXML(response);
Below are the xml responses that I get:
<response-root>
<id></id>
<specific-response-b>
//... content specific for class B
</specific-response-b>
</response-root>
or
<response-root>
<id></id>
<specific-response-c>
//... content specific for class c
</specific-response-c>
</response-root>
The code above does not deserialize the response. It throws the following exception:
com.thoughtworks.xstream.converters.ConversionException: Element specific-response-b of type B is not defined as field in type Response
Can you guys provide any pointers on how to achieve what I want?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
XStream 没有这个问题。您可能正在执行某种自定义编组。下面是编组和解组您上面指定的类的示例:
输出 - 由于没有别名和 toString(),所以有点混乱:
XStream doesn't have a problem with this. It's probably some kind of custom marshalling that you're doing. Here's an example of marshalling and unmarshalling the classes you specified above:
The output--a little messy because of no aliasing and no toString():