WebService是一个SOA(面向服务的编程)的架构,它是不依赖于语言,不依赖于平台,可以实现不同的语言间的相互调用,通过Internet进行基于Http协议的网络应用间的交互。 WebService实现不同语言间的调用,是依托于一个标准,webservice是需要遵守WSDL(web服务定义语言)/SOAP(简单请求协议)规范的。 WebService=WSDL+SOAP+UDDI(webservice的注册)Soap是由Soap的part和0个或多个附件组成,一般只有part,在part中有Envelope和Body。 Web Service是通过提供标准的协议和接口,可以让不同的程序集成的一种SOA架构。
可以让异构的程序相互访问(跨平台)
松耦合
基于标准协议(通用语言,允许其他程序访问)
Service Provider采用WSDL描述服务
Service Provider 采用UDDI将服务的描述文件发布到UDDI服务器(Register server)
Service Requestor在UDDI服务器上查询并 获取WSDL文件
Service requestor将请求绑定到SOAP,并访问相应的服务。
SOAP请求(Simple Object Access Protocol,简单对象访问协议)是HTTP POST的一个专用版本,遵循一种特殊的XML消息格式,Content-type设置为:text/xml ,任何数据都可以XML化。 SOAP:简单对象访问协议。SOAP是一种轻量的,简单的,基于XML的协议,它被设计成在web上交换结构化的和固化的信息。SOAP可以和现存的许多因特网协议和格式结合使用,包括超文本传输协议(HTTP),简单邮件传输协议(SMTP),多用途网际邮件扩充协议(MIME)。它还支持从消息系统到远程过程调用(RPC)等大量的应用程序。
实现webservice服务,并通过client调用服务端
<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"><parent><artifactId>webservice</artifactId><groupId>com.et</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>webservice-server</artifactId><properties><maven.compiler.source>11</maven.compiler.source><maven.compiler.target>11</maven.compiler.target></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- 引入Spring Boot Web Services Starter --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web-services</artifactId></dependency><!-- 引入Apache CXF Spring Boot Starter for JAX-WS --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-spring-boot-starter-jaxws</artifactId><version>3.3.4</version></dependency></dependencies></project>
package com.et.webservice.server.config;import com.et.webservice.server.service.MyWebService;import org.apache.cxf.bus.spring.SpringBus;import org.apache.cxf.jaxws.EndpointImpl;import org.apache.cxf.transport.servlet.CXFServlet;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.web.servlet.ServletRegistrationBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;/*** CXF配置类,负责初始化CXF相关组件、发布Webservice服务以及配置CXF Servlet。*/public class CxfConfig {/*** 自动注入Spring Bus实例,它是CXF的核心组件之一,用于管理和配置CXF运行时环境。*/private SpringBus bus;/*** 自动注入实现了MyWebService接口的服务实现类实例,该实例将被发布为Webservice供外部调用。*/private MyWebService myWebServiceImpl;/*** 创建并返回Webservice端点(Endpoint)实例,用于发布MyWebService服务。* 将服务实现类与Spring Bus关联,并指定发布地址为"/1"。** @return Webservice端点实例*/public EndpointImpl endpoint() {EndpointImpl endpoint = new EndpointImpl(bus, myWebServiceImpl);endpoint.publish("/1"); // 发布地址return endpoint;}/*** 创建并返回CXF Servlet的ServletRegistrationBean实例,用于注册CXF Servlet到Spring Boot的Servlet容器中。* 设置CXF Servlet的映射路径为"/services/*",表示所有以"/services/"开头的HTTP请求都将由CXF Servlet处理。** @return CXF Servlet的ServletRegistrationBean实例*/public ServletRegistrationBean wsServlet() {return new ServletRegistrationBean(new CXFServlet(), "/services/*");}}
package com.et.webservice.server.service;import javax.jws.WebMethod;import javax.jws.WebService;@WebService(name = "MyWebService",targetNamespace = "http://liuhaihua.cn/mywebservice")public interface MyWebService {@WebMethodString sayHello(String name);}
package com.et.webservice.server.service;import org.springframework.stereotype.Service;import javax.jws.WebService;public class MyWebServiceImpl implements MyWebService {public String sayHello(String name) {System.err.println("sayHello is called..."); // 只是为了更明显的输出,采用errreturn "Hello, " + name + "!";}}
package com.et.webservice.server;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}}
<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"><parent><artifactId>webservice</artifactId><groupId>com.et</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>webservice-client</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- 测试依赖配置 --><dependency><groupId>org.junit</groupId><artifactId>junit-bom</artifactId><version>5.9.1</version><type>pom</type><scope>test</scope></dependency><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter</artifactId><version>5.9.1</version><scope>test</scope></dependency><!-- 主要依赖配置 --><dependency><groupId>com.sun.xml.bind</groupId><artifactId>jaxb-impl</artifactId><version>4.0.5</version></dependency><dependency><groupId>javax.xml.bind</groupId><artifactId>jaxb-api</artifactId><version>2.3.1</version></dependency><dependency><groupId>jakarta.activation</groupId><artifactId>jakarta.activation-api</artifactId><version>2.1.3</version></dependency><dependency><groupId>jakarta.jws</groupId><artifactId>jakarta.jws-api</artifactId><version>3.0.0</version></dependency><dependency><groupId>jakarta.xml.ws</groupId><artifactId>jakarta.xml.ws-api</artifactId><version>4.0.1</version></dependency><dependency><groupId>jakarta.xml.bind</groupId><artifactId>jakarta.xml.bind-api</artifactId><version>4.0.1</version></dependency><!-- Apache CXF相关依赖 --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http-jetty</artifactId><version>3.3.4</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>3.3.4</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-reload4j</artifactId><version>2.1.0-alpha1</version></dependency></dependencies></project>
package com.et.webservice.client;import javax.jws.WebService;(name = "MyWebService",targetNamespace = "http://liuhaihua.cn/mywebservice")public interface HelloService {// 接口名一样String sayHello(String name); // 方法定义名一样}
package com.et.webservice.client;import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;/*** 客户端调用类,用于通过JAX-WS代理方式访问HelloService Web服务。*/public class Client {/*** 程序主入口方法。** @param args 命令行参数*/public static void main(String[] args) {// 创建JAX-WS代理工厂对象JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();// 设置要访问的服务地址jaxWsProxyFactoryBean.setAddress("http://localhost:8088/services/1?wsdl");// 设置服务接口类,即HelloServicejaxWsProxyFactoryBean.setServiceClass(HelloService.class);// 使用工厂对象创建HelloService接口的代理实例HelloService helloService = jaxWsProxyFactoryBean.create(HelloService.class);System.out.println(helloService.getClass());// 调用代理实例的方法,向服务端发送请求,并打印返回结果System.out.println(helloService.sayHello("hello world"));}}
以上只是一些关键代码,所有代码请参见下面代码仓库
https://github.com/Harries/springboot-demo
启动服务端,访问http://localhost:8088/services/1?wsdl

调用客户端,返回结果
11:20:20.148 [main] DEBUG org.apache.cxf.phase.PhaseInterceptorChain - Invoking handleMessage on interceptor org.apache.cxf.ws.policy.PolicyVerificationInInterceptor@1e8823d211:20:20.148 [main] DEBUG org.apache.cxf.ws.policy.PolicyVerificationInInterceptor - Verified policies for inbound message.Hello, hello world!
https://en.wikipedia.org/wiki/Web_service
http://www.liuhaihua.cn/archives/710832.html
原文作者:HBLOG;如有侵权,请联系管理员删除。