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

相关文章

Maven 配置中的 <mirror>绕过 HTTP 阻断机制的方法

《Maven配置中的<mirror>绕过HTTP阻断机制的方法》:本文主要介绍Maven配置中的<mirror>绕过HTTP阻断机制的方法,本文给大家分享问题原因及解决方案,感兴趣的朋友一... 目录一、问题场景:升级 Maven 后构建失败二、解决方案:通过 <mirror> 配置覆盖默认行为1. 配置示

MYSQL查询结果实现发送给客户端

《MYSQL查询结果实现发送给客户端》:本文主要介绍MYSQL查询结果实现发送给客户端方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录mysql取数据和发数据的流程(边读边发)Sending to clientSending DataLRU(Least Rec

C++作用域和标识符查找规则详解

《C++作用域和标识符查找规则详解》在C++中,作用域(Scope)和标识符查找(IdentifierLookup)是理解代码行为的重要概念,本文将详细介绍这些规则,并通过实例来说明它们的工作原理,需... 目录作用域标识符查找规则1. 普通查找(Ordinary Lookup)2. 限定查找(Qualif

maven私服配置全过程

《maven私服配置全过程》:本文主要介绍maven私服配置全过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录使用Nexus作为 公司maven私服maven 私服setttings配置maven项目 pom配置测试效果总结使用Nexus作为 公司maven私

Java调用C#动态库的三种方法详解

《Java调用C#动态库的三种方法详解》在这个多语言编程的时代,Java和C#就像两位才华横溢的舞者,各自在不同的舞台上展现着独特的魅力,然而,当它们携手合作时,又会碰撞出怎样绚丽的火花呢?今天,我们... 目录方法1:C++/CLI搭建桥梁——Java ↔ C# 的“翻译官”步骤1:创建C#类库(.NET

Python FastMCP构建MCP服务端与客户端的详细步骤

《PythonFastMCP构建MCP服务端与客户端的详细步骤》MCP(Multi-ClientProtocol)是一种用于构建可扩展服务的通信协议框架,本文将使用FastMCP搭建一个支持St... 目录简介环境准备服务端实现(server.py)客户端实现(client.py)运行效果扩展方向常见问题结

Nginx Location映射规则总结归纳与最佳实践

《NginxLocation映射规则总结归纳与最佳实践》Nginx的location指令是配置请求路由的核心机制,其匹配规则直接影响请求的处理流程,下面给大家介绍NginxLocation映射规则... 目录一、Location匹配规则与优先级1. 匹配模式2. 优先级顺序3. 匹配示例二、Proxy_pa

IDEA中Maven Dependencies出现红色波浪线的原因及解决方法

《IDEA中MavenDependencies出现红色波浪线的原因及解决方法》在使用IntelliJIDEA开发Java项目时,尤其是基于Maven的项目,您可能会遇到MavenDependenci... 目录一、问题概述二、解决步骤2.1 检查 Maven 配置2.2 更新 Maven 项目2.3 清理本

maven中的maven-antrun-plugin插件示例详解

《maven中的maven-antrun-plugin插件示例详解》maven-antrun-plugin是Maven生态中一个强大的工具,尤其适合需要复用Ant脚本或实现复杂构建逻辑的场景... 目录1. 核心功能2. 典型使用场景3. 配置示例4. 关键配置项5. 优缺点分析6. 最佳实践7. 常见问题

windows系统上如何进行maven安装和配置方式

《windows系统上如何进行maven安装和配置方式》:本文主要介绍windows系统上如何进行maven安装和配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不... 目录1. Maven 简介2. maven的下载与安装2.1 下载 Maven2.2 Maven安装2.