为什么 JAXB 2 RI 的 XJC 简单模式会更改集合名称?

发布于 2024-10-20 01:15:34 字数 687 浏览 0 评论 0原文

JAXB简单绑定模式将集合名称修改为其复数 “版本”,例如“additionalData”变为“additionalDatas”。有什么解决方案可以改变这种行为吗?我需要一个与 XSD 字段名称相同的 Java 字段名称和方法名称。我的绑定文件:

<?xml version="1.0" encoding="UTF-8"?>
<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
            xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
            xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
            xsi:schemaLocation="
http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
version="2.1">
  <globalBindings>
    <serializable uid="1" />
    <xjc:simple/>
  </globalBindings>
</bindings>

JAXB simple binding mode modifies collection names to their plural
'version', e.g. "additionalData" becomes "additionalDatas". Is there any solution to change this behavior? I need to have a Java field name and methods name equal to XSD field name. My bindings file:

<?xml version="1.0" encoding="UTF-8"?>
<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
            xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
            xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
            xsi:schemaLocation="
http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
version="2.1">
  <globalBindings>
    <serializable uid="1" />
    <xjc:simple/>
  </globalBindings>
</bindings>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不忘初心 2024-10-27 01:15:34

“简单绑定模式”是JAXB RI (Metro)的扩展功能。将集合属性名称设为复数是其设计的一部分。

来自: http://weblogs.java.net/blog/ kohsuke/archive/2007/01/using_jaxb_ris.html

JAXB RI 中我最喜欢的功能是
更简单、更好的绑定方式,
这使得生成的代码均匀
更易于使用,通过...

  1. 尽可能消除 JAXBElement
    可能的
  2. 给您更好、更多
    一般类型绑定
  3. 使用复数
    适用的属性名称

您可以使用正常的架构自定义来控制属性名称。请参阅我之前的答案之一(下面的链接):

The "simple binding mode" is an extended feature of the JAXB RI (Metro). Making the collection property names plural was part of its design.

From: http://weblogs.java.net/blog/kohsuke/archive/2007/01/using_jaxb_ris.html

My favorite feature in the JAXB RI is
the simpler and better binding mode,
which makes the generated code even
easier to use, by ...

  1. Eliminating JAXBElement as much as
    possible
  2. Giving you a better, more
    typed binding in general
  3. Use plural
    property names where applicable

You may be able to use the normal schema customizations to control the property name. Refer to one of my previous answers (link below):

梦罢 2024-10-27 01:15:34

以下是一个利用 JAXB2 Simplify 插件 其中可以禁用复数化。

 <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxb2-maven-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
      <execution>
        <id>xjc</id>
        <goals>
          <goal>xjc</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <xjbSources>
        <xjbSource>src/main/resources/jaxb_bindings.xjb</xjbSource>
      </xjbSources>
      <sources>
        <source>${basedir}/schema.xsd</source>
      </sources>
      <outputDirectory>${basedir}/target/generated-sources/</outputDirectory>
      <packageName>${groupId}.${artifactId}</packageName>
      <clearOutputDir>true</clearOutputDir>
      <arguments>
        <argument>-Xsimplify-usePluralForm=false</argument>
      </arguments>
    </configuration>
    <dependencies>
<dependency>
  <groupId>org.jvnet.jaxb</groupId>
  <artifactId>jaxb-plugins</artifactId>
  <version>4.0.0</version>
</dependency>
    </dependencies>
  </plugin>

Here is an example plugin configuration that leverages the JAXB2 Simplify Plugin which among other things can disable pluralization.

 <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxb2-maven-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
      <execution>
        <id>xjc</id>
        <goals>
          <goal>xjc</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <xjbSources>
        <xjbSource>src/main/resources/jaxb_bindings.xjb</xjbSource>
      </xjbSources>
      <sources>
        <source>${basedir}/schema.xsd</source>
      </sources>
      <outputDirectory>${basedir}/target/generated-sources/</outputDirectory>
      <packageName>${groupId}.${artifactId}</packageName>
      <clearOutputDir>true</clearOutputDir>
      <arguments>
        <argument>-Xsimplify-usePluralForm=false</argument>
      </arguments>
    </configuration>
    <dependencies>
<dependency>
  <groupId>org.jvnet.jaxb</groupId>
  <artifactId>jaxb-plugins</artifactId>
  <version>4.0.0</version>
</dependency>
    </dependencies>
  </plugin>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文