maven+webservice+客户端调用-基于契约优先规则

2024-05-07 07:32

本文主要是介绍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+客户端调用-基于契约优先规则的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/966710

相关文章

C#如何调用C++库

《C#如何调用C++库》:本文主要介绍C#如何调用C++库方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录方法一:使用P/Invoke1. 导出C++函数2. 定义P/Invoke签名3. 调用C++函数方法二:使用C++/CLI作为桥接1. 创建C++/CL

Maven中引入 springboot 相关依赖的方式(最新推荐)

《Maven中引入springboot相关依赖的方式(最新推荐)》:本文主要介绍Maven中引入springboot相关依赖的方式(最新推荐),本文给大家介绍的非常详细,对大家的学习或工作具有... 目录Maven中引入 springboot 相关依赖的方式1. 不使用版本管理(不推荐)2、使用版本管理(推

解决Maven项目idea找不到本地仓库jar包问题以及使用mvn install:install-file

《解决Maven项目idea找不到本地仓库jar包问题以及使用mvninstall:install-file》:本文主要介绍解决Maven项目idea找不到本地仓库jar包问题以及使用mvnin... 目录Maven项目idea找不到本地仓库jar包以及使用mvn install:install-file基

Nginx location匹配模式与规则详解

《Nginxlocation匹配模式与规则详解》:本文主要介绍Nginxlocation匹配模式与规则,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、环境二、匹配模式1. 精准模式2. 前缀模式(不继续匹配正则)3. 前缀模式(继续匹配正则)4. 正则模式(大

Maven如何手动安装依赖到本地仓库

《Maven如何手动安装依赖到本地仓库》:本文主要介绍Maven如何手动安装依赖到本地仓库问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、下载依赖二、安装 JAR 文件到本地仓库三、验证安装四、在项目中使用该依赖1、注意事项2、额外提示总结一、下载依赖登

Maven的使用和配置国内源的保姆级教程

《Maven的使用和配置国内源的保姆级教程》Maven是⼀个项目管理工具,基于POM(ProjectObjectModel,项目对象模型)的概念,Maven可以通过一小段描述信息来管理项目的构建,报告... 目录1. 什么是Maven?2.创建⼀个Maven项目3.Maven 核心功能4.使用Maven H

idea maven编译报错Java heap space的解决方法

《ideamaven编译报错Javaheapspace的解决方法》这篇文章主要为大家详细介绍了ideamaven编译报错Javaheapspace的相关解决方法,文中的示例代码讲解详细,感兴趣的... 目录1.增加 Maven 编译的堆内存2. 增加 IntelliJ IDEA 的堆内存3. 优化 Mave

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

详解nginx 中location和 proxy_pass的匹配规则

《详解nginx中location和proxy_pass的匹配规则》location是Nginx中用来匹配客户端请求URI的指令,决定如何处理特定路径的请求,它定义了请求的路由规则,后续的配置(如... 目录location 的作用语法示例:location /www.chinasem.cntestproxy

一文教你如何将maven项目转成web项目

《一文教你如何将maven项目转成web项目》在软件开发过程中,有时我们需要将一个普通的Maven项目转换为Web项目,以便能够部署到Web容器中运行,本文将详细介绍如何通过简单的步骤完成这一转换过程... 目录准备工作步骤一:修改​​pom.XML​​1.1 添加​​packaging​​标签1.2 添加