XStream。未知字段异常
XStream. UnknownFieldException
请告诉我为什么会产生异常?
当我尝试将 xml 反序列化为 java 对象时出现异常。
com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$UnknownFieldException: No such field addressservice.Region.region.
---- Debugging information ----
field : region
class : addressservice.Region
required-type : addressservice.Region
converter-type : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
path : /AddressServiceConfig/regions/region
line number : 3
class[1] : addressservice.AddressServiceConfig
version : 1.4.7
我的XML:
<addressServiceConfig>
<regions>
<region>
<regionName>region1</regionName>
<provinces>
<province>
<name>province11</name>
<code>11</code>
</province>
<province>
<name>province12</name>
<code>12</code>
</province>
</provinces>
</region>
<region>
<regionName>region2</regionName>
<provinces>
<province>
<name>province21</name>
<code>21</code>
</province>
<province>
<name>province22</name>
<code>22</code>
</province>
</provinces>
</region>
</regions>
<pathToCodeposFiles></pathToCodeposFiles>
</addressServiceConfig>
我的类
@XStreamAlias("addressServiceConfig")
public class AddressServiceConfig
{
private String pathToCodeposFiles;
@XStreamImplicit(itemFieldName = "region")
private List<Region> regions;
}
@XStreamAlias("region")
public class Region
{
private String regionName;
@XStreamImplicit(itemFieldName = "province")
private List<Province> provinces;
}
@XStreamAlias("province")
public class Province
{
private int code;
private String name;
}
请帮忙。
您的 xml
定义没有隐式的 collections regions
和 provinces
,而是显式的。
您应该从 class 定义中删除 @XStreamImplicit(itemFieldName = "region")
和 @XStreamImplicit(itemFieldName = "province")
。
显式 regions
collection:
<addressServiceConfig>
<regions>
<region>
...
</region>
<region>
...
</region>
</regions>
</addressServiceConfig>
隐式 regions
collection:
<addressServiceConfig>
<region>
...
</region>
<region>
...
</region>
</addressServiceConfig>
请告诉我为什么会产生异常? 当我尝试将 xml 反序列化为 java 对象时出现异常。
com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$UnknownFieldException: No such field addressservice.Region.region.
---- Debugging information ----
field : region
class : addressservice.Region
required-type : addressservice.Region
converter-type : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
path : /AddressServiceConfig/regions/region
line number : 3
class[1] : addressservice.AddressServiceConfig
version : 1.4.7
我的XML:
<addressServiceConfig>
<regions>
<region>
<regionName>region1</regionName>
<provinces>
<province>
<name>province11</name>
<code>11</code>
</province>
<province>
<name>province12</name>
<code>12</code>
</province>
</provinces>
</region>
<region>
<regionName>region2</regionName>
<provinces>
<province>
<name>province21</name>
<code>21</code>
</province>
<province>
<name>province22</name>
<code>22</code>
</province>
</provinces>
</region>
</regions>
<pathToCodeposFiles></pathToCodeposFiles>
</addressServiceConfig>
我的类
@XStreamAlias("addressServiceConfig")
public class AddressServiceConfig
{
private String pathToCodeposFiles;
@XStreamImplicit(itemFieldName = "region")
private List<Region> regions;
}
@XStreamAlias("region")
public class Region
{
private String regionName;
@XStreamImplicit(itemFieldName = "province")
private List<Province> provinces;
}
@XStreamAlias("province")
public class Province
{
private int code;
private String name;
}
请帮忙。
您的 xml
定义没有隐式的 collections regions
和 provinces
,而是显式的。
您应该从 class 定义中删除 @XStreamImplicit(itemFieldName = "region")
和 @XStreamImplicit(itemFieldName = "province")
。
显式 regions
collection:
<addressServiceConfig>
<regions>
<region>
...
</region>
<region>
...
</region>
</regions>
</addressServiceConfig>
隐式 regions
collection:
<addressServiceConfig>
<region>
...
</region>
<region>
...
</region>
</addressServiceConfig>