WebService总结

服务器端的实现

WebService是一种跨编程语言和跨操作系统平台的远程调用技术。

作用:

1.可重复使用的应用程序组件

Web services 可以把应用程序组件作为服务来提供,比如汇率转换、天气预报或者甚至是语言翻译等等。

2.连接现有的软件

通过使用 Web services,您可以在不同的应用程序与平台之间来交换数据。

 

Web Services 拥有三种基本的元素。

它们是:SOAP、WSDL 以及 UDDI

SOAP:基本的 Web services 平台是 XML + HTTP

 WebService通过HTTP协议发送请求和接收结果时,发送的请求内容和结果内容都采用XML格式封装,并增加了一些特定的HTTP消息头,以说明 HTTP消息的内容格式,这些特定的HTTP消息头和XML内容格式就是SOAP协议。SAP提供了标准的RPC方法来调用Web Service。

  SOAP协议 = HTTP协议 + XML数据格式

 

WSDL(Web Services Description Language), web服务描述语言,他是webservice服务端使用说明书,说明服务端接口、方法、参数和返回值,WSDL是随服务发布成功,自动生成,无需编写。

Service:相关端口的集合,包括其关联的接口、操作、消息等。

Binding:特定端口类型的具体协议和数据格式规范

portType: 服务端点,描述 web service可被执行的操作方法,以及相关的消息,通过binding指向portType

message: 定义一个操作(方法)的数据参数

types: 定义 web service 使用的全部数据类型

UDDI 是一种目录服务,企业可以使用它对 Web services 进行注册和搜索。 

如果我们要使用一种服务,但是不知道地址(wsdl等),我们就可以在UDDI中查找。 

大部分情况下,我们都是知道服务地址的。

 

来自 <https://blog.csdn.net/c99463904/article/details/76018436>

 

客户端的实现

客户端调用服务有很多种方法,我们先用工具生成客户端代码,后面会详解

wsimport是jdk自带的webservice客户端工具,可以根据wsdl文档生成客户端调用代码(java代码)。

>

//编写SEI(Service Endpoint Interface),SEI在webservice中称为portType,在java中就是普通接口

public interface IWeatherTest01 {

public String queryWeather(String cityName);

}        

 

import javax.jws.WebService;

 

@WebService

public class WeatherImplTest01 implements IWeatherTest01 {

 

@Override

public String queryWeather(String cityName) {

System.out.println("获取城市名"+cityName);

        String weather="暴雨";

return weather;

}

}

 

import javax.xml.ws.Endpoint;

 

public class WebServiceTest02 {

public static void main(String[] args) {

Endpoint.publish("http://192.168.2.100:8089/weather", new WeatherImplTest01());

}

}

WSDL文件(自动生成)

<?xml version="1.0" encoding="UTF-8"?>

<!-- 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 name="WeatherImplTest01Service" targetNamespace="http://test02/" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://test02/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"><types><xsd:schema><xsd:import schemaLocation="http://192.168.2.100:8089/weather?xsd=1" namespace="http://test02/"/></xsd:schema></types><message name="queryWeather"><part name="parameters" element="tns:queryWeather"/></message><message name="queryWeatherResponse"><part name="parameters" element="tns:queryWeatherResponse"/> </message><portType name="WeatherImplTest01"><operation name="queryWeather"><input message="tns:queryWeather" wsam:Action="http://test02/WeatherImplTest01/queryWeatherRequest"/><output message="tns:queryWeatherResponse" wsam:Action="http://test02/WeatherImplTest01/queryWeatherResponse"/></operation></portType><binding name="WeatherImplTest01PortBinding" type="tns:WeatherImplTest01"><soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/><operation name="queryWeather"><soap:operation soapAction=""/><input><soap:body use="literal"/></input><output><soap:body use="literal"/></output></operation></binding><service name="WeatherImplTest01Service"><port name="WeatherImplTest01Port" binding="tns:WeatherImplTest01PortBinding"><soap:address location="http://192.168.2.100:8089/weather"/></port></service></definitions>

 

 

屏幕剪辑的捕获时间: 2018/12/11 11:23

客户端的实现

1.创建一个客户端空项目,cmd命令行进入此项目的src目录

  使用以下命令生成客户端代码  

wsimport -s . http://192.168.2.100/weather?wsdl -s是指编译出源代码文件,后面的.(点)指將代码放到当前目录下. 最后面的http….是指获取wsdl说明书的地址

public class WeatherClient {
    public static void main(String[] args) {
        //创建服务视图,视图是从wsdl文件的service标签的name属性获取
        WeatherImplTest01Service weatherServiceImpl = new WeatherImplTest01Service(); 
        //获取服务实现类,实现类从wsdl文件的portType的name属性获取
        WeatherImplTest01 weatherInterfaceImpl = weatherServiceImpl.getPort(WeatherImplTest01.class);
        //获取查询方法,从portType的operation标签获取
        String weather = weatherInterfaceImpl.queryWeather("北京");
        System.out.println(weather);
    }
}


 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值