前言
web Service 技术在我第一次接触, 又没有实际使用时完全不理解这是什么. 以为是一种类似 Spring,Shiro 的编程框架. 后来渐渐理解, WS(即 Web Service 缩写) 是一种通用的接口规范, 并按照该规范编写接口对外提供服务.
一 啥是 WS
这个问题在我没有编写 WS 代码时可是困扰了我很久, 甚至第一次需要写 WS 接口都不知道老大到底要我写什么. 因为我习惯于去网上寻找资料自学并实践某些知识点, 而我在百度时查到的 WS 介绍基本都是这样的.
百度百科:
Web service 是一个平台独立的, 低耦合的, 自包含的, 基于可编程的 Web 的应用程序, 可使用开放的 xml(标准通用标记语言下的一个子集) 标准来描述, 发布, 发现, 协调和配置这些应用程序, 用于开发分布式的互操作的应用程序.
百度搜索 WS 排名前列的博客:
Web Service 技术, 能使得运行在不同机器上的不同应用无须借助附加的, 专门的第三方软件或硬件, 就可相互交换数据或集成. 依据 Web Service 规范实施的应用之间, 无论它们所使用的语言, 平台或内部协议是什么, 都可以相互交换数据.
这种解释对于不了解 WS 的人来看完全是一头雾水, 后来在实践中渐渐明白了什么是 WS. 自我总结如下:
Web Service 就是一种跨编程语言和跨操作系统平台的远程调用技术. 所谓跨编程语言和跨操作平台, 就是说服务端程序采用 Java 编写, 客户端程序则可以采用其他编程语言编写, 反之亦然. 跨操作系统平台则是指服务端程序和客户端程序可以在不同的操作系统上运行. 远程调用, 就是一台计算机的应用可以调用其他计算机上的应用, 也可以直接理解为接口. 例如: 支付宝, 支付宝并没有银行卡等数据, 它只是去调用银行提供的接口来获得数据. 还有天气预报等, 也是气象局把自己的系统服务以 webservice 服务的形式暴露出来, 让第三方网站和程序可以调用这些服务功能.
所以 Web Servic 就是一种跨语言跨平台的接口技术.
二 Spring 整合 CXF
Web Service 只是一套接口规范, 实际运用中实现 Web Service 的方法有很多种, Java 本身在 jdk1.7 之后也对 webservice 有了默认的实现 (Oracle JAX-WS RI), 但是在我们实际开发中一般还是会使用框架来, 比如这里所提到的 CXF 就有着广泛的应用.
CXF 单独发布就不说了, 直接讲 Spring 整合 CXF, 毕竟现在的 JavaEE 开发是离不开 Spring 了.
2.1 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>
2.2 配置 Web.xml
- <!-- 定义一个 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>
2.3 写一个 WS 接口及其实现
非常简洁的接口和实现
接口: 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;
- }
- }
2.4 修改 Spring 配置文件
在 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 则是我们定义的接口地址.
三 运行发布 WS
之后我们运行项目输入该地址: http://127.0.0.1:8080/ssm/webservice/HelloWorld?wsdl 如果出现如下界面:
则说明我们的 webservice 发布成功了. 接下来只需要通过客户端调用这个接口即可获得返回结果了. 客户端的代码可以直接使用工具生成, 可参考: https://blog.csdn.net/andyliulin/article/details/53680915.
总结
以上就是一个简单的 Web Service 入门实例, 更多的关于 CXF 拦截器, 客户端调用就没有做过多介绍, 后续有时间的话再接着更新, 明白了 Web Service 服务端和客户端怎么构建, 后续只需要当做普通接口编写业务代码即可.
来源: https://www.cnblogs.com/taojinxuan/p/11136340.html