gpt4 book ai didi

xstream - 为什么使用xstream同一个Class会出现java.lang.ClassCastException

转载 作者:行者123 更新时间:2023-12-04 00:06:42 36 4
gpt4 key购买 nike

java.lang.ClassCastException: cn.yunnet.wxhotel.utils.TestMicropay 无法转换为 cn.yunnet.wxhotel.utils.TestMicropay

TestMicropayEntity:

package cn.yunnet.wxhotel.utils;

public class TestMicropay {
private String return_code;
private String return_msg;
public String getReturn_code() {
return return_code;
}
public void setReturn_code(String return_code) {
this.return_code = return_code;
}
public String getReturn_msg() {
return return_msg;
}
public void setReturn_msg(String return_msg) {
this.return_msg = return_msg;
}
}

----xstream
package me.chanjar.weixin.common.util.xml;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.basic.DoubleConverter;
import com.thoughtworks.xstream.converters.basic.FloatConverter;
import com.thoughtworks.xstream.converters.basic.IntConverter;
import com.thoughtworks.xstream.core.util.QuickWriter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.XppDriver;
import com.thoughtworks.xstream.security.NoTypePermission;
import com.thoughtworks.xstream.security.NullPermission;
import com.thoughtworks.xstream.security.PrimitiveTypePermission;

import java.io.Writer;

public class XStreamInitializer {

public static XStream getInstance() {
XStream xstream = new XStream(new XppDriver() {

@Override
public HierarchicalStreamWriter createWriter(Writer out) {
return new PrettyPrintWriter(out, getNameCoder()) {
protected String PREFIX_CDATA = "<![CDATA[";
protected String SUFFIX_CDATA = "]]>";
protected String PREFIX_MEDIA_ID = "<MediaId>";
protected String SUFFIX_MEDIA_ID = "</MediaId>";
@Override
protected void writeText(QuickWriter writer, String text) {
if (text.startsWith(PREFIX_CDATA) && text.endsWith(SUFFIX_CDATA)) {
writer.write(text);
} else if (text.startsWith(PREFIX_MEDIA_ID) && text.endsWith(SUFFIX_MEDIA_ID)) {
writer.write(text);
} else {
super.writeText(writer, text);
}

}
};
}
});
xstream.ignoreUnknownElements();
xstream.setMode(XStream.NO_REFERENCES);
xstream.addPermission(NullPermission.NULL);
xstream.addPermission(PrimitiveTypePermission.PRIMITIVES);
return xstream;
}

}

调用:
String text = "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[签名错误]]></return_msg>";
XStream xstream = XStreamInitializer.getInstance();
xstream.alias("xml", TestMicropay.class);
Object o = xstream.fromXML(responseContent);
TestMicropay t = (TestMicropay)o;

异常(exception):
java.lang.ClassCastException: cn.yunnet.wxhotel.utils.TestMicropay cannot be cast to cn.yunnet.wxhotel.utils.TestMicropay
at cn.yunnet.wxhotel.utils.wechat.AdvancedWxMpServiceImpl.micropayPost(AdvancedWxMpServiceImpl.java:106) ~[classes/:na]

- 为什么当我在 main() 调用中使用时,它工作正常;但在 SpringMVC Controller 中使用时,抛出此异常??

最佳答案

Java, getting class cast exception where both classes are exactly the same它说两个类的相等性取决于类名和加载器。

XStream 必须使用不同的类加载器。

快速解决方案是设置当前线程正在使用的相同类加载器:

XStream xStream = new XStream();
xStream.setClassLoader(Thread.currentThread().getContextClassLoader());

关于xstream - 为什么使用xstream同一个Class会出现java.lang.ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40167735/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com