问题描述
我想在提供给JAXB的XML文件中添加注释.
I want add an annotation to the XML file provided to JAXB.
我们有一个要求,我将使用相同的属性名称和数据类型创建对象,但为其使用不同的JSON密钥.
We have a requirement where I will create the objects with same attribute names and datatypes but will have different JSON keys for them.
基于提供的注释,JSON中的键名将有所不同.这些注释是GSON注释,例如:@SerializedName(您想要的JSON的键名").
The key names in JSON will differ on the basis of annotation provided. These annotations are GSON annotations for example: @SerializedName("key name of JSON you would like").
我试图从我发布的问题中得到一些投入
I tried to get some inputs from the question I posted here
但无法真正获得任何解决方案.
But could not really get any solution.
有人有什么建议吗?
我将添加一些XML模式进行解释.
I will add some XML schema to explain.
<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添加注释吗?
Now in above schema can I add an annotation to attribute Pos?
推荐答案
我假设您正在询问如何让JAXB编译器自动注释生成的类.有一个用于添加注释的JAXB插件: http://confluence.highsource.org/display/J2B /Annotate + Plugin
I assume that you're asking how to have the JAXB compiler automatically annotate the generated classes. There's a JAXB plugin for adding annotations: http://confluence.highsource.org/display/J2B/Annotate+Plugin
您可以像这样将其挂接到Maven构建的generate-sources
阶段:
You can hook it into the generate-sources
phase of a Maven build like so:
<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>
如果上面的插件不能完全满足您的要求(我认为应该如此,它看起来很灵活),那么进行自己的修改就不会太困难(我在添加副本构造函数之前已经做了此事)到生成的类).
If the above plugin doesn't do exactly what you want (which I think it should, it looks pretty flexible), it shouldn't be too difficult to roll your own modification (I have done this before for adding copy constructors to generated classes).
这篇关于向JAXB xml添加注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!