无法使用xstream将xml字符串反序列化为扩展的genericType的Object

时间:2013-02-04 17:55:05

标签: java generics xstream

首先,我想展示我的核心代码,用于反序列化xml String

    public XMLClient<T> xmlToClient(String xmlString,T t) throws Exception{
    //according to the generic class's name,insert the name after input tag
    //to make the xstream to know which class is going to deserialize to
        String insertClassName = t.getGenericTypeRecord();
        xmlString.replaceAll("<INPUT>", "<INPUT input."+insertClassName+">");
        XMLClient<T> xmlClient = new XMLClient<T>();
        XStream xs = new XStream(new DomDriver());
        xs.processAnnotations(XMLClient.class);
        xmlClient = (XMLClient<T>)xs.fromXML(xmlString);
        logger.info(xmlString);
        return xmlClient;
    }

T是从超基类扩展的类。 就像

  

T扩展baseInputXml

但是当应用程序在

运行时
xmlClient = (XMLClient<T>)xs.fromXML(xmlString);

会有错误消息告诉我XMLClient中没有匹配的属性。尽管超级T类中不存在该属性,但是存在于子T类中

我希望你能得到我的想法,我真的对这些genericType和xstream转换感到困惑 谢谢

1 个答案:

答案 0 :(得分:0)

xs.processAnnotations(XMLClient.class);

此行应编辑为

xs.processAnnotations(XMLClient.class);
xs.processAnnotations(XMLClientsSuperClass.class);