如何配置 JAXB 以使其默认修剪空格
我想配置 JAXB,以便它修剪所有字符串字段上的空格
我看到以下答案: 如何配置 JAXB,以便在解组标记值时修剪空格?
但我不想这样做根据建议的答案注释所有字符串字段
@XmlElement(required=true)
@XmlJavaTypeAdapter(MyNormalizedStringAdapter.class)
String name;
谢谢!
I would like to configure JAXB so that it trims whitespaces on all string fields
I saw the following answer : How to configure JAXB so it trims whitespaces when unmarshalling tag value?
But I do not want to have to annotate all string fields as per the suggested answer
@XmlElement(required=true)
@XmlJavaTypeAdapter(MyNormalizedStringAdapter.class)
String name;
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
创建 XmlAdapter< /a>.
在
com.foo.bar
中创建一个package-info.java
文件。将以下内容添加到
package-info.java
文件StringTrimAdapter
应用于所有String
字段,没有任何额外的注释。com.foo.bar
中的编辑
请注意,如果包级别注释对您来说太大,您始终可以将
@XmlJavaTypeAdapter
注释应用于类。Create a XmlAdapter.
Create a
package-info.java
file incom.foo.bar
.Add the following to the
package-info.java
fileStringTrimAdapter
to allString
fields incom.foo.bar
without any extra annotations.EDIT
Do note that if the package level annotation is too expansive for you, you could always apply a
@XmlJavaTypeAdapter
annotation to classes.可能值得一提的是,除了编写执行修剪的
XmlAdapter
之外,您可以配置 XJC 以在生成的代码中实际使用此适配器。如何执行此操作的示例:上面的示例使用 Sahil 的答案中给出的
XmlAdapter
It might be worth mentioning, that in addition to writing an
XmlAdapter
, which performs the trimming, you can configure XJC to actually use this adapter in generated code. An example of how to do it:The above example uses the
XmlAdapter
given in Sahil's answer为了使配置 XJC
(在 Lukas Eder 提供的答案中)
的示例完整,我想添加以下示例配置,我们需要在 maven 的 pom.xml 中添加
我们需要在
bindings.xjb
中添加以下内容。To make the example of configuring XJC
(in the answer provided by Lukas Eder)
complete,i would like to add the following sample configuration which we need to add in maven's pom.xml
We need to have the following content to be added in
bindings.xjb
.