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!
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.xmlWe need to have the following content to be added in
bindings.xjb
.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.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