我正在尝试对生成的 JAXB 类强制执行枚举验证,但是在绑定它们时遇到了一些问题。
基本的 XSD 设置是:
-enumsXSD
-2 other XSDs that import this XSD
为了证明我的课程重复我正在使用剧集,但是在 enumsXSD 中添加注释时看起来这并不好
com.sun.istack.SAXParseException2: compiler was unable to honor this annox:annotateEnumValueMethod customization. It is attached to a wrong place, or its inconsistent with other bindings.
[ERROR] Error while generating code.Location [ file:somewhere/generic.episode{64,99}].
com.sun.istack.SAXParseException2: (the above customization is attached to the following location in the schema)
编码:
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.13.3</version>
<configuration>
<extension>true</extension>
<args>
<arg>-Xannotate</arg>
</args>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-annotate</artifactId>
<version>1.0.4</version>
</plugin>
</plugins>
</configuration>
<executions>
<!--GENERIC ENUMS -->
<execution>
<id>ENUMS</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<extension>true</extension>
<generatePackage>com.foo.generic.enums</generatePackage>
<generateDirectory>${project.build.directory}/generated-sources/xjc1/generic</generateDirectory>
<!-- Define the directory where we should find the XSD files -->
<schemaDirectory>
src/main/resources/dtd/generic
</schemaDirectory>
<schemaIncludes>
<source>enums.xsd</source>
</schemaIncludes>
<episodeFile>
${project.build.directory}/generated-sources/xjc1/generic/META-INF/generic.episode
</episodeFile>
</configuration>
</execution>
<execution>
<id>A_XSD</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<extension>true</extension>
<bindingDirectory>${project.build.directory}/generated-sources/xjc1/generic/META-INF</bindingDirectory>
<bindingIncludes>
<include>generic.episode</include>
</bindingIncludes>
<!-- Set the package of the generated code -->
<generatePackage>com.foo.something</generatePackage>
<generateDirectory>${project.build.directory}/generated-sources/xjc1/a_something</generateDirectory>
<!-- Define the directory where we should find the XSD files -->
<schemaDirectory>
src/main/resources/dtd/someplace/a/
</schemaDirectory>
<schemaIncludes>
<source>*.xsd</source>
</schemaIncludes>
</configuration>
</execution>
</executions>
<plugin>
</plugins>
并在枚举 XSD
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema elementFormDefault="qualified" id="commonEnums"
targetNamespace="http://foo.com/xsd/commons/enum"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
jaxb:version="2.1"
xmlns:annox="http://annox.dev.java.net"
jaxb:extensionBindingPrefixes="annox">
<xs:simpleType name="bulletinCategory">
<xs:annotation>
<xs:appinfo>
<annox:annotateEnumValueMethod>@java.lang.Deprecated</annox:annotateEnumValueMethod>
</xs:appinfo>
</xs:annotation>
<xs:restriction base="xs:string">
<xs:enumeration value="valueA" />
<xs:enumeration value="valueB" />
<xs:enumeration value="valueC" />
</xs:restriction>
</xs:simpleType>
</xs>
理想情况下,@deprecated 应该是@XmlJavaTypeAdapter(FooAdapter.class)
,但我认为让我们从小处着手。