用Xstream时候遇到的两个小异常

第一个

com.thoughtworks.xstream.converters.ConversionException: Cannot construct ClassXXX as it does not have a no-args constructor : Cannot construct yourpackage.yourclass as it does not have a no-args constructor

搜了一下才发现是版本的问题:

我用的是JDK7+XStream1.3.1

你可以换回JDK1.6或者升级XStream到最新版本。

参考:http://stackoverflow.com/questions/7966817/xstream-unmarshalling-type-specified-in-xml-not-visible

复制过来:

I got to the bottom of it - turns out xstream should handle that xml (it doesn't need a no-args constructor), the issue arose because I was using jdk 7 with an older version of xstream (1.3.1). See here http://code.google.com/p/pitestrunner/issues/detail?id=4. Moving back to jdk 6 fixed the issue (for various reasons i can't upgrade).

Before I realised that I did write a converter that worked for RandomAccessSubList if anyone needs it:

复制代码
public class RandomAccessSubListConverter extends CollectionConverter {

public RandomAccessSubListConverter(Mapper mapper) {
    super(mapper); 
} 

@Override
public boolean canConvert(Class arg0) {     
    return arg0.getName().equals("java.util.RandomAccessSubList");
}

@Override
public Object unmarshal(HierarchicalStreamReader reader,
        UnmarshallingContext context) {        
    reader.moveDown();
    ArrayList arrayList = new ArrayList();
    populateCollection(reader, context, arrayList);
    reader.moveUp();
    return arrayList;
}
复制代码

Thanks to anyone who was looking into for me!

我选择用最新的版本来解决,然后就遇到了第二个问题:

第二个

java.lang.NoClassDefFoundError: org/xmlpull/v1/XmlPullParserFactory

在初始化你的XStream的时候,在构造函数里面传入入:new DomDriver()

参考:http://blog.csdn.net/chenallen1025/article/details/8030552

复制过来:

1.抛出的异常信息如下:

2.原因:

应该改成:

XStream xstream=new XStream(new DomDriver()); //注意:不是new Xstream(); 否则报上面的错
xstream.processAnnotations(PersonBean.class); //如果是用注解的方式,这句不能少
PersonBean person=(PersonBean)xstream.fromXML(xmlStr);
System.out.println("person=firstname=="+person.getFirstName());
return person;

转载于:https://www.cnblogs.com/zhangsongren/p/7263718.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值