2016-10-21 21 views
0

java.lang.ClassCastExceptionが表示されます。cn.yunnet.wxhotel.utils.TestMicropayはcn.yunnet.wxhotel.utils.TestMicropayはなぜXStreamの同じクラスを使用するとjava.lang.ClassCastException

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] 

-why main()呼び出しで使用すると正常に動作しますが、SpringMVCコントローラで使用するとこの例外がスローされます??

答えて

4

Java, getting class cast exception where both classes are exactly the sameでは、2つのクラスの等価性はクラス名とローダーによって決まります。

XStreamは異なるクラスローダーを使用する必要があります。

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

迅速な解決策は、現在のスレッドが使用している同じクラスローダを設定することです

関連する問題