一丶传统的WebService服务
- xml技术,服务方和客户端之间的远程方法之间的调用,是使用xml数据格式交互的。(优点:xml文档与远程平台无关)
- SAOP协议:Simple Object Access Protocal 简单对象访问协议 soap=http+xml
- WSDL: web服务的描述语言,本质就是一个xml文档,webservice的描述文件(说明书)
二丶Restful风格的服务(Springboot)
- json数据交互 比较流利的restful服务是以springMVC框架进行发布
- Http协议
- 无需wsdl文档
三丶开发传统的webservice
Apache CXF = Celtix + Xfire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF。Apache CXF 是一个开源的 web Services 框架,CXF 帮助您构建和开发 web Services ,它支持多种协议,比如:SOAP1.1,1,2XML/HTTP、RESTful 或者CORBA。
Cxf是基于SOA总线结构,依靠spring完成模块的集成,实现SOA方式。
webservice服务方实现步骤:
- 导入cxf类库集,开发一个JavaWeb项目,把cxf的类库导入到工程。
- 在web.xml中,配置cxf的中央控制器
-
-
<!-- CXF 中央控制器 -->
-
<servlet>
-
<servlet-name>cxfServlet</servlet-name>
-
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
-
<load-on-startup>1</load-on-startup>
-
</servlet>
-
<servlet-mapping>
-
<servlet-name>cxfServlet</servlet-name>
-
<url-pattern>/services/*</url-pattern>
-
</servlet-mapping>
-
<context-param>
-
<param-name>contextConfigLocation</param-name>
-
<param-value>classpath:applicationContext.xml</param-value>
-
</context-param>
-
<listener>
-
<listener-class>
-
org.springframework.web.context.ContextLoaderListener
-
</listener-class>
-
</listener>
-
-
实现业务
业务接口 ProductService
-
@WebService
-
public interface ProductService {
-
public List<Product> findAllProducts();
-
}
业务实现类
-
public class ProductServiceImpl implements ProductService {
-
@Override
-
public List<Product> findAllProducts() {
-
List<Product> list = new ArrayList<>();
-
for (int i = 1; i <= 10; i++) {
-
list.add(new Product(i,"华为Mate30 "+i,4000D,"中国深圳"));
-
}
-
return list;
-
}
-
}
5.以spring的方式发布,添加applicationContext.xml
-
<!-- 发布应用程序接口 -->
-
<jaxws:endpoint id="productServiceImpl"
-
implementor="com.gec.ws.service.ProductServiceImpl"
-
address="/productService"></jaxws:endpoint>
6. 访问webservice的WSDL文件
地址参考:http://localhost:8080/services/productService?wsdl
四丶什么是Restful风格的服务
概念:网络上的所有的资源(业务数据)都有一个唯一标识(URI)来定位,只要获取该唯一标识就可以对相应的资源进行操作(增删改查)。在Spring技术领域中,使用SpringMVC通过Controller对外发布业务数据,而且可以使用Springboot技术简化开发的流程。简单来讲,每一个Springboot的应用都可以视为一个Restful的服务。
Restful的常用动作:
- GET 查询
- POST 添加
- DELETE 删除
- PUT 修改