使用XStream反序列化泛型类型

时间:2011-08-29 13:30:12

标签: java xstream generic-type-argument

我正在尝试使用XStream将xml响应反序列化为具有泛型类型字段的类。

例如,我有类响应

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

你们能提供关于如何实现我想要的任何指示吗? 谢谢。

1 个答案:

答案 0 :(得分:1)

XStream对此没有任何问题。这可能是你正在做的某种定制编组。以下是编组和解组上面指定的类的示例:

public static void main(String[] args) {
    Response<B> responseB = new Response<B>(5, new B());
    String xml = new XStream().toXML(responseB);
    System.out.println("marshalled:\n" + xml);
    Response<B> resultB = (Response<B>) new XStream().fromXML(xml);
    System.out.println("unmarshalled: " + resultB);

    Response<C> responseC = new Response<C>(10, new C());
    xml = new XStream().toXML(responseC);
    System.out.println("marshalled:\n" + xml);
    Response<C> resultC = (Response<C>) new XStream().fromXML(xml);
    System.out.println("unmarshalled: " + resultC);
}

static class Response<T extends Foo> {
    int id;
    T field;

    Response(int id, T field) {
        this.id = id;
        this.field = field;
    }

    public int getId() { return id; }
    public T getField() { return field; }
}

static class Foo {}

static class B extends Foo {}

static class C extends Foo {}

输出 - 因为没有别名而没有toString()而有点乱:

marshalled:
<rds.testbed.XStreamReadingNamespaceAttributes_-Response>
  <id>5</id>
  <field class="rds.testbed.XStreamReadingNamespaceAttributes$B"/>
</rds.testbed.XStreamReadingNamespaceAttributes_-Response>
unmarshalled: rds.testbed.XStreamReadingNamespaceAttributes$Response@1cc7b00c
marshalled:
<rds.testbed.XStreamReadingNamespaceAttributes_-Response>
  <id>10</id>
  <field class="rds.testbed.XStreamReadingNamespaceAttributes$C"/>
</rds.testbed.XStreamReadingNamespaceAttributes_-Response>
unmarshalled: rds.testbed.XStreamReadingNamespaceAttributes$Response@3d66aa18