Apache CXF maven cxf-codegen-plugin (wsdl2java) を使用して、外部 XSD を含む wsdl から JAXB クラスを生成し、XSD で複合型を使用して要素を定義しています。
pom.xml スニペット -
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>3.4.1</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<wsdlOption>
<wsdl>${base.wsdl.path}/myWSDL.wsdl</wsdl>
<extraargs>
<extraarg>-p</extraarg>
<extraarg>${base.package.name}.mywsdl</extraarg>
</extraargs>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
myWSDL.wsdl スニペット -
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="myService"
targetNamespace="http://com.company/myservice/schemas">
<wsdl:types>
<xs:schema xmlns="http://com.company/myservice/schemas"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://com.company/myservice/schemas">
<xsd:include schemaLocation="myXSD.xsd"/>
<xsd:complexType name="myElementType">
<xsd:sequence>
<xsd:element name="MyElementXSD" type="MyElementComplexTypeFromXSD" minOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
<xs:element name="MyElement" type="myElementType"/>
...
...
</xs:schema>
</wsdl:types>
myXSD.xsd -
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://com.company/myservice/schemas" targetNamespace="http://com.company/myservice/schemas">
<complexType name="MyElementComplexTypeFromXSD">
<attribute name="MyAttribute" type="string"/>
</complexType>
</schema>
生成された MyElement.java -
public class MyElement {
@XmlElement(name = "MyElementXSD", namespace="", required = true)
protected MyElementXSD myElementXSD;
}
これらの WSDL および XSD を使用すると、生成された JAXB クラスに明示的に空の名前空間が含まれるため、アンマーシャリング中に問題が発生します。
以下のように WSDL と XSD を変更すると、生成された JAXB クラスは適切になり、アンマーシャリングも正常に機能します。
XSD で複合型の要素を直接作成する -
<?xml version="1.0" encoding="UTF-8"?> <schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://com.company/myservice/schemas" targetNamespace="http://com.company/myservice/schemas"> <element name="MyElementXSD"> <complexType > <attribute name="MyAttribute" type="string"/> </complexType> </element> </schema>WSDL で「type」の代わりに「ref」を使用して直接参照する -
<xsd:complexType name="myElementType"> <xsd:sequence> <xsd:element ref="MyElementXSD" minOccurs="1"/> </xsd:sequence> </xsd:complexType>次に、生成されたクラス @XmlElement が - に変更されます
public class MyElement { @XmlElement(name = "MyElementXSD", required = true) protected MyElementXSD myElementXSD; }
しかし、私は提供された WSDL と XSD に自由に触れることができないため、CXF maven プラグインに引数を渡す/バインディング ファイルを使用するなどの解決策/提案を探しています。