Web Service技术在我第一次接触,又没有实际使用时彻底不理解这是什么。觉得是一种相似Spring,Shiro的编程框架。后来渐渐理解,WS(即Web Service缩写)是一种通用的接口规范,并按照该规范编写接口对外提供服务。html
这个问题在我没有编写WS代码时但是困扰了我好久,甚至第一次须要写WS接口都不知道老大到底要我写什么。由于我习惯于去网上寻找资料自学并实践某些知识点,而我在百度时查到的WS介绍基本都是这样的。java
百度百科:web
Web service是一个平台独立的,低耦合的,自包含的、基于可编程的web的应用程序,可以使用开放的XML(标准通用标记语言下的一个子集)标准来描述、发布、发现、协调和配置这些应用程序,用于开发分布式的互操做的应用程序。spring
百度搜索WS排名前列的博客:apache
Web Service技术, 能使得运行在不一样机器上的不一样应用无须借助附加的、专门的第三方软件或硬件, 就可相互交换数据或集成。依据Web Service规范实施的应用之间, 不管它们所使用的语言、 平台或内部协议是什么, 均可以相互交换数据。编程
这种解释对于不了解WS的人来看彻底是一头雾水,后来在实践中渐渐明白了什么是WS。自我总结以下:app
Web Service就是一种跨编程语言和跨操做系统平台的远程调用技术。所谓跨编程语言和跨操做平台,就是说服务端程序采用Java编写,客户端程序则能够采用其余编程语言编写,反之亦然。跨操做系统平台则是指服务端程序和客户端程序能够在不一样的操做系统上运行。 远程调用,就是一台计算机的应用能够调用其余计算机上的应用,也能够直接理解为接口。例如:支付宝,支付宝并无银行卡等数据,它只是去调用银行提供的接口来得到数据。还有天气预报等,也是气象局把本身的系统服务以webservice服务的形式暴露出来,让第三方网站和程序能够调用这些服务功能。框架
因此Web Servic就是一种跨语言跨平台的接口技术。frontend
Web Service只是一套接口规范,实际运用中实现Web Service的方法有不少种,Java自己在jdk1.7以后也对webservice有了默认的实现(Oracle JAX-WS RI),可是在咱们实际开发中通常仍是会使用框架来,好比这里所提到的CXF就有着普遍的应用。
CXF单独发布就不说了,直接讲Spring整合CXF,毕竟如今的JavaEE开发是离不开Spring了。maven
加入maven依赖
<!--cxf--> <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-frontend-jaxws --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.1.6</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-transports-http --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.1.6</version> </dependency>
<!--定义一个cxf的servlet--> <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/webservice/*</url-pattern> </servlet-mapping>
很是简洁的接口和实现
接口:HelloWorld.java
package com.erictao.cxf; import javax.jws.WebService; @WebService public interface HelloWorld { public String say(String str); }
实现:HelloWorldImpl.java
package com.erictao.cxf.impl; import com.erictao.cxf.HelloWorld; import org.springframework.stereotype.Component; import javax.jws.WebService; @Component("helloWorld") @WebService public class HelloWorldImpl implements HelloWorld { public String say(String str) { return "Hello"+str; } }
在spring主配置文件spring.xml中添加引入spring-cxf.xml
<!-- lang: xml --> <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <context:component-scan base-package="com.erictao"/> <!-- 省略其余配置内容 --> <import resource="spring-cxf.xml"/> </beans>
而且建立配置文件spring-cxf.xml
<!-- lang: xml --> <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <!-- 定义webservice的发布接口 --> <jaxws:endpoint implementor="#helloWorld" address="/HelloWorld"> </beans>
更加具体的配置能够查看官方给出的文档:http://cxf.apache.org/docs/how-do-i-develop-a-service.html。
#helloWorld
指的是咱们在HelloWorldImpl类中所自定义的名字,即@Component("helloWorld")
定义的bean id,/HelloWorld
则是咱们定义的接口地址。
以后咱们运行项目输入该地址:http://127.0.0.1:8080/ssm/webservice/HelloWorld?wsdl若是出现以下界面:
则说明咱们的webservice发布成功了。接下来只须要经过客户端调用这个接口便可得到返回结果了。客户端的代码能够直接使用工具生成,可参考:https://blog.csdn.net/andyliulin/article/details/53680915。
总结 以上就是一个简单的Web Service入门实例,更多的关于CXF拦截器,客户端调用就没有作过多介绍,后续有时间的话再接着更新,明白了Web Service服务端和客户端怎么构建,后续只须要当作普通接口编写业务代码便可。