Xstream XML to Java fails with Classcast exception

一世执手 提交于 2019-12-12 03:48:25

问题


I have two classes.

XClass1

  @XStreamAlias("reply")
  public class XClass1 {

    @XStreamAlias("message")
    private String message;

    public String getMessage() {
      return message;
    }

  }

XClass2

  @XStreamAlias("reply")
  public class XClass2 {

    @XStreamAlias("status")
    private String status;

    public String getStatus() {
      return status;
    }

  }

I am using Xstream to convert xml to Java

@Test
  public void test() {
    XStream stream = new XStream() {
      @Override
      protected MapperWrapper wrapMapper(MapperWrapper next) {
        return new MapperWrapper(next) {
          @Override
          public boolean shouldSerializeMember(@SuppressWarnings("rawtypes") Class definedIn, String fieldName) {
            if (definedIn == Object.class)
              return false;

            return super.shouldSerializeMember(definedIn, fieldName);
          }
        };
      }
    };
    stream.autodetectAnnotations(true);
    stream.processAnnotations(new Class[] {XClass1.class, XClass2.class});

    String reply = "<reply><message>Not OK</message></reply>";
    XClass1 lass1 = (XClass1) stream.fromXML(reply);

    String reply2 = "<reply><status>Not OK</status></reply>";
    XClass2 lass2 = (XClass2) stream.fromXML(reply2);
  }

I am getting class cast exception when converting xml to java

Error at this place

 XClass1 lass1 = (XClass1) stream.fromXML(reply);

Exception

java.lang.ClassCastException: com.myapp.Test123$XClass2 cannot be cast to com.myapp.Test123$XClass1
    at com.myapp.Test123.test(Test123.java:56)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

回答1:


There is a little bug in the last line:

XClass2 lass2 = (XClass2) stream.fromXML(reply);

I think it shoult be

XClass2 lass2 = (XClass2) stream.fromXML(reply2); // reply2 


来源:https://stackoverflow.com/questions/40263289/xstream-xml-to-java-fails-with-classcast-exception

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!