WebService

一、XML Schema

schema 规范中:

① 所有标签和属性都需要有 schema 文件来定义;

② 所有的 schema 文件都必须有一个 namespace,由 targetNamespace 属性值指定,相当于该 schema 文件的 id;

③ 通过 xmlns 属性来为 xml 文件引入一个 schema 约束,xmlns 的值即为被引入约束的 namespace;

④ 如果引入的 schema 不是 w3c 组织定义的,则必须指定该 schema 文件的位置(以便 xml 解析器读取);

⑤ schema 文件的位置由 schemaLocation 属性指定,属性值格式为 [该 schema 的 namespace][空格][物理文件位置];

⑥ 如果引入了N个约束,需要给 n-1 个取别名。 

schema 文件的格式为 xsd。

一份 schema 文件的 实例:

<?xml version="1.0" encoding="UTF-8" ?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.ldj.com/bookSchema"
elementFormDefault="qualified">
<element name="书架">
  <complexType>
    <sequence maxOccurs="unbounded">
      <element name="书">
        <complexType>
          <sequence>
            <element name="书名" type="string"/>
            <element name="作者" type="string"/>
            <element name="售价" type="string"/>
          </sequence>
        </complexType>
      </element>
    </sequence>
  </complexType>
</element>
</schema>

以及使用该约束的实例:

<?xml version="1.0" encoding="UTF-8"?>
<书架 xmlns="http://www.ldj.com/bookSchema" xmlns:std="http://www.w3.org/2001/XMLSchema-instance" std:schemaLocation="http://www.ldj.com/bookSchema book.xsd">
  <>
    <书名>大话西游</书名>
    <作者>刘振宇</作者>
    <售价>23.0</售价>
  </> 
</书架>

 

二、Web Service中的几个重要术语

1. WSDL:web service definition language

① 对应一种类型的文件.wsdl。

② 定义了 web service 的服务器端与客户端应用交互传递请求和响应数据的格式和方式。

③ 一个 web service 对应一个唯一的 wsdl 文档。


2. SOAP:simple object access protocal

① 是一种简单的、基于 HTTP 和 XML 的协议,用于在 WEB 上交换结构化的数据。

② soap 消息包括:请求消息和响应消息。

③ http + xml 片断。


3. SEI:WebService EndPoint Interface

即是 WebService 服务器端用来处理请求的接口

@Webservice

@WebMethod


4. CXF:Celtix + XFire

一个apache的用于开发webservice服务器端和客户端的框架。


三、开发 WebService

WebService 的开发包括两种方式:直接使用 JDK 开发(需要 jdk1.6 及以上版本)、使用 CXF 框架开发。生产环境中一般使用框架进行 WebService 的开发。不论使用哪种方式,都需要进行 WebService 服务端和客户端的开发。

3.1 使用 JDK 开发 WebService

(1)开发服务器端

WebService 是围绕 SEI 进行工作的,所以需要编写相关业务的接口和实现类:

// in file HelloWS.java
package com.ldj.myws.helloworld;
​
​import javax.jws.WebMethod;
import javax.jws.WebService;
​
@WebService
public interface HelloWS {
  @WebMethod
  String sayHello(String name);
}
​
// in file HelloWSImpl.java
package com.ldj.myws.helloworld;
​
import javax.jws.WebService;
​
@WebService
public class HelloWSImpl implements HelloWS {
  public String sayHello(String name) {
    System.out.println("HelloWSImple.sayHello.."+name);
    return "hello " + name;
  }
}

注意,需要对接口使用 @WebService 修饰,并且对接口中定义的方法使用 @WebMethod 修饰,如上面第7、9行代码所示。另外还需对实现类也使用 @WebService 修饰,如第18行代码所示。

定义好 SEI 及其实现类后,需要对 WebService 进行发布:

package com.ldj.myws.helloworld;
​
import javax.xml.ws.Endpoint;
/*
* 发布Web Service
*/
public class ServerTest {
  public static void main(String[] args) {
    String address = "http://192.168.2.229/hellows";
    Endpoint.publish(address, new HelloWSImpl());
    System.out.println("发布 HelloWS WebService 成功");
  }
}

以上就完成了 HelloWS 的服务端开发,接下来进行对应的客户端开发。


(2)开发客户端

客户端使用某个 WebService 时必须首先知道该服务对应的 wsdl 文件。为了获取该文件,可以在 WebService 服务的 URL 后跟 ?wsdl 参数进行请求,如:http://192.168.2.229/hellows?wsdl。通过浏览器访问上面的 URL,即可获取到 helloWS 服务的 WSDL:

 

也可以通过 Eclipse 自带的 Web Service Explorer 来查看 wsdl,该工具会解析 wsdl 以对其进行可视化:

 

注意:在查看 wsdl 前,需要启动 WebService 的服务端,即运行 ServerTest 中的 main 方法。以上是使用第三方工具对 HelloWS 服务进行访问,下面开始编写代码,使用代码的方式来访问。

首先使用 jdk 的 wsimort.exe 工具生成客户端代码:

①.进入客户端项目源码根目录(即 src 目录),在此目录下运行 jdk 中的 wsimport -keep url (url 为wsdl文件的路径)。如“wsimport -keep http://192.168.2.229/hellows?wsdl”,该工具会自动生成如下的包和类:

 

②.定义客户端的测试类,添加访问 WebService 的代码:

package com.ldj.myws.client;
​
import com.ldj.myws.helloworld.HelloWSImpl;
import com.ldj.myws.helloworld.HelloWSImplService;​
​
public class ClientTest {
  public static void main(String[] args) {
    HelloWSImplService factory = new HelloWSImplService();
    HelloWSImpl helloWS = factory.getHelloWSImplPort();

    // helloWS 其实是一个代理类 com.sun.proxy.$Proxy32
    System.out.println(helloWS.getClass());

    String ret = helloWS.sayHello("laideju");
    System.out.println(ret);
  }
}

以上就完成了最简单的使用代码访问 WebService 的功能。

3.2 监听请求: 使用Eclipse的TCP/IP工具(端口转发)

 面试问题:

1.webservice相当于HTTP+?+?
   ? : xml
   ? : schema
2.wsdl是什么?
   webservice定义语言, 对应.wsdl文档, 一个webservice会对应一个唯一的wsdl文档, 定义了客户端与服务端发送请求和响应的数据格式和过程
3.如何发布一个webservice?
   定义SEI @webservice @webMethod
   定义SEI的实现
   发布:Endpoint.publish(url, SEIImplObject)
4.如何请求一个webservice?
   1.根据wsdl文档生成客户端代码  jdk/cxf
   2.根据生成的代码调用webService


 

posted @ 2020-09-25 08:58  少司命  阅读(193)  评论(0编辑  收藏  举报