WebService介绍

257 阅读2分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第16天,点击查看活动详情

1、前言

WebService也叫XML WebService,是一种跨编程语言和跨操作系统平台的远程调用技术,可以理解位一种接口调用技术,与http接口调用效果类似,其响应结果是以xml形式返回,适合于一些复杂数据的返回,跨防火墙通信等。同时WebService支持跨域。

这是一个webService接口返回值示例:

image.png

2、SOAP

SOAP - Simple Object Access Protocol,简单对象访问协议。

WebService是基于SOAP协议来传输数据的,它可以让我们通过RPC形式调用WebService接口。从根本上来说SOAP协议就是HTTP+XML,XML是SOAP的数据编码方式。

image.png

上图就是一个SOAP返回值示例,我们可以看到SOAP协议返回的数据格式,包括三大部分:

  • Envelope,作为XML的根元素;

  • Header,头部可有可无

  • Body,包含了要调用的方法以及参数

3、WSDL

WSDL - WebService Definition Language,WebService定义语言。

我们可以将WSDL理解为WebService的说明书,通过WSDL接口的返回值,我们可以清晰地看到WebService服务提供方,提供了哪些接口,接口的调用参数,以及返回值的数据格式。

标准的WSDL接口示例:

中国邮政编码 <-> 地址信息双向查询/搜索 WEB 服务 www.webxml.com.cn/WebServices…

返回值示例:

image.png

4、适用场景

其实WebService与http从效果上来讲,没什么区别,都是远程调用,获取接口数据。

如果项目中的某一个功能,需要对外提供,在公司内部系统之间并且是不同的客户端来调用,那么就可以用WebService来开发。(PS:我遇到过一个项目,银行的前置系统,与其他子系统间的调用,就是采用了WebService来实现。)

5、WebService框架

这里说下Java中的WebService开发框架。

主要有axis,xfire,cxf。一位内广泛使用的还是ApacheCXF。cxf需要引入的包也很多。这里我贴下我们项目中的:

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-core</artifactId>
    <version>3.0.3</version>
   </dependency>
   <dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-bindings-soap</artifactId>
    <version>3.0.3</version>
   </dependency>
   <dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-databinding-jaxb</artifactId>
    <version>3.0.3</version>
    <exclusions>
     <exclusion>
      <groupId>com.sun.xml.bind</groupId>
      <artifactId>jaxb-core</artifactId>
     </exclusion>
    </exclusions>
   </dependency>
   <dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxws</artifactId>
    <version>3.0.3</version>
    <exclusions>
     <exclusion>
      <groupId>*</groupId>
      <artifactId>*</artifactId>
     </exclusion>
    </exclusions>
   </dependency>
   <dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-simple</artifactId>
    <version>3.0.3</version>
   </dependency>
   <dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http</artifactId>
    <version>3.0.3</version>
   </dependency>
   <dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-wsdl</artifactId>
    <version>3.0.3</version>
   </dependency>

好了、本期就先介绍到这里,有什么需要交流的,大家可以随时私信我。😊