xstream CannotResolveClassException [英] xstream CannotResolveClassException

查看:187
本文介绍了xstream CannotResolveClassException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用xstream 1.4.2将xml转换为对象。它确实对我来说非常好,直到我将对象的类文件放在一个单独的包中,而不是主代码运行的位置。然后我得到一个CannotResolveClassException。我已尝试使用其他人推荐的setClassLoader方法,但这没有帮助。

I'm trying to use xstream 1.4.2 to convert xml to object. It does work perfectly fine for me until I put the object's class file in a separate package than where the main code runs. Then I get a CannotResolveClassException. I've tried using the setClassLoader method as recommended by others but that doesn't help.

Exception in thread "main" com.thoughtworks.xstream.mapper.CannotResolveClassException: result
at com.thoughtworks.xstream.mapper.DefaultMapper.realClass(DefaultMapper.java:56)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.DynamicProxyMapper.realClass(DynamicProxyMapper.java:55)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.PackageAliasingMapper.realClass(PackageAliasingMapper.java:88)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.ClassAliasingMapper.realClass(ClassAliasingMapper.java:79)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.ArrayMapper.realClass(ArrayMapper.java:74)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.CachingMapper.realClass(CachingMapper.java:45)
at com.thoughtworks.xstream.core.util.HierarchicalStreams.readClassType(HierarchicalStreams.java:29)
at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:133)
at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1052)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1036)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:912)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:903)
at main.readClass(main.java:48)
at main.main(main.java:28)

答案: xstream期望xml结构相对于它(对象)所源自的包。因此必须使用xstream.alias来为xml结构设置别名。

Answer: xstream expects the xml structure to be relative to the package in which it (the object) originates from. So xstream.alias must be used in order to alias the xml structure.

xstream.alias("something", Something.class);

否则xstream会将Something放在默认包中而不是它所属的包中。

Otherwise xstream will expect "Something" to be in the default package rather than the package it is a member of.

推荐答案

当您将类名传递给别名方法时,您需要传递完全限定的类名,即类名及其包名。

When you pass the class name to the alias method you need to pass the fully qualified class name i.e the class name along with its package name.

示例:假设如果类Something在com.xyz包中,则需要传递com.xyz.Something.class作为参数

Example : Suppose if the class Something is in the package com.xyz then you need to pass com.xyz.Something.class as the parameter

谢谢

这篇关于xstream CannotResolveClassException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆