本文主要是介绍maven+webservice+客户端调用-基于契约优先规则,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
这是一篇基于契约优先规则的webservice远程服务调用项目,采用cxf
1.首先创建一个maven项目
添加pom依赖
<?xml version="1.0" encoding="UTF-8"?><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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.cxf</groupId><artifactId>maven-cxf</artifactId><version>1.0-SNAPSHOT</version><modules><module>maven-client</module><module>maven-client</module></modules><packaging>pom</packaging><name>maven-cxf Maven Webapp</name><!-- FIXME change it to the project's website --><url>http://www.example.com</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.7</maven.compiler.source><maven.compiler.target>1.7</maven.compiler.target><!-- ##########依赖属性参数配置 start############### --><junit.version>4.11</junit.version><cxf.version>2.2.3</cxf.version><spring.version>3.2.3.RELEASE</spring.version><slf4j.version>1.7.7</slf4j.version></properties><dependencies><!-- 单元测试依赖包 --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version></dependency><!-- CXF Dependencies --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>${cxf.version}</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http</artifactId><version>${cxf.version}</version></dependency><!-- Jetty is needed if you're are not using the CXFServlet --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http-jetty</artifactId><version>${cxf.version}</version></dependency><!-- End of CXF Dependencies --><!-- Spring Dependencies ${spring.version} --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>${spring.version}</version></dependency><!--<dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>${slf4j.version}</version><type>jar</type><scope>compile</scope></dependency>--></dependencies><build><finalName>maven-cxf</finalName><pluginManagement><!-- lock down plugins versions to avoid using Mavendefaults (may be moved to parent pom) --><plugins><plugin><artifactId>maven-clean-plugin</artifactId><version>3.0.0</version></plugin><!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --><plugin><artifactId>maven-resources-plugin</artifactId><version>3.0.2</version></plugin><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.7.0</version></plugin><plugin><artifactId>maven-surefire-plugin</artifactId><version>2.20.1</version></plugin><plugin><artifactId>maven-war-plugin</artifactId><version>3.2.0</version></plugin><plugin><artifactId>maven-install-plugin</artifactId><version>2.5.2</version></plugin><plugin><artifactId>maven-deploy-plugin</artifactId><version>2.8.2</version></plugin></plugins></pluginManagement></build>
</project>
2.在src目录下建个META-INF目录,并在下面建立mywsdl.wsdl
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"xmlns:tns="http://www.example.org/mywsdl/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="MyServiceImplService"targetNamespace="http://www.example.org/mywsdl/"><wsdl:types><xsd:schema targetNamespace="http://www.example.org/mywsdl/"><!-- 1.1 编写元素add,addResponse,minus,minusResponse --><xsd:element name="add" type="tns:add"></xsd:element><xsd:element name="addResponse" type="tns:addResponse"></xsd:element><xsd:element name="minus" type="tns:minus"></xsd:element><xsd:element name="minusResponse" type="tns:minusResponse"></xsd:element><!-- 1.2 编写元素的类型 --><xsd:complexType name="add"><xsd:sequence><xsd:element name="a" type="xsd:int"></xsd:element><xsd:element name="b" type="xsd:int"></xsd:element></xsd:sequence></xsd:complexType><xsd:complexType name="addResponse"><xsd:sequence><xsd:element name="addResult" type="xsd:int"></xsd:element></xsd:sequence></xsd:complexType><xsd:complexType name="minus"><xsd:sequence><xsd:element name="c" type="xsd:int"></xsd:element><xsd:element name="d" type="xsd:int"></xsd:element></xsd:sequence></xsd:complexType><xsd:complexType name="minusResponse"><xsd:sequence><xsd:element name="minusResult" type="xsd:int"></xsd:element></xsd:sequence></xsd:complexType></xsd:schema></wsdl:types><!-- 1.3 编写message --><wsdl:message name="add"><wsdl:part name="add" element="tns:add"></wsdl:part></wsdl:message><wsdl:message name="addResponse"><wsdl:part name="addResponse" element="tns:addResponse"></wsdl:part></wsdl:message><wsdl:message name="minus"><wsdl:part name="minus" element="tns:minus"></wsdl:part></wsdl:message><wsdl:message name="minusResponse"><wsdl:part name="minusResponse" element="tns:minusResponse"></wsdl:part></wsdl:message><!--1.4 编写port,其中operation为方法 --><wsdl:portType name="IMyService"><wsdl:operation name="add"><wsdl:input message="tns:add"></wsdl:input><wsdl:output message="tns:addResponse"></wsdl:output></wsdl:operation><wsdl:operation name="minus"><wsdl:input message="tns:minus"></wsdl:input><wsdl:output message="tns:minusResponse"></wsdl:output></wsdl:operation></wsdl:portType><!--1.5 编写binding,其中document类型为默认 --><wsdl:binding name="myServiceSOAP" type="tns:IMyService"><soap:binding style="document"transport="http://schemas.xmlsoap.org/soap/http" /><wsdl:operation name="add"><!-- <soap:operation soapAction="http://www.example.org/mywsdl/NewOperation"/> --><wsdl:input><soap:body use="literal" /></wsdl:input><wsdl:output><soap:body use="literal" /></wsdl:output></wsdl:operation><wsdl:operation name="minus"><wsdl:input><soap:body use="literal" /></wsdl:input><wsdl:output><soap:body use="literal" /></wsdl:output></wsdl:operation></wsdl:binding><!-- 1.6 编写service --><wsdl:service name="MyServiceImplService"><!-- 与公布接口的name一致 --><wsdl:port binding="tns:myServiceSOAP" name="MyServiceImplPort"><soap:address location="http://localhost:8989/ms" /><!--发布地址 --></wsdl:port></wsdl:service>
</wsdl:definitions>
3.利用wsimport命令生成服务端代码
4.建立实现类MyServiceImpl.java
package main.webapp.serviceImpl;import org.example.mywsdl.IMyService;import javax.jws.WebService;@WebService(endpointInterface="org.example.mywsdl.IMyService",targetNamespace="http://www.example.org/mywsdl/",wsdlLocation="META-INF/mywsdl.wsdl")
public class MyServiceImpl implements IMyService {@Overridepublic int add(int a, int b) {int result = a+b;System.out.println("The result is:"+result);return result;}@Overridepublic int minus(int c, int d) {int result = c - d;System.out.println("The result is:"+result);return result;}
}
5.建立服务并发布服务 MyService.java
package main.webapp.service;import main.webapp.serviceImpl.MyServiceImpl;import javax.xml.ws.Endpoint;public class MyService {/*** @param args*/public static void main(String[] args) {Endpoint.publish("http://localhost:8989/ms", new MyServiceImpl());System.out.println("The service is running.................");}
}
发布服务,可以通过浏览器看到该该服务的wsdl
<!--Published by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e.
-->
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.example.org/mywsdl/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="MyServiceImplService" targetNamespace="http://www.example.org/mywsdl/">
<wsdl:types>
<xsd:schema targetNamespace="http://www.example.org/mywsdl/">
<!-- 1.1 编写元素add,addResponse,minus,minusResponse -->
<xsd:element name="add" type="tns:add"/>
<xsd:element name="addResponse" type="tns:addResponse"/>
<xsd:element name="minus" type="tns:minus"/>
<xsd:element name="minusResponse" type="tns:minusResponse"/>
<!-- 1.2 编写元素的类型 -->
<xsd:complexType name="add">
<xsd:sequence>
<xsd:element name="a" type="xsd:int"/>
<xsd:element name="b" type="xsd:int"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="addResponse">
<xsd:sequence>
<xsd:element name="addResult" type="xsd:int"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="minus">
<xsd:sequence>
<xsd:element name="c" type="xsd:int"/>
<xsd:element name="d" type="xsd:int"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="minusResponse">
<xsd:sequence>
<xsd:element name="minusResult" type="xsd:int"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
</wsdl:types>
<!-- 1.3 编写message -->
<wsdl:message name="add">
<wsdl:part name="add" element="tns:add"/>
</wsdl:message>
<wsdl:message name="addResponse">
<wsdl:part name="addResponse" element="tns:addResponse"/>
</wsdl:message>
<wsdl:message name="minus">
<wsdl:part name="minus" element="tns:minus"/>
</wsdl:message>
<wsdl:message name="minusResponse">
<wsdl:part name="minusResponse" element="tns:minusResponse"/>
</wsdl:message>
<!-- 1.4 编写port,其中operation为方法 -->
<wsdl:portType name="IMyService">
<wsdl:operation name="add">
<wsdl:input message="tns:add"/>
<wsdl:output message="tns:addResponse"/>
</wsdl:operation>
<wsdl:operation name="minus">
<wsdl:input message="tns:minus"/>
<wsdl:output message="tns:minusResponse"/>
</wsdl:operation>
</wsdl:portType>
<!-- 1.5 编写binding,其中document类型为默认 -->
<wsdl:binding name="myServiceSOAP" type="tns:IMyService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="add">
<!--<soap:operation soapAction="http://www.example.org/mywsdl/NewOperation"/>
-->
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="minus">
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<!-- 1.6 编写service -->
<wsdl:service name="MyServiceImplService">
<!-- 与公布接口的name一致 -->
<wsdl:port binding="tns:myServiceSOAP" name="MyServiceImplPort">
<soap:address location="http://localhost:8989/ms"/>
<!-- 发布地址 -->
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
6.利用该wsdl,通过wsimport命令生成客户端
7.建立测试类测试
import org.example.mywsdl.IMyService;
import org.example.mywsdl.MyServiceImplService;
import org.junit.Test;public class WebService {@Testpublic void testClient2(){MyServiceImplService service=new MyServiceImplService();IMyService ms=service.getMyServiceImplPort();System.out.println(ms.add(3,5));}
}
结果如下:
这篇关于maven+webservice+客户端调用-基于契约优先规则的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!