java 如何配置 JAXB 使其默认修剪空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7419951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:53:40  来源:igfitidea点击:

How to configure JAXB so it trims whitespaces by default

javaxmljaxbtrim

提问by jpboudreault

I would like to configure JAXB so that it trims whitespaces on all string fields

我想配置 JAXB 以便它修剪所有字符串字段上的空格

I saw the following answer : How to configure JAXB so it trims whitespaces when unmarshalling tag value?

我看到了以下答案:如何配置 JAXB 以便在解组标记值时修剪空格?

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!

谢谢!

回答by Sahil Muthoo

  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.javafile in com.foo.bar.

  3. Add the following to the package-info.javafile

    @XmlJavaTypeAdapter(value=StringTrimAdapter.class,type=String.class)
    package com.foo.bar;
    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
    
  4. This will apply StringTrimAdapterto allStringfields in com.foo.barwithout any extra annotations.
  1. 创建一个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. 创建一个package-info.java文件com.foo.bar

  3. 将以下内容添加到package-info.java文件中

    @XmlJavaTypeAdapter(value=StringTrimAdapter.class,type=String.class)
    package com.foo.bar;
    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
    
  4. 这将适用StringTrimAdapter所有String字段,com.foo.bar无需任何额外注释。

EDIT
Do note that if the package level annotation is too expansive for you, you could always apply a @XmlJavaTypeAdapterannotation to classes.

编辑
请注意,如果包级别的注释对您来说过于广泛,您始终可以将@XmlJavaTypeAdapter注释应用于类。

回答by Lukas Eder

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:

值得一提的是,除了编写XmlAdapter执行修剪的 ,您还可以配置 XJC 以在生成的代码中实际使用此适配器。如何做到的一个例子:

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

The above example uses the XmlAdaptergiven in Sahil's answer

上面的例子使用了XmlAdapterSahil 的回答中给出的

回答by Vignesh

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

为了使配置XJC的示例(in the answer provided by Lukas Eder)完整,我想添加以下示例配置,我们需要将其添加到maven的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.

我们需要在 bindings.xjb.

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