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

相关文章

JAVA项目swing转javafx语法规则以及示例代码

《JAVA项目swing转javafx语法规则以及示例代码》:本文主要介绍JAVA项目swing转javafx语法规则以及示例代码的相关资料,文中详细讲解了主类继承、窗口创建、布局管理、控件替换、... 目录最常用的“一行换一行”速查表(直接全局替换)实际转换示例(JFramejs → JavaFX)迁移建

maven异常Invalid bound statement(not found)的问题解决

《maven异常Invalidboundstatement(notfound)的问题解决》本文详细介绍了Maven项目中常见的Invalidboundstatement异常及其解决方案,文中通过... 目录Maven异常:Invalid bound statement (not found) 详解问题描述可

在C#中调用Windows防火墙界面的常见方式

《在C#中调用Windows防火墙界面的常见方式》在C#中调用Windows防火墙界面(基础设置或高级安全设置),可以使用进程启动(Process.Start)或Win32API来实现,所以本文给大家... 目录引言1. 直接启动防火墙界面(1) 打开基本防火墙设置(firewall.cpl)(2) 打开高

python调用dubbo接口的实现步骤

《python调用dubbo接口的实现步骤》本文主要介绍了python调用dubbo接口的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编... 目录 ​​其他实现方式与注意事项​​ ​​高级技巧与集成​​用 python 提供 Dubbo 接口

C# FTP调用的实现示例

《C#FTP调用的实现示例》本文介绍了.NET平台实现FTP/SFTP操作的多种方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录1. 使用 .NET 自带 FtpWebRequest 实现 FTP 操作1.1 文件上传1.2

Java Lettuce 客户端入门到生产的实现步骤

《JavaLettuce客户端入门到生产的实现步骤》本文主要介绍了JavaLettuce客户端入门到生产的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目录1 安装依赖MavenGradle2 最小化连接示例3 核心特性速览4 生产环境配置建议5 常见问题

Python之变量命名规则详解

《Python之变量命名规则详解》Python变量命名需遵守语法规范(字母开头、不使用关键字),遵循三要(自解释、明确功能)和三不要(避免缩写、语法错误、滥用下划线)原则,确保代码易读易维护... 目录1. 硬性规则2. “三要” 原则2.1. 要体现变量的 “实际作用”,拒绝 “无意义命名”2.2. 要让

MyBatis/MyBatis-Plus同事务循环调用存储过程获取主键重复问题分析及解决

《MyBatis/MyBatis-Plus同事务循环调用存储过程获取主键重复问题分析及解决》MyBatis默认开启一级缓存,同一事务中循环调用查询方法时会重复使用缓存数据,导致获取的序列主键值均为1,... 目录问题原因解决办法如果是存储过程总结问题myBATis有如下代码获取序列作为主键IdMappe

SpringBoot集成WebService(wsdl)实践

《SpringBoot集成WebService(wsdl)实践》文章介绍了SpringBoot项目中通过缓存IWebService接口实现类的泛型入参类型,减少反射调用提升性能的实现方案,包含依赖配置... 目录pom.XML创建入口ApplicationContextUtils.JavaJacksonUt

深入浅出Java中的Happens-Before核心规则

《深入浅出Java中的Happens-Before核心规则》本文解析Java内存模型中的Happens-Before原则,解释其定义、核心规则及实际应用,帮助理解多线程可见性与有序性问题,掌握并发编程... 目录前言一、Happens-Before是什么?为什么需要它?1.1 从一个问题说起1.2 Haht