使用Maven的jaxws-maven-plugin插件,将wsdl生成java

本文介绍了如何在Maven工程中利用jaxws-maven-plugin插件,从wsdl文件生成Java代码。首先,配置好标准的Maven工程,然后在指定目录下放置所需的binding文件,如Global_jaxb.xml和TestService.wsdl。同时,文章还推荐了一位老师的人工智能教程,适合零基础学习者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

               

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



           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.youkuaiyun.com/jiangjunshow

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值