我想从我的 Java 项目调用 ISAN Restful API,所以我尝试使用 maven-jaxb2-plugin 从 xsd 文件生成 java bean。这是 xsds:
- http://www.isan.org/schema/v1.11/common/common.xsd
- http://www.isan.org/schema/v1.21/common/serial.xsd
- http://www.isan.org/schema/v1.11/common/version.xsd
- http://www.isan.org/ISAN/isan.xsd
- http://www.isan.org/schema/v1.11/common/title.xsd
- http://www.isan.org/schema/v1.11/common/externalid.xsd
- http://www.isan.org/schema/v1.11/common/participant.xsd
- http://www.isan.org/schema/v1.11/common/language.xsd
- http://www.isan.org/schema/v1.11/common/country.xsd
我下载了这些文件并将它们复制到我的 src/main/resources 文件夹中并定义了一个目录。 当我构建项目时,出现错误,因为两种类型具有相同的名称:
org.xml.sax.SAXParseExceptionpublicId: http://www.isan.org/schema/v1.11/common/language; systemId: http://www.isan.org/schema/v1.11/common/language.xsd; lineNumber: 39; columnNumber: 48; A class/interface with the same name "org.isan.CodingSystemType" is already in use. Use a class customization to resolve this conflict.
org.xml.sax.SAXParseExceptionpublicId: http://www.isan.org/schema/v1.11/common/country; systemId: http://www.isan.org/schema/v1.11/common/country.xsd; lineNumber: 39; columnNumber: 48; (Relevant to above error) another "CodingSystemType" is generated from here.
这是正确的:language.xsd 和 country.xsd 都定义了一个名为 CodingSystemType 的类型:
<xs:simpleType name="CodingSystemType">
<xs:restriction base="xs:string">
<xs:enumeration value="ISO639_2"/>
<xs:enumeration value="RFC3066"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="CodingSystemType">
<xs:restriction base="xs:string">
<xs:enumeration value="ISO3166_1"/>
</xs:restriction>
</xs:simpleType>
按照建议,我尝试对 country.xsd 类型使用类自定义。我在 pom.xml 中添加了这个绑定(bind):
<bindings>
<binding>
<url>http://www.isan.org/schema/v1.11/common/country.xjb</url>
</binding>
</bindings>
xjc 文件:
<bindings version="2.0" xmlns="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" xmlns:annox="http://annox.dev.java.net"
xmlns:namespace="http://jaxb2-commons.dev.java.net/namespace-prefix">
<bindings schemaLocation="http://www.isan.org/schema/v1.11/common/country.xsd">
<bindings node="//xs:simpleType[@name='CodingSystemType']">
<class name="CountryCodingSystemType" />
</bindings>
</bindings>
</bindings>
现在,我遇到另一个我无法处理的错误:
[ERROR] Error while parsing schema(s).Location [ http://www.isan.org/schema/v1.11/common/country.xjb{7,58}].
com.sun.istack.SAXParseException2; systemId: http://www.isan.org/schema/v1.11/common/country.xjb; lineNumber: 7; columnNumber: 58; compiler was unable to honor this class customization. It is attached to a wrong place, or its inconsistent with other bindings.
[ERROR] Error while parsing schema(s).Location [ http://www.isan.org/schema/v1.11/common/country.xsd{39,48}].
com.sun.istack.SAXParseException2; systemId: http://www.isan.org/schema/v1.11/common/country.xsd; lineNumber: 39; columnNumber: 48; (the above customization is attached to the following location in the schema)
最佳答案
尝试
<bindings node="//xs:simpleType[@name='CodingSystemType']">
<typesafeEnumClass name="CountryCodingSystemType" />
</bindings>
相反。
我认为 XJC 使自定义枚举和普通类有所不同。查看相关问题:
关于java - XJC - 编译器无法接受此类定制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40468120/