排除JAXB中的字段 [英] Excluding fields in JAXB
问题描述
我有2个班级:
@XmlRootElement
public class A {
private Long id;
private B b;
// setters and getters
}
和
@XmlRootElement
public class B {
private Long id;
private String field1;
private String field2;
// setters and getters
}
默认情况下,如果我将类 A
的实例转换为XML,我将拥有其所有字段( id
)和引用 B
类字段( id
, field1
, field2
)像这样:
By default, if I transform an instance of class A
to the XML, I will have all its fields (id
) and the referenced B
class fields (id
, field1
, field2
) like this:
<a>
<id>2</id>
<b>
<id>5</id>
<field1>test1</field1>
<field2>test3</field2>
</b>
</a>
可以修改引用类<$ c $中的 字段c> B 包含在 A
类的XML中?例如。我想说当我转换 A
类的实例时,我只想从<$获得 id
c $ c> B class(没有 field1
和 field2
字段),所以我想要得到:
Is is possible to modify what fields from referenced class B
are included in the XML of the A
class? E.g. I want to say that when I transform an instance of A
class, I just want to get id
from the B
class (no field1
and field2
fields), so I want to get:
<a>
<id>2</id>
<b>
<id>5</id>
</b>
</a>
我不想永久注释 B
class(使用 @XMLTransient
或 @XMLElement
)来实现它,因为在某些情况下我想要按原样导出 B
类( id
, field1
和 field2
。)
我只是不想在 B
class是从 A
引用的。
I don't want to permanently annotate the B
class (using @XMLTransient
or @XMLElement
) to achieve it, as there are cases in which I want to export whole B
class as is (with id
, field1
and field2
.)
I just don't want to export all these fields when the B
class is referenced from A
.
这对JAX-B来说是否可行?
Is this even possible with JAX-B?
推荐答案
默认的JAXB绑定可以在全局范围内进行覆盖,也可以根据需要使用自定义绑定声明或使用 @XmlTransient
在球场上。
The default JAXB bindings can be overridden at a global scope or on a case-by-case basis as needed by using custom binding declarations or use @XmlTransient
on the field.
Chec来自Baeldung网站的这个例子, JAXB指南。
Check out this example from Baeldung website, Guide to JAXB.
这篇关于排除JAXB中的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!