Is it possible to ignore a wrapper class during JAXB marshalling(在 JAXB 编组期间是否可以忽略包装类)
问题描述
我试图让 JAXB 在编组过程中忽略包装类,在代码中包含这个包装类是有意义的,因为它将所有相关信息放在一起,但是我需要在编组期间摆脱它过程.以下是相关代码.
I'm trying to get JAXB to ignore a wrapper class during the Mashalling process, it makes sense to have this wrapper class in code, as it keep all related information together, however I need to get rid of it during the marshaling process. The following is the relevant code.
@XmlType(name = "root")
@XmlRootElement(name = "root")
public class Root {
@XmlElementRef
private List<Resource> resources = new ArrayList<>();
public void addResource(Resource resource) {
resources.add(resource);
}
}
@XmlRootElement(name = "", namespace = "")
@XmlAccessorType(XmlAccessType.NONE)
public class Resource {
@XmlElementRef
private Element element;
@XmlElementRef
private FieldType fieldType;
@XmlElementRef
private ListType listType;
}
Root 是主要对象,而 Resource 是我不希望为其创建节点的包装对象.但是,我仍然希望呈现 Resource 中的 Element、FieldType 和 ListType.
Root is the main object, and Resource is the wrapper object that I'd like not have a node created for. I still want the Element, FieldType and ListType within the Resource to be rendered however.
这是我目前拥有的:
<root>
<>
<element name="resource1"/>
<fieldType name="resource1--type">
</fieldType>
<listType name="resource--list">
</listType>
</>
<>
<element name="resource2"/>
<fieldType name="resource2--type">
</fieldType>
<listType name="resource2--list">
</listType>
</>
</root>
我想要实现的目标如下:
What I'd like to achieve is the following:
<root>
<element name="resource1"/>
<fieldType name="resource1--type">
</fieldType>
<listType name="resource--list">
</listType>
<element name="resource2"/>
<fieldType name="resource2--type">
</fieldType>
<listType name="resource2--list">
</listType>
</root>
我不知道这是否可能,但我们将不胜感激.
I don't know if it's possible, but any help would be appreciated.
谢谢.
推荐答案
你无法在 JAXB 中实现.即使您能够像这样进行序列化,例如使用 XmlAdapter,也无法对其进行反序列化.
You cannot achieve that in JAXB. Even if you would be able to serialize like this, using a XmlAdapter for example, it will be impossible to deserialize it.
试试这个:
@XmlType(name = "root")
@XmlRootElement(name = "root")
@XmlAccessorType(XmlAccessType.NONE)
public class Root {
private ArrayList<Resource> resources = new ArrayList<Resource>();
public void addResource(Resource resource) {
resources.add(resource);
}
@XmlElementRefs(value = { @XmlElementRef(type = Element.class),
@XmlElementRef(type = ListType.class),
@XmlElementRef(type = FieldType.class) })
public List<Object> getResourceFields() {
List<Object> list = new ArrayList<Object>();
for (Resource r : resources) {
list.add(r.getElement());
list.add(r.getFieldType());
list.add(r.getListType());
}
return list;
}
}
基本上 getRerourceFields 将所有资源的字段连接到同一个列表中.如果您无法更改 Root 类,这可能是您的 RootAdapter 并按照@Biju 的建议使用它.
Basically getRerourceFields concatenates all the resources' fields in the same list.
If you cannot change the Root class, this could be your RootAdapter and use it as @Biju suggested.
这篇关于在 JAXB 编组期间是否可以忽略包装类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 JAXB 编组期间是否可以忽略包装类
基础教程推荐
- Struts2 URL 无法访问 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
