Web Service 入门
一、什么是web service
1.1 概念
-
它是一种跨编程语言和跨操作系统平台的远程调用技术即跨平台远程调用技术。将调用方称为客户端,将被调方称为服务端,客户端通过网络通信协议访问服务端提供的接口。
-
xml是web service的跨语言、跨平台的基础,XML主要的优点在于它既与平台无关,又与厂商无关。
-
web service采用标准SOAP(Simple Object Access Protocol) 协议传输,soap属于w3c标准。Soap协议是基于http的应用层协议,soap协议传输是xml数据。
-
XSD,W3C为webservice制定了一套传输数据类型,使用xml进行描述,即XSD(XML Schema Datatypes),任何编程语言写的web service接口在发送数据时都要转换成web service标准的XSD发送。
-
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