How to configure JAXB so it trims whitespaces by default

≡放荡痞女 提交于 2020-02-13 07:16:50

问题


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!


回答1:


  1. Create a XmlAdapter.

    package com.foo.bar;
    public class StringTrimAdapter extends XmlAdapter<String, String> {
        @Override
        public String unmarshal(String v) throws Exception {
            if (v == null)
                return null;
            return v.trim();
        }
        @Override
        public String marshal(String v) throws Exception {
            if (v == null)
                return null;
            return v.trim();
        }
    }
    
  2. Create a package-info.java file in com.foo.bar.

  3. Add the following to the package-info.java file

    @XmlJavaTypeAdapter(value=StringTrimAdapter.class,type=String.class)
    package com.foo.bar;
    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
    
  4. This will apply StringTrimAdapter to all String fields in com.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.




回答2:


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:

<jaxb:globalBindings>
    <xjc:javaType 
         name="java.lang.String" 
         xmlType="xs:string" 
         adapter="com.foo.bar.StringTrimAdapter"/>
</jaxb:globalBindings>

The above example uses the XmlAdapter given in Sahil's answer




回答3:


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

    <build>
    .
    .
    <execution>
       <id>responseSchema</id>
       <goals>
          <goal>xjc</goal>
       </goals>
       <phase>generate-sources</phase>
       <configuration>
          <clearOutputDir>false</clearOutputDir>
          <outputDirectory>${project.basedir}/src/main/java</outputDirectory>
          <packageName>com.foo.bar.domain.response</packageName>
          <bindingFiles>../resources/bindings.xjb</bindingFiles>
          <schemaDirectory>${project.basedir}/src/main/resources/wsdl/xsd</schemaDirectory>
          <schemaFiles>response.xsd</schemaFiles>
          <extension>true</extension>
       </configuration>
    </execution>
    .
    .
 </build>

We need to have the following content to be added in bindings.xjb.

<jaxb:globalBindings>
    <xjc:javaType 
         name="java.lang.String" 
         xmlType="xs:string" 
         adapter="com.foo.bar.StringTrimAdapter"/>
</jaxb:globalBindings>


来源:https://stackoverflow.com/questions/7419951/how-to-configure-jaxb-so-it-trims-whitespaces-by-default

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!