XJC - compiler was unable to honor this class customization
我想从我的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文件夹中,并定义了目录。
生成项目时,出现错误,因为两种类型具有相同的名称:
1 2 | 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的类型:
1 2 3 4 5 6 7 8 9 10 11 12 | <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中添加了此绑定:
1 2 3 4 5 | <bindings> <binding> <url>http://www.isan.org/schema/v1.11/common/country.xjb</url> </binding> </bindings> |
xjc文件:
1 2 3 4 5 6 7 8 9 10 11 | <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> |
现在,我遇到另一个我无法处理的错误:
1 2 3 4 | [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) |
尝试
1 2 3 | <bindings node="//xs:simpleType[@name='CodingSystemType']"> <typesafeEnumClass name="CountryCodingSystemType" /> </bindings> |
代替。
我认为XJC在自定义枚举和普通类之间有所不同。请参阅相关问题:
- JAXB:匿名简单类型作为枚举吗?
我将添加另一个答案,因为它涉及与架构编译有关的另一个主题。
我还注意到,这两个简单类型实际上属于不同的名称空间。因此,实际上它们首先不应该在同一包中生成。
我想您只是使用
因此,我通常不鼓励使用
1 2 3 4 5 | <bindings schemaLocation="http://www.isan.org/schema/v1.11/common/country.xsd"> <schemaBindings> <package name="org.isan.schema.v1_11.common.country"/> </schemaBindings> </bindings> |
我还建议使用主要/次要架构版本的软件包名称。稍后可能需要并行支持多个架构版本。