为什么使用xstream同一类出现java.lang.ClassCastException

时间:2016-10-21 03:09:26

标签: xstream

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;

异常:

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控制器中使用时,抛出此异常?

1 个答案:

答案 0 :(得分:6)

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

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

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

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