如果我有一个包含 20 个字段的类,我是否需要在每个字段上添加 @Element 注释?有没有办法告诉简单框架采取所有?
最佳答案
不,您可以使用@Default
注释:
The
Default
annotation is used to specify that all fields or methods should be serialized in a default manner. This basically allows an objects fields or properties to be serialized without the need to annotate them.
除了@Default
之外,您仍然可以使用自定义注释作为@Element
:
@Root
@Default
public static class Example
{
private int value = 3;
private String string = "abc";
@Element(name = "some-custom-name")
private String customString = "custom";
// ...
}
输出:
<example>
<value>3</value>
<string>abc</string>
<some-custom-name>custom</some-custom-name>
</example>
关于java - 所有字段都需要添加@Element注解吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38571548/