本文主要是介绍在springboot下面使用xstream 将xml字符串转换成对象时出现 相同对象不能互转的问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在springboot下面使用xstream 将xml字符串转换成对象时出现 相同对象不能互转的问题:
原因是因为SpringBoot重新加载了对象,若未指定classloader的时候,SpringBoot未正确使用classloader,
需要指定classloader。
添加代码如下:
xstream.setClassLoader(clazz.getClassLoader());
eg:
XStream xstream =new XStream();xstream.autodetectAnnotations(true);xstream.ignoreUnknownElements();xstream.alias("response", GreenLineWHResponse.class);xstream.alias("returnresult", ReturnResult.class);xstream.alias("pageactionout", LxWhPageActionOut.class);xstream.alias("data_row", LxWhDoctorSchedule.class);xstream.aliasField("data", GreenLineWHResponse.class, "m_doctorSchedule");xstream.setClassLoader(GreenLineWHResponse.class.getClassLoader());GreenLineWHResponse resp=(GreenLineWHResponse)xstream.fromXML(rtnMessage);ReturnResult result =resp.getReturnResult();checkRtn(result); //检查返回结果flag = HyEnumHoptRtn.SUCCESS.getStatus();infos=resp.getDoctorSchedule();XStream xstream = new XStream();xstream.addPermission(AnyTypePermission.ANY);Class<?>[] classes = new Class[] {GreenLineWHResponse.class, ReturnResult.class,LxWhOrderInfo.class};xstream.allowTypes(classes);xstream.autodetectAnnotations(true);xstream.ignoreUnknownElements();xstream.setClassLoader(GreenLineWHResponse.class.getClassLoader());xstream.alias("response", GreenLineWHResponse.class);xstream.alias("returnresult", ReturnResult.class);xstream.aliasField("outputvalues", GreenLineWHResponse.class,"m_lxWhOrderInfo");GreenLineWHResponse resp=(GreenLineWHResponse)xstream.fromXML(rtnMessage);
这篇关于在springboot下面使用xstream 将xml字符串转换成对象时出现 相同对象不能互转的问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!