使用Maven的jaxws-maven-plugin外掛程式,將wsdl產生java

來源:互聯網
上載者:User

1、準備一個標準的maven工程,將pom.xml修改成如下:

<pre name="code" class="xml"><?xml version="1.0" encoding="ISO-8859-1"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="   http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.test</groupId><artifactId>TEST_WS</artifactId><packaging>jar</packaging><name>TEST WS</name><version>1.0.0</version><description>Web service</description><properties><!-- 這個目錄用於存放產生後的JAVA,如果不存在就先建立好了 --><genSource.dir>src/gen/java</genSource.dir><!-- 指定bingding檔案所在的目錄,在這個樣本中只指定了bindingDirectory,而沒有指定bindingFile,那說明只要是這個目錄下面的所有xml檔案都會被使用 --><!-- 使用協助,參看:http://www.oracle.com/technetwork/articles/entarch/jax-ws-jaxb-customization-082750.html, http://jax-ws-commons.java.net/jaxws-maven-plugin/wsimport-mojo.html#bindingDirectory --><jaxwsBing.dir>src/jaxws/testService</jaxwsBing.dir><!-- WSDL及xsd檔案所在的路徑,只需要指明wsdl檔案即可,因為wsdl檔案中會引用到xsd --><wsdlFile>testService/TestService.wsdl</wsdlFile></properties><build><plugins><plugin><groupId>org.codehaus.mojo</groupId><artifactId>jaxws-maven-plugin</artifactId><version>1.12</version><dependencies><dependency><artifactId>jsr181</artifactId><groupId>javax.jws</groupId><version>1.0</version></dependency></dependencies><configuration><destDir /> <!-- don't need .class files --><extension>true</extension><keep>true</keep><sourceDestDir>${project.basedir}/${genSource.dir}</sourceDestDir><target>2.1</target><verbose>true</verbose></configuration><executions><execution><id>wsimport-IsatService</id><phase>process-sources</phase><goals><goal>wsimport</goal></goals><configuration><bindingDirectory>${project.basedir}/${jaxwsBing.dir}</bindingDirectory><wsdlFiles><wsdlFile>${wsdlFile}</wsdlFile></wsdlFiles></configuration></execution></executions></plugin></plugins></build><dependencies><dependency><artifactId>jaxws-api</artifactId><groupId>javax.xml.ws</groupId><scope>provided</scope><version>2.1-1</version></dependency></dependencies></project>

2、在目錄src/jaxws/testService下面準備好以下三個binging檔案:

Global_jaxb.xml:

<?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" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"version="2.0" jaxb:extensionBindingPrefixes="xjc"><jaxb:globalBindings underscoreBinding="asCharInWord"><jaxb:serializable uid="1" /><xjc:simple /></jaxb:globalBindings></jaxb:bindings>

TestService_jaxb.xml:

<?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" schemaLocation="../../wsdl/testService/TestElement.xsd"node="/xsd:schema" version="2.0"><jaxb:schemaBindings><jaxb:package name="com.test.obj" /></jaxb:schemaBindings></jaxb:bindings>

TestService_jaxws.xml:

<?xml version="1.0" encoding="UTF-8"?><jaxws:bindings xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"wsdlLocation="../../wsdl/testService/TestService.wsdl" version="2.0"jaxb:extensionBindingPrefixes="xjc"><jaxws:bindings node="wsdl:definitions"><jaxws:enableWrapperStyle>false</jaxws:enableWrapperStyle><jaxws:package name="com.test.ws" /></jaxws:bindings></jaxws:bindings>

3。在目錄src/wsdl/testService下面準備TestService.wsdl及TestElement.xsd檔案,內容分別如下,

TestService.wsdl:

<?xml version="1.0" encoding="UTF-8"?><wsdl:definitions name="TestService"targetNamespace="http://test.com/TestService" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"xmlns:tns="http://test.com/TestService" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"xmlns:xsd="http://www.w3.org/2001/XMLSchema"><wsdl:types><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"><xsd:import namespace="http://test.com/TestService" schemaLocation="./TestElement.xsd"></xsd:import></xsd:schema></wsdl:types><wsdl:message name="testRequest"><wsdl:part name="parameters" element="tns:testRequest"></wsdl:part></wsdl:message><wsdl:message name="testResponse"><wsdl:part name="parameters" element="tns:testResponse"></wsdl:part></wsdl:message><wsdl:portType name="TestService"><wsdl:operation name="test"><wsdl:input message="tns:testRequest"></wsdl:input><wsdl:output message="tns:testResponse"></wsdl:output></wsdl:operation></wsdl:portType><wsdl:binding name="TestBinding" type="tns:TestService"><soap:binding style="document"transport="http://schemas.xmlsoap.org/soap/http" /><wsdl:operation name="test"><soap:operation soapAction="" /><wsdl:input><soap:body use="literal" /></wsdl:input><wsdl:output><soap:body use="literal" /></wsdl:output></wsdl:operation></wsdl:binding><wsdl:service name="TestService_Service"><wsdl:port name="TestPort" binding="tns:TestBinding"><soap:address location="http://localhost:8080/TestService/TestService_Service" /></wsdl:port></wsdl:service></wsdl:definitions>

TestElement.xsd:

<?xml version="1.0" encoding="UTF-8"?><schema targetNamespace="http://test.com/TestService"elementFormDefault="qualified" xmlns="http://www.w3.org/2001/XMLSchema"xmlns:tns="http://test.com/TestService">    <element name="testRequest"><complexType><sequence><element name="USER_NAME" type="string"></element></sequence></complexType></element><element name="testResponse"><complexType><sequence><element name="AGE" type="string" /></sequence></complexType></element></schema>

4.執行命令mvn clean install,如果出現如下提現,則說明配置成功:

parsing WSDL...generating code...com\test\ws\TestService.javacom\test\ws\TestServiceService.javacom\test\obj\ObjectFactory.javacom\test\obj\TestRequest.javacom\test\obj\TestResponse.javacom\test\obj\package-info.java

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.