Web Service 入门 一看会!

Web Service 入门

一、什么是web service

1.1 概念

  1. 它是一种跨编程语言和跨操作系统平台的远程调用技术即跨平台远程调用技术。将调用方称为客户端,将被调方称为服务端,客户端通过网络通信协议访问服务端提供的接口。

  2. xml是web service的跨语言、跨平台的基础,XML主要的优点在于它既与平台无关,又与厂商无关。

  3. web service采用标准SOAP(Simple Object Access Protocol) 协议传输,soap属于w3c标准。Soap协议是基于http的应用层协议,soap协议传输是xml数据。

  4. XSD,W3C为webservice制定了一套传输数据类型,使用xml进行描述,即XSD(XML Schema Datatypes),任何编程语言写的web service接口在发送数据时都要转换成web service标准的XSD发送。

  5. WSDL 是基于 XML 的用于描述Web Service及其函数、参数和返回值。通俗理解Wsdl是web service的使用说明书。

1.2应用场景

因为web service使用xml作为传输数据格式,xml是可跨平台跨语言的。因此对于要支持不同语言的系统交互时可以使用web service作为接口使用。web service表面上是一个应用程序,外界不用关心里面的具体实现,只需要获得它的API描述文档,传递对应的参数就可以返回需要的数据。在使用上解决了客户与服务的独立、

二、我的第一个WebService应用

2.1 准备工作

我这里一开始常见项目的时候用的是SpringBoot模块,其实是用普通的maven项目即可,但是要自己配置tomcat。在SpringBoot项目的基础下,maven依赖如下:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

2.1 创建服务

服务接口

@WebService
public interface WeatherService {
   

    public String query(String city);

}

接口实现

@WebService
public class WeatherServiceImpl implements WeatherService {
   

    @Override
    public String query(String city) {
   
        return city + "大晴天";
    }
}

2.2 发布接口

@SpringBootApplication
public class WebserviceApplication {
   

    public static void main(String[] args) {
   
        SpringApplication.run(WebserviceApplication.class, args);
        Endpoint.publish("http://localhost:8081/weatherservice",new WeatherServiceImpl());
    }

}

我这里直接在SpringBoot中的主函数中发布接口,端口会与SpringBoot中的默认端口冲突,所以发布的地址中使用的接口是8081。

2.3 查看发布的xml接口文档

整个springboot项目启动之后,在浏览器中输入http://localhost:8081/weatherservice/wsdl会得到下面的xml信息

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<!--  Published by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e.  -->
<!--  Generated by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e.  -->
<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://WebServiceImpl.achao.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://WebServiceImpl.achao.com/" name="WeatherServiceImplService">
<types>
<xsd:schema>
<xsd:import namespace="http://WebServiceImpl.achao.com/" schemaLocation="http://localhost:8081/weatherservice?xsd=1"/>
</xsd:schema>
</types>
<message name="query">
<part name="parameters" element="tns:query"/>
</message>
<message name="queryResponse">
<part name="parameters" element="tns:queryResponse"/>
</message>
<portType name="WeatherServiceImpl">
<operation 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值