我一直试图将自创的xml配置文件解组为具有JAXB默认实现的对象,我忽略了原因,但是跳过了一些内部元素和值,给出了null结果。
这是xml文档:
<import-sources>
<domain-objects>
<domain-object class="xxx.xxx.core.business.mapping.Make">
<sources>
<source name="xxxxxx" class="xxx.xxx.core.web.common.model.xxxxxxx.output.MakeDTO">
<uri secured="true" value="xxxxxxxxxxxxxxxxxx"/>
<key name="api_key" mode="in_uri" value="xxxxxxxxxxxx"/>
</source>
</sources>
</domain-object>
</domain-objects>
这是映射的类:
@XmlRootElement(name = "import-sources")
public class ImportSources {
@XmlType
public static class DomainObject {
@XmlType
public static class Source {
@XmlType
public static class URI {
private String value;
private boolean secured;
}
@XmlType
public static class Key {
@XmlEnum
public enum Mode {
@XmlEnumValue("in_uri")
IN_URI,
@XmlEnumValue("in_header")
IN_HEADER
}
private String name;
private String value;
private Mode mode;
}
private String name;
private URI uri;
private Key key;
@XmlElement(name = "class")
private Class outputClass;
}
@XmlAttribute(name = "class")
private Class<? extends Entity> subjectClass;
@XmlElementWrapper
@XmlElement(name = "source")
private Source[] sources;
}
@XmlElementWrapper(name = "domain-objects")
@XmlElement(name = "domain-object")
private DomainObject[] domainObjects;
}
使用Unmarshaller.unmarshall()解析文档后,所有Source的字段都为null,如果我替换它的内部标记属性,我可以得到它的标量字段:
<source>
<name>....</name>
<class>...</class>
</source>
由于冗余,我想避免使用。
答案 0 :(得分:0)
我最终通过使用'@XmlAttribute'在开始标记或单个标记中注释我想要的所有字段来解决我的问题。
答案 1 :(得分:0)
我建议相应地明确地将字段注释为@XmlAttribute
或@XmlRootElement(name = "import-sources")
public class ImportSources {
@XmlType
public static class DomainObject {
@XmlType
public static class Source {
@XmlType
public static class URI {
@XmlAttribute //here
private String value;
@XmlAttribute //here
private boolean secured;
}
@XmlType
public static class Key {
@XmlEnum
public enum Mode {
@XmlEnumValue("in_uri")
IN_URI,
@XmlEnumValue("in_header")
IN_HEADER
}
@XmlAttribute //here
private String name;
@XmlAttribute //here
private String value;
@XmlAttribute //here
private Mode mode;
}
@XmlAttribute //here
private String name;
@XmlElement //here
private URI uri;
@XmlElement //here
private Key key;
@XmlElement(name = "class")
private Class outputClass;
}
@XmlAttribute(name = "class")
private Class<? extends Entity> subjectClass;
@XmlElementWrapper
@XmlElement(name = "source")
private Source[] sources;
}
}
。您的课程将如下所示,字段将填充:
{{1}}