How to configure JAXB unmarshaller so it will trim leading and trailing whitespaces from strings?
For instance let\'s consider a simple binding between a Java bean
Use a custom Adapter class. I was thinking that NormalizedStringAdapter would do the work but it's only for unmarshaling and it doesn't do what you want anyway.
public class MyNormalizedStringAdapter extends XmlAdapter {
@Override
public String marshal(String text) {
return text.trim();
}
@Override
public String unmarshal(String v) throws Exception {
return v.trim();
}
}
then decorate the field with your adapter like this:
@XmlElement(required=true)
@XmlJavaTypeAdapter(MyNormalizedStringAdapter.class)
String name;