XStream——java中xml转bean对象

注解+代码

        
            com.thoughtworks.xstream
            xstream
            1.4.10
        

注解:

 @XStreamAlias("a")

 代码:

    public  T transferXMLToBean(Class clazz, String str) {
        XStream xstream = new XStream();
        xstream.processAnnotations(clazz);
        xstream.autodetectAnnotations(true);
        xstream.setupDefaultSecurity(xstream);
        xstream.allowTypes(new Class[]{clazz});
//或//  xstream.allowTypesByRegExp(new String[] {".*"});
        xstream.setClassLoader(clazz.getClassLoader());//这行是重点

        return (T)xstream.fromXML(str);
    }

 

你可能感兴趣的:(springBoot,大厂任性挑)