A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 小江哥 于 2018-11-15 17:16 编辑

一、WebService介绍

1、什么是WebService

webService是一种使用http传输soap协议数据的远程调用技术

2.webService三要素

SOAP:规范XML标签

WSDL:服务端的使用说明书

UDDI:目录

二、webService入门小程序

1.服务端

(1)、开发步骤

A、创建接口

[Java] 纯文本查看 复制代码
package com.webservice.jaxws;
 
public interface WeatherService {
 
        //查询天气的方法
        public String queryWeather(String cityName);
}


B、创建实现类,在实现类上加入@WebService注解,该注解的作用是标识该实现类是webservice的服务类,发布该实现类中的public方法

[Java] 纯文本查看 复制代码
package com.webservice.jaxws;
 
import javax.jws.WebService;
 
 
/**
 * 天气查询的实现类
 * @author Administrator
 *
 */
@WebService
public class WeatherServiceImpl implements WeatherService {
 
        //查询天气
        public String queryWeather(String cityName) {
                System.out.println(cityName + "天气是:晴天");
                return "晴";
        }
        
}


C、发布服务,使用EndPoint类中的publish()方法发布,参数分别为服务访问的地址和服务的实现类

[Java] 纯文本查看 复制代码
package com.webservice.jaxws;
 
import javax.xml.ws.Endpoint;
 
public class ServerPoint {
 
        public static void main(String[] args) {
                //参数1:服务地址,weather为服务的名称
                //参数2:服务实现类
                Endpoint.publish("http://127.0.0.1:12345/weather", new WeatherServiceImpl());
        }
}


D、测试服务是否发布成功,阅读使用说明书,确认要调用的类、方法、参数等

    ● WSDL访问地址:http://localhost:12345/weather?wsdl

    ● WSDL说明书阅读方式:从下往上阅读

E、如何发布SOAP1.2版本的服务端

    ●   引入第三方jar包

    ●   在服务实现类上加入注解 @BindingType(SOAPBinding.SOAP12HTTP_BINDING)

[Java] 纯文本查看 复制代码
package com.webservice.jaxws;
 
import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;
 
 
/**
 * 天气查询的实现类
 * @author Administrator
 *
 */
@WebService
@BindingType(SOAPBinding.SOAP12HTTP_BINDING)
public class WeatherServiceImpl implements WeatherService {
 
        //查询天气
        public String queryWeather(String cityName) {
                System.out.println(cityName + "天气是:晴天");
                return "晴";
        }
        
}


2、客户端

(1)、开发步骤

A、在工作空间创建用于存放使用wsimport命令生成的客户端代码的java工程

B、使用jdk提供的wsimport命令生成客户端代码

    ●  wsimport命令是jdk提供的,作用是根据使用说明书生成客户端代码,wsimport只支持SOAP1.1客户端的生成

    ●  wsimport常用参数

        -d:默认参数,用于生成.class文件
        -s:生成.java文件
        -p:指定生成java文件的包名,不指定则为WSDL说明书中namespace值得倒写

C、在doc窗口进入java工程项目的src目录,执行wsimport命令

D、在Eclipse中刷新java项目,将生成的客户端代码copy到客户端工程中

E、创建服务视图,类名从<service>标签的name属性获取

F、获取服务实现类,视图实例调用getProt()方法,实现类的类名从portType的name属性获取

G、调用查询方法,方法名从portType下的operation标签的name属性获取

[Java] 纯文本查看 复制代码
package com.webservice.client;
 
import com.webservice.jaxws.WeatherServiceImpl;
import com.webservice.jaxws.WeatherServiceImplService;
 
public class Client {
 
        public static void main(String[] args) {
                //创建视图
                WeatherServiceImplService wsis = new WeatherServiceImplService();
                //获取服务实现类
                WeatherServiceImpl wsi = wsis.getPort(WeatherServiceImpl.class);
                //调用查询方法
                String weather = wsi.queryWeather("北京");
                System.out.println(weather);
        }
}


三:webService三要素详解

1.WSDL

(1)、定义

WSDL即web服务描述语言,它是服务端的使用说明书,是XML格式的文档,说明服务地址、服务类、方法、参数和返回值,是伴随服务发布成功,自动生成的

(2)、文档结构



●   <service>    服务视图,webservice的服务结点,它包括了服务端点

●   <binding>     为每个服务端点定义消息格式和协议细节

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

●   <message>   定义一个操作(方法)的数据参数(可有多个参数)

●   <types>        定义 web service 使用的全部数据类型

2.SOAP

(1)、定义

SOAP即简单对象访问协议(Simple Object Access Protocol),使用http发送XML格式的数据,他不是webservice的专有协议


(2)、结构 SOAP = HTTP + XML



(3)、协议的格式

Envelope:必须有,此元素将整个 XML 文档标识为一条SOAP消息

Header:可选元素,包含头部信息

Body:必须有,包含所有调用和响应信息

Fault:可选元素,提供有关在处理此消息时所发生的错误信息

(4)、版本

A、SOAP1.1

●   请求

POST /weather HTTP/1.1
Accept: text/xml, multipart/related
Content-Type: text/xml; charset=utf-8
SOAPAction: "http://jaxws.ws.itcast.cn/WeatherInterfaceImpl/queryWeatherRequest"
User-Agent: JAX-WS RI 2.2.4-b01
Host: 127.0.0.1:54321
Connection: keep-alive
Content-Length: 211

<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body><ns2:queryWeather xmlns:ns2="http://jaxws.ws.itcast.cn/"><arg0>北京</arg0></ns2:queryWeather>
</S:Body>
</S:Envelope>

●   响应

HTTP/1.1 200 OK
Transfer-encoding: chunked
Content-type: text/xml; charset=utf-8
Date: Fri, 04 Dec 2015 03:45:56 GMT

<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:queryWeatherResponse xmlns:ns2="http://jaxws.ws.itcast.cn/"><return>晴</return></ns2:queryWeatherResponse>
</S:Body>
</S:Envelope>

B、SOAP1.2

●   请求

POST /weather HTTP/1.1
Accept: application/soap+xml, multipart/related
Content-Type: application/soap+xml; charset=utf-8;
action="http://jaxws.ws.itcast.cn/WeatherInterfaceImpl/queryWeatherRequest"
User-Agent: JAX-WS RI 2.2.4-b01
Host: 127.0.0.1:54321
Connection: keep-alive
Content-Length: 209

<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envelope">
<S:Body><ns2:queryWeather xmlns:ns2="http://jaxws.ws.itcast.cn/"><arg0>北京</arg0></ns2:queryWeather>
</S:Body>
</S:Envelope>

●   响应

HTTP/1.1 200 OK
Transfer-encoding: chunked
Content-type: application/soap+xml; charset=utf-8
Date: Fri, 04 Dec 2015 03:55:49 GMT

<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envelope">
<S:Body>
<ns2:queryWeatherResponse xmlns:ns2="http://jaxws.ws.itcast.cn/"><return>晴</return></ns2:queryWeatherResponse>
</S:Body>
</S:Envelope>

C、SOAP1.1 和 SOAP1.2的区别

●   相同点

请求方式都是POST

协议格式都一样,都有envelope和body

●   不同点

①、数据格式不同

SOAP1.1:text/xml;charset=utf-8
SOAP1.2:application/soap+xml;charset=utf-8

②、命名空间不同

四:webservice客户端的四种调用方式

1.生成客户端调用方式

(1)、开发步骤

A、wisimport生成客户端代码

B、创建服务视图

C、获取实现类

D、调用查询方法

2.service编程实现调用

(1)、开发步骤

A、wisimport生成客户端代码
B、使用serivce类创建服务视图
C、获取服务实现类
D、调用查询方法

[Java] 纯文本查看 复制代码
import java.io.IOException;

特点:方便管理,是一个标准的开发方式
3.HttpURLConnection调用方式

(1)、开发步骤

A、创建服务地址
B、打开服务地址的一个连接
C、设置连接参数
    ●   注意
        a、POST必须大写,如果小写会出如下异常:

        b、如果不设置输入输出,会报如下异常:

D、组织SOAP协议数据,发送给服务器

E、接收服务端的响应

[mw_shl_code=java,true][mw_shl_code=java,true]
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import cn.itcast.mobile.MobileCodeWSSoap;

/**
* 
* <p>Title: ServiceClient.java</p>
* <p>Description:Service编程实现客户端</p>
*/
public class ServiceClient {

        public static void main(String[] args) throws IOException {
                //创建WSDL地址,不是服务地址
                URL url = new URL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl");
                //创建服务名称
                //1.namespaceURI - 命名空间地址
                //2.localPart - 服务名称
                QName qname = new QName("http://WebXml.com.cn/", "MobileCodeWS");
                
                //Service创建视图
                //参数:
                //1.wsdlDocumentLocation - 使用说明书地址
                //2.serviceName - 服务名称
                Service service = Service.create(url, qname);
                //获取实现类
                MobileCodeWSSoap mobileCodeWSSoap = service.getPort(MobileCodeWSSoap.class);
                //调用查询方法
                String result = mobileCodeWSSoap.getMobileCodeInfo("188888888", "");
                System.out.println(result);
        }
}
[/mw_shl_code]

[Java] 纯文本查看 复制代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
* 
* <p>Title: HttpClient.java</p>
* <p>Description:HttpURLConnection调用方式</p>
*/
public class HttpClient {

        public static void main(String[] args) throws IOException {
                //1:创建服务地址
                URL url = new URL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx");
                //2:打开到服务地址的一个连接
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                //3:设置连接参数
                //3.1设置发送方式:POST必须大写
                connection.setRequestMethod("POST");
                //3.2设置数据格式:Content-type
                connection.setRequestProperty("content-type", "text/xml;charset=utf-8");
                //3.3设置输入输出,新创建的connection默认是没有读写权限的,
                connection.setDoInput(true);
                connection.setDoOutput(true);

                //4:组织SOAP协议数据,发送给服务端
                String soapXML = getXML("1866666666");
                OutputStream os = connection.getOutputStream();
                os.write(soapXML.getBytes());
                
                //5:接收服务端的响应
                int responseCode = connection.getResponseCode();
                if(200 == responseCode){//表示服务端响应成功
                        InputStream is = connection.getInputStream();
                        InputStreamReader isr = new InputStreamReader(is);
                        BufferedReader br = new BufferedReader(isr);
                        
                        StringBuilder sb = new StringBuilder();
                        String temp = null;
                        
                        while(null != (temp = br.readLine())){
                                sb.append(temp);
                        }
                        
                        System.out.println(sb.toString());
                        
                        is.close();
                        isr.close();
                        br.close();
                }

                os.close();
        }
        
        /**
         * <?xml version="1.0" encoding="utf-8"?>
                <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
                        <soap:Body>
                                <getMobileCodeInfo xmlns="http://WebXml.com.cn/">
                                        <mobileCode>string</mobileCode>
                                        <userID>string</userID>
                                </getMobileCodeInfo>
                        </soap:Body>
                </soap:Envelope>
         * @param phoneNum
         * @return
         */
        public static String getXML(String phoneNum){
                String soapXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                +"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
                        +"<soap:Body>"
                    +"<getMobileCodeInfo xmlns=\"http://WebXml.com.cn/\">"
                            +"<mobileCode>"+phoneNum+"</mobileCode>"
                      +"<userID></userID>"
                    +"</getMobileCodeInfo>"
                 +" </soap:Body>"
                +"</soap:Envelope>";
                return soapXML;
        }
}
[/mw_shl_code]

4.Ajax调用方式

[HTML] 纯文本查看 复制代码
<!doctype html>
<html lang="en">
 <head>
  <title>Ajax调用方式</title>
  <script type="text/javascript">
        function queryMobile(){
                //创建XMLHttpRequest对象
                var xhr = new XMLHttpRequest();
                //打开链接
                xhr.open("post","http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx",true);
                //设置content-type
                xhr.setRequestHeader("content-type","text/xml;charset=utf-8");
 
                //设置回调函数
                xhr.onreadystatechange=function(){
                        //判断客户端发送成功&&服务端响应成功
                        if(4 == xhr.readyState && 200 == xhr.status){
                                alert(xhr.responseText);
                        }
                }
 
                //组织SOAP协议数据
                var soapXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                +"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
                        +"<soap:Body>"
                    +"<getMobileCodeInfo xmlns=\"http://WebXml.com.cn/\">"
                            +"<mobileCode>"+document.getElementById("phoneNum").value+"</mobileCode>"
                      +"<userID></userID>"
                    +"</getMobileCodeInfo>"
                 +" </soap:Body>"
                +"</soap:Envelope>";
                alert(soapXML);
 
                //发送请求
                xhr.send(soapXML);
        }
  </script>
 </head>
 <body>
        手机号归属地查询:<input type="text" id="phoneNum" /><input type="button" value="查询"/>
 </body>
</html>




             推荐阅读


    众览群雄,唯我杭城独秀——一贴汇总杭州校区所有就业薪资



一贴看杭州分校吃住行,学习生活攻略大集锦



全新图文杭州新校区来校路线图:


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马