我正在研究JAXB / SOAP项目,并且在我的XSD模式文件中出现“两个全局组件不能具有相同的名称”错误。我尝试按照其他帖子中的建议使用外部绑定和架构绑定,但无法使其正常工作。一定是简单的事情,我只是想念而已,但是我已经尝试了所有我能想到的一切,但是没有运气。任何帮助,将不胜感激。这是我在做什么:
每个SOAP服务/目标名称空间可以有多个请求/响应,并且每个请求都有自己的模式文件。我希望它们的类都在同一个Java包中,所以我使用模式绑定来分配所需的包名称。但是XJC每个目标名称空间只允许一个这样的绑定(即,多个文件不能具有相同的绑定),因此我创建了一个“ master” xsd,其中包含所有其他xsd文件。第一个包含该名称空间中所有请求/响应的程序包名称的架构绑定。在我添加了一个包含与另一个请求同名的字段的请求之前,此方法一直很好,给了我上面提到的错误。
基于其他地方的建议和示例,我首先尝试在新添加的xsd中进行模式绑定,因为我希望绑定与它们所引用的元素位于同一文件中。那没有用,所以我在'bindings.xjb'中尝试了外部绑定,那也没有用。我想我要更改的元素的XPATH可能有问题,但是如果存在,我看不到它。这些是文件:
位于src / main / xsd / service1 / Service1.xsd中的Master XSD,以及所有其他架构:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetamespace="http://services.myco.com/service1"
xmlns:service1="http://services.myco.com/service1"
elementFormDefault="qualified"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
jaxb:version="2.1" >
<xs:include schemaLocation="AuthenticationHeader.xsd"/>
<xs:include schemaLocation="Request1.xsd"/>
<xs:include schemaLocation="Response1.xsd"/>
<xs:include schemaLocation="Request2.xsd"/>
</xs:schema>
Auth header XSD
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
targetNamespace="http://services.myco.com/service1"
xmlns:service1="http://services.myco.com/service1"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
jaxb:version="2.1" >
<xs:annotation>
<xs:appinfo>
<jaxb:schemaBindings >
<jaxb:package name="com.myco.soap.api.service1"/>
</jaxb:schemaBindings>
</xs:appinfo>
</xs:annotation>
<xs:element name="AuthenticationHeader">
<xs:complexType>
<xs:sequence>
<xs:element ref="service1:Username"/>
<xs:element ref="service1:Password"/>
<xs:element ref="service1:DataSource"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Username" type="xs:string"/>
<xs:element name="Password" type="xs:string"/>
<xs:element name="DataSource" type="xs:string"/>
</xs:schema>
Request1 XSD
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
targetNamespace="http://services.myco.com/service1"
xmlns:operator="http://services.myco.com/service1"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
jaxb:version="2.0" >
<xs:element name="Request1">
<xs:complexType>
<xs:sequence>
<xs:element ref="service1:customerName" minOccurs="0"/>
<xs:element ref="service1:customerId" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="customerName" type="xs:string"/>
<xs:element name="customerId" type="xs:integer"/>
</xs:schema>
为简洁起见,省略了Responses1 XSD
显示了无效工作模式绑定的Request2 XSD
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
targetNamespace="http://services.myco.com/service1"
xmlns:operator="http://services.myco.com/service1"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
jaxb:version="2.1" >
<xs:annotation>
<xs:appinfo>
<jaxb:schemaBindings>
<jaxb:bindings node="//xs:element[@name='customerName']">
<property name="customerNameRq2"/>
</jaxb:bindings>
</jaxb:schemaBindings>
</xs:appinfo>
</xs:annotation>
<xs:element name="Request2">
<xs:complexType>
<xs:sequence>
<xs:element ref="service1:customerName"/>
<xs:element ref="service1:customerId"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="customerName" type="xs:string"/>
<xs:element name="customerId" type="xs:integer"/>
</xs:schema>
位于src / main / xjb / bindings.xjb中的外部绑定XJB
<jaxb:bindings xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
version="2.1">
<jaxb:bindings schemaLocation="../xsd/service1/Request2.xsd" node="/xs:schema">
<jaxb:schemaBindings>
<jaxb:bindings node="//xs:element[@name='customerName']">
<property name="customerNameRq2"/>
</jaxb:bindings>
</jaxb:schemaBindings>
</jaxb:bindings>
</jaxb:bindings>
在此先感谢您的帮助。