使用xstream时,实体类转化出现的问题
最近在使用xstream时发现同一个实体类不能相互转化,最后发现在SpringBoot
中使用 xstream不会自动装载ClassLoader,需手动配置ClassLoader。
//工具转换类
public Object toBean(Class<?> calzz,String xml){
Object xmlObject=null;
XStream xStream=new XStream(new DomDriver(“UTF-8”, new XmlFriendlyNameCoder("-", "")));
//应用传过来类的注解
xStream.processAnnotations(calzz);
//自动检测注解
xStream.autodetectAnnotations(true);
//手动设置ClassLoader
xStream.setClassLoader(UnifiedserviceList.class.getClassLoader());
xmlObject=xStream.fromXML(xml);
return xmlObject;
}