jaxb解组与其他类属性
问题描述:
我有这两个模型。 AssetMetadata:jaxb解组与其他类属性
@XmlRootElement(name="AssetMetadata")
public class AssetMetadata {
private AssetMetadataType assetMetadataType;
private String id;
private String assetId;
....
AssetMetadataType:
@XmlRootElement(name = "AssetMetadataType")
public class AssetMetadataType {
private String id;
private String name;
....
我使用JAXB解组这样的。 Spring配置:
<oxm:jaxb2-marshaller id="marshaller">
<oxm:class-to-be-bound name="ch.srf.esb.radioimporter.domain.AssetMetadata"/>
<oxm:class-to-be-bound name="ch.srf.esb.radioimporter.domain.AssetMetadataType"/>
</oxm:jaxb2-marshaller>
Java代码:
@Autowired @Qualifier("marshaller") private Unmarshaller unmarshaller;
...
final InputStream is = new ByteArrayInputStream(xml.getBytes());
this.unmarshaller.unmarshal(new StreamSource(is));
现在,当我把下面的XML,该AssetMetadataType未设置:
<AssetMetadata>
<AssetMetadataType>
<id>1</id>
<name>EPG</name>
</AssetMetadataType>
<assetId>39b4864d-931b-40c6-85ad-c45251b97952</assetId>
<title>title</title>
<description>description</description>
</AssetMetadata>
我该怎么办错了吗?
答
@XmlRootElement
应该只设置在根元素上。这就是为什么叫做@XmlRootElement
。它会在其他地方被忽略。
尝试从AssetMetadataType
类删除@XmlRootElement
,并更改AssetMetadata
的属性为:
@XmlElement(name="AssetMetadataType")
private AssetMetadataType assetMetadataType;
谢谢您的回答。不幸的是,我得到了这个异常,然后:类有两个同名的属性“assetMetadataType” – superbly 2012-01-16 10:52:09
好吧你是对的:我必须删除AssetMetadata模型中的getAssetMetadataType()和setAssetMetadataType()方法。现在它工作。谢谢。 – superbly 2012-01-16 10:58:04
+1 - @shifty,你不需要删除任何方法。只要确保你要么注释get方法,要么注解字段(实例变量),然后设置@XmlAccessorType(XmlAccessType.FIELD):http://blog.bdoughan.com/2011/06/using-jaxbs- xmlaccessortype-to.html – 2012-01-16 11:17:19