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

时间:2021-07-02 17:15:36

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.java
com\test\ws\TestServiceService.java
com\test\obj\ObjectFactory.java
com\test\obj\TestRequest.java
com\test\obj\TestResponse.java
com\test\obj\package-info.java