我有一个导入xsd的wsdl文件。在pom.xml中,我使用ApacheCXF和wsdl2java生成代理类,但其中一个需要更改名称。这应该是可能的,因为反过来,从xsd文件生成类名自定义是可行的。这里的困难在于,我不知道如何访问导入的xsd根元素。为了简单起见,我使用了一个示例项目。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>
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>
pom插件代码:
<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>
以及xjb文件和我记录的尝试:
<?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>
好的,我解决了它,多亏了https://stackoverflow.com/a/20245920/10017087它比我想象的要简单:1.jaxws:bbindings用于wsdl编辑,使用jaxb:bbindings进行xsd编辑。2.您只需要指示xsd位置(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>