Xstream XML to Java因Classcast异常而失败

时间:2016-10-26 13:13:09

标签: java xstream

我有两节课。

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;
    }

  }

我正在使用Xstream将xml转换为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);
  }

将xml转换为java

时,我遇到了类转换异常

此地点出错

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

异常

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 个答案:

答案 0 :(得分:0)

最后一行有一个小错误:

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

我认为应该是

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