【问题标题】:Adding annotation to JAXB xml向 JAXB xml 添加注释
【发布时间】:2013-03-22 12:15:48
【问题描述】:

我想为提供给 JAXB 的 XML 文件添加注释。

我们有一个要求,我将创建具有相同属性名称和数据类型但具有不同 JSON 键的对象。

JSON 中的键名将根据提供的注释而有所不同。这些注解是 GSON 注解,例如:@SerializedName("您想要的 JSON 的键名")。

我试图从我发布的here 的问题中获得一些输入

但无法真正得到任何解决方案。

有人有什么建议吗?

我将添加一些 XML 模式来解释。

<xsd:complexType name="RouteType">
 <xsd:attribute name="Pos" type="xsd:int" use="optional" default="1"/>
 <xsd:attribute name="Dir" type="DirType" use="required"/>
</xsd:complexType>

现在我可以在上面的模式中为属性 Pos 添加注释吗?

【问题讨论】:

  • 您说的向 JAXB XML 添加注释究竟是什么意思?
  • 我想我会稍微修改一下问题,以便于理解。

标签: java json jaxb


【解决方案1】:

我假设您问的是如何让 JAXB 编译器自动注释生成的类。有一个用于添加注释的 JAXB 插件:http://confluence.highsource.org/display/J2B/Annotate+Plugin

您可以将它挂接到 Maven 构建的 generate-sources 阶段,如下所示:

<build>
    <!-- snip -->
    <plugins>
        <plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <version>0.8.2</version>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <schemaIncludes>
                            <include>path/to/your/schema.xsd</include>
                        </schemaIncludes>
                        <bindingIncludes>
                            <include>path/to/your/custom-bindings.xjb</include> <!-- if you choose to use a custom bindings file instead of inline annotations in the xsd -->
                        </bindingIncludes>
                        <forceRegenerate>true</forceRegenerate>
                        <extension>true</extension>
                        <episode>false</episode>
                        <args>
                            <arg>-Xannotate</arg>
                        </args>
                        <plugins>
                            <plugin>
                                <groupId>org.jvnet.jaxb2_commons</groupId>
                                <artifactId>jaxb2-basics-annotate</artifactId>
                                <version>0.6.4</version>
                            </plugin>
                        </plugins>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

如果上面的插件不能完全满足你的要求(我认为它应该这样做,它看起来很灵活),滚动你自己的修改应该不会太困难(我之前已经这样做了添加复制构造函数到生成的类)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-09
    • 1970-01-01
    • 1970-01-01
    • 2016-08-31
    • 1970-01-01
    相关资源
    最近更新 更多