美文网首页
webservice

webservice

作者: guideEmotion | 来源:发表于2019-05-19 23:15 被阅读0次

    参考:尚硅谷 webservice

    什么是Web service

    1. 基于web的服务;服务器端提供资源让客户端访问
    2. 一个跨语言、跨平台的规范(抽象)
    3. 多个跨语言、跨平台的应用间通信整合的方案(实际)

    1.1 重要术语

    WSDL web service definition language

    web service定义语言

    • 对于了一种类型的文件.wsdl
    • 定义了webservice服务器端和客户端应用交互传递请求和响应数据的格式
    • 一个web service对应一个唯一的wsdl文档

    SOAP simple object access protocol

    1. 是一种简单的,基于http和xml的协议,用于在web上交换结构化的数据
    2. soap消息:请求消息和响应消息
    3. http+xml片段

    SEI web service Endpoint Interface

    web servicew的终端接口
    就是web service服务器用来处理请求的接口

    CXF celtix+XFire

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

    二 使用jdk开发服务端

    使用jdk开发(1.6及以上)

    • web service编码
      @WebServiceSEI和SEI的实现类
      @WebMethodSEI中的所有方法

    开发服务端

    接口

    package com.zyc.ws;
    
    import javax.jws.WebMethod;
    import javax.jws.WebService;
    
    @WebService
    public interface HelloWs {
    
        
        @WebMethod
        public String sayHello(String name) ;
        
    }
    

    实现类

    package com.zyc.ws;
    
    import javax.jws.WebService;
    
    @WebService
    public class HelloWsImpl implements HelloWs {
    
        @Override
        public String sayHello(String name) {
            System.out.println("server sayHello");
            return "hello" + name;
        }
        
    
    }
    

    发布服务

    package com.zyc.ws.service;
    
    import javax.xml.ws.Endpoint;
    
    import com.zyc.ws.HelloWsImpl;
    
    /**
     * 发布webservice
     *
     */
    public class ServerTest {
        
        public static void main(String[] args) {
            String address = "http://localhost:8888/ws/hellows";
            Endpoint.publish(address, new HelloWsImpl());
            System.out.println("ServerTest start");
        }
        
    }
    

    开发客户端

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

    wsimport -keep url//url是wsdl文件的路径
    
    image.png

    生成目录

    image.png

    调用

    package com.zyc.ws.test;
    
    import com.zyc.ws.HelloWsImpl;
    import com.zyc.ws.HelloWsImplService;
    
    public class ClientTest {
        public static void main(String[] args) {
            HelloWsImplService factory = new HelloWsImplService();
            HelloWsImpl helloWsImpl = factory.getHelloWsImplPort();
            
            String result = helloWsImpl.sayHello("zhuyc");
            System.out.println("client receive:"+result);
        }
        
    }
    

    使用TCP/IP Monitor监听请求(端口转发)

    1.将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 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://ws.zyc.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://ws.zyc.com/" name="HelloWsImplService">
    <types>
    <xsd:schema>
    <xsd:import namespace="http://ws.zyc.com/" schemaLocation="http://localhost:8889/ws/hellows?xsd=1"></xsd:import>
    </xsd:schema>
    </types>
    <message name="sayHello">
    <part name="parameters" element="tns:sayHello"></part>
    </message>
    <message name="sayHelloResponse">
    <part name="parameters" element="tns:sayHelloResponse"></part>
    </message>
    <portType name="HelloWsImpl">
    <operation name="sayHello">
    <input wsam:Action="http://ws.zyc.com/HelloWsImpl/sayHelloRequest" message="tns:sayHello"></input>
    <output wsam:Action="http://ws.zyc.com/HelloWsImpl/sayHelloResponse" message="tns:sayHelloResponse"></output>
    </operation>
    </portType>
    <binding name="HelloWsImplPortBinding" type="tns:HelloWsImpl">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"></soap:binding>
    <operation name="sayHello">
    <soap:operation soapAction=""></soap:operation>
    <input>
    <soap:body use="literal"></soap:body>
    </input>
    <output>
    <soap:body use="literal"></soap:body>
    </output>
    </operation>
    </binding>
    <service name="HelloWsImplService">
    <port name="HelloWsImplPort" binding="tns:HelloWsImplPortBinding">
    <soap:address location="http://localhost:8800/ws/hellows"></soap:address>
    </port>
    </service>
    </definitions>
    

    改成8800端口

    2.生成客户端代码

    wsimport -keep C:\workspace\sts\learn\ws_client2\src\hello.wsdl
    

    3.客户端调用,实际上端口转发到了8889端口


    image.png

    三 使用CXF框架开发(工作中)

    1.导入maven

    <dependencies>
        <!--添加cxf支持  -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.1.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>3.1.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-core</artifactId>
            <version>3.1.9</version>
        </dependency>
      </dependencies>
    

    2.代码和jdk开发相同

    wsdl文档解析

    <?xml version='1.0' encoding='UTF-8'?>
    <wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://ws.zyc.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="HelloWsImplService" targetNamespace="http://ws.zyc.com/">
      <wsdl:types>
        <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://ws.zyc.com/" elementFormDefault="unqualified" targetNamespace="http://ws.zyc.com/" version="1.0">
        <xs:element name="sayHello" type="tns:sayHello"/>
        <xs:element name="sayHelloResponse" type="tns:sayHelloResponse"/>
        <xs:complexType name="sayHello">
          <xs:sequence>
            <xs:element minOccurs="0" name="arg0" type="xs:string"/>
          </xs:sequence>
        </xs:complexType>
    
        <xs:complexType name="sayHelloResponse">
          <xs:sequence>
            <xs:element minOccurs="0" name="return" type="xs:string"/>
          </xs:sequence>
        </xs:complexType>
        </xs:schema>
      </wsdl:types>
    
      <wsdl:message name="sayHello">
        <wsdl:part element="tns:sayHello" name="parameters">
        </wsdl:part>
      </wsdl:message>
      <wsdl:message name="sayHelloResponse">
        <wsdl:part element="tns:sayHelloResponse" name="parameters">
        </wsdl:part>
      </wsdl:message>
      <wsdl:portType name="HelloWs">
        <wsdl:operation name="sayHello">
          <wsdl:input message="tns:sayHello" name="sayHello">
        </wsdl:input>
          <wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse">
        </wsdl:output>
        </wsdl:operation>
      </wsdl:portType>
      <wsdl:binding name="HelloWsImplServiceSoapBinding" type="tns:HelloWs">
        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
        <wsdl:operation name="sayHello">
          <soap:operation soapAction="" style="document"/>
          <wsdl:input name="sayHello">
            <soap:body use="literal"/>
          </wsdl:input>
          <wsdl:output name="sayHelloResponse">
            <soap:body use="literal"/>
          </wsdl:output>
        </wsdl:operation>
      </wsdl:binding>
      <wsdl:service name="HelloWsImplService">
        <wsdl:port binding="tns:HelloWsImplServiceSoapBinding" name="HelloWsImplPort">
          <soap:address location="http://localhost:8889/ws/hellows"/>
        </wsdl:port>
      </wsdl:service>
    </wsdl:definitions>
    

    文档结构

    image.png

    portType 端口类型,用来定义服务器端的SEI

    • operation 指定SEI中处理请求的方法
      • input 指定客户端应用传过来的数据,会引用上面定义的message
      • input 指定服务器端响应的数据,会引用上面定义的message

    service一个web service容器
    + name属性 指定一个客户端容器类
    + port 指定一个服务器端的入口(SEI的实现)

    binding 用于定义SEI的实现类

    • type属性 引用上面的portType
    • operation 用来定义实现的方法
      `<soap:binding style="document" >绑定的数据是一个document(xml)

    message 用来定义消息的结构

    • part 指定引用types中定义的标签片段

    cxf也可以生成客户端代码


    image.png

    TCP/IP monitor监听请求和响应
    请求

    <?xml version="1.0"  standalone="no"?>
    <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
      <S:Body>
    
      <ns2:sayHello xmlns:ns2="http://ws.zyc.com/">
        <arg0>zhuyc2</arg0>
      </ns2:sayHello>
    
      </S:Body>
    </S:Envelope>
    

    响应

    <?xml version="1.0"  standalone="no"?>
      <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
        <S:Body>
    
        <ns2:sayHelloResponse xmlns:ns2="http://ws.zyc.com/">
          <return>hellozhuyc2</return>
        </ns2:sayHelloResponse>
    
      </S:Body>
    </S:Envelope>
    

    这里又和wsdl对应

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
        xmlns:tns="http://ws.zyc.com/" 
        elementFormDefault="unqualified" 
        targetNamespace="http://ws.zyc.com/" version="1.0">
    
        <xs:element name="sayHello" type="tns:sayHello"/>
        <xs:element name="sayHelloResponse" type="tns:sayHelloResponse"/>
        <xs:complexType name="sayHello">
          <xs:sequence>
            <xs:element minOccurs="0" name="arg0" type="xs:string"/>
          </xs:sequence>
        </xs:complexType>
    
        <xs:complexType name="sayHelloResponse">
          <xs:sequence>
            <xs:element minOccurs="0" name="return" type="xs:string"/>
          </xs:sequence>
        </xs:complexType>
        </xs:schema>
    

    文档结构图

    image.png

    cxf支持的数据类型

    • 基本数据类型 int float boolean等
    • 引用类型 (string 集合 数组 list Set Map(jdk不支持map))
    • 自定义类型

    请求的流程

    image.png
    image.png

    四 cxf框架

    cxf拦截器

    image.png
    可以实现的功能
    image.png

    spring整合webservice

    相关文章

      网友评论

          本文标题:webservice

          本文链接:https://www.haomeiwen.com/subject/qupfzqtx.html