向JAXB xml添加注释

时间:2013-03-22 12:15:48

标签: java json jaxb

我想为提供给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?

添加注释

1 个答案:

答案 0 :(得分:2)

我假设您正在询问如何让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>

如果上面的插件没有完全符合您的要求(我认为应该这样做,它看起来非常灵活),那么滚动自己的修改应该不会太难(我之前为添加复制构造函数做了这个生成类)。