有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

首先,我想展示反序列化xml字符串的核心代码

    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是从超基类扩展而来的a类。 就像

T extends baseInputXml

但是当应用程序在

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

将出现错误消息,告诉我XMLClient中没有匹配的属性。虽然该属性在超级T类中不存在,但在子T类中存在

我希望你能了解我的想法,我对这些genericType和xstream转换非常困惑 多谢各位


共 (1) 个答案

  1. # 1 楼答案

    xs.processAnnotations(XMLClient.class);
    

    该行应编辑为

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