简体   繁体   中英

Customize in wsdl imported xsd: how to get to the rootelement

I am have a wsdl file that imports an xsd. In my pom.xml I use Apache-CXF and wsdl2java to generate proxy classes, but one needs to be changed by name. It should be possible because the other way around, generating from xsd file, the class name customizing works. The difficulty here is that I do not know how to reach my imported xsd rootelement. I have used an example project for simplicity. The wsdl:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wsdl:definitions
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap12/"
xmlns:tns="http://www.briansdevblog.com/Accounts/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="Accounts"
targetNamespace="http://www.briansdevblog.com/Accounts/"
xmlns:accounts="http://com/blog/demo/webservices/accountservice">
<wsdl:types>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:import
namespace="http://com/blog/demo/webservices/accountservice"
schemaLocation="../schema/AccountService.xsd">
</xsd:import>
</xsd:schema>
</wsdl:types>
<wsdl:message name="AccountDetailsRequest">
<wsdl:part element="accounts:AccountDetailsRequest"
name="parameters" />
</wsdl:message>
<wsdl:message name="AccountDetailsResponse">
<wsdl:part element="accounts:AccountDetailsResponse"
name="parameters" />
</wsdl:message>
<wsdl:portType name="AccountService">
<wsdl:operation name="GetAccountDetails">
<wsdl:input message="tns:AccountDetailsRequest" />
<wsdl:output message="tns:AccountDetailsResponse" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="AccountServiceSoapBinding"
type="tns:AccountService">
<soap:binding style="document"
transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="GetAccountDetails">
<soap:operation
soapAction="http://www.briansjavablog.com/Accounts/GetAccountDetails" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="AccountService">
<wsdl:port binding="tns:AccountServiceSoapBinding"
name="AccountsPort">
<soap:address
location="http://localhost:8080/apache-cfx-demo/services/accounts" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

The xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://com/blog/demo/webservices/accountservice"
xmlns:account="http://webservices.demo.blog.com"
targetNamespace="http://com/blog/demo/webservices/accountservice"
elementFormDefault="qualified">
<xsd:complexType name="Account">
<xsd:sequence>
<xsd:element name="AccountNumber" type="xsd:string"/>
<xsd:element name="AccountName" type="xsd:string"/>
<xsd:element name="AccountBalance" type="xsd:double"/>
<xsd:element name="AccountStatus" type="EnumAccountStatus"/>
</xsd:sequence>
</xsd:complexType>
<xsd:simpleType name="EnumAccountStatus">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Active"/>
<xsd:enumeration value="Inactive"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:element name="AccountDetailsRequest">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="accountNumber" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="AccountDetailsResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="AccountDetails" type="Account"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>

The pom plugin code:

<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>${cxf.version}</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<sourceRoot>${basedir}/target/generated/java</sourceRoot>
<wsdlOptions>
<wsdlOption>
<wsdl>${basedir}/src/main/resources/wsdl/Accounts.wsdl</wsdl>
<bindingFiles>
<bindingFile>${basedir}/src/main/resources/custom/custom.xjb</bindingFile>
</bindingFiles>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>

and the xjb file with my documented attempts:

<?xml version="1.0" encoding="UTF-8"?>
<jaxws:bindings wsdlLocation="../wsdl/Accounts.wsdl"
xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<!-- option 2
<jaxws:bindings node="wsdl:definitions">
<jaxws:bindings node="wsdl:types" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb">
<jaxb:bindings schemaLocation="../schema/AccountService.xsd" node="/xsd:schema">
<jaxb:bindings node="//xsd:complexType[@name='Account']">
<jaxb:class name="AccountEx"/>
</jaxb:bindings>
</jaxb:bindings>
</jaxws:bindings>
</jaxws:bindings>
  no errors but also no effect
-->
<!-- option 1
<jaxws:bindings node="wsdl:definitions">
<jaxws:bindings node="wsdl:types" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb">
<jaxb:bindings schemaLocation="../schema/AccountService.xsd" node="/xsd:schema">
<jaxb:bindings xmlns:xsd="http://www.w3.org/2001/XMLSchema" node="//schema/xs:import[@namespace='http://com/blog/demo/webservices/accountservice']/xsd:complexType[@name='Account']">
<jaxb:class name="AccountEx"/>
</jaxb:bindings>
</jaxb:bindings>
</jaxws:bindings>
  </jaxws:bindings>
  no errors but also no effect
  -->
</jaxws:bindings>

Ok, I got it solved, thanks to https://stackoverflow.com/a/20245920/10017087 It's simpler than I thought: 1. jaxws:bindings are for wsdl edits, use jaxb:bindings for xsd edits. 2. you only have to indicate the xsd location (jaxb:bindings schemaLocation)

<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" jaxb:version="2.0">
    <jaxb:bindings
        schemaLocation="../schema/AccountService.xsd">
        <jaxb:bindings node="//xsd:complexType[@name='Account']">
            <jaxb:class name="AccountEx" />
        </jaxb:bindings>
    </jaxb:bindings>
</jaxb:bindings>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM