zoukankan      html  css  js  c++  java
  • Web Service随笔

    什么是Web Service?

      WebService是一个SOA(面向服务的编程)的架构,它是不依赖于语言,不依赖于平台,可以实现不同的语言间的相互调用,通过Internet进行基于Http协议的网络应用间的交互。

    Web Service有什么用处?

      Web Service可以使两个不同语言和平台编写的程序实现交互,可以发布一些服务共他人使用,也可以调用他人发布的一些服务。

    Web Service需要了解的知识:

      soap:一种通信协议通过http发送xml格式的数据。

      wsdl:用来描述Web Service的,也就是Web Service的说明书

      uddl:信息注册中心的实现标准规范,同时也包含一组使企业能将自身提供的Web Service注册,以使别的企业能够发现的访问协议的实现标准。

    如何发布一个自己的服务(Web Service)

      Web Service发布和调用都有好几种方法,这里只说明其中的一种

    代码如下:

     1 package com.web.service;
     2 
     3 import javax.jws.WebService;
     4 import javax.xml.ws.Endpoint;
     5 
     6 @WebService
     7 public class PublishService {
     8     //@webService用注解的方式来将此类定义为一个web服务注意要jdk1.6及以上版本才可以使用
     9     public static void main(String[] args) {
    10         //用Endpoint将此类发布为一个服务端点
    11         Endpoint.publish("http://localhost:6789/hi", new PublishService());
    12         System.out.println("ready...");
    13     }
    14     
    15     public String sayHi(String name){
    16         return "Hello,"+name;
    17     }
    18     //用static或final修饰的方法不会再wsdl中显示
    19     public static String sayHi1(String name){
    20         return "Hello,"+name;
    21     }
    22     public final String sayHi2(String name){
    23         return "Hello,"+name;
    24     }
    25 }

    运行成功后打开浏览器访问服务端点:http://localhost:6789/hi?wsdl会显示如下界面说明发布成功:

    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 at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. 
    -->
    <!--
     Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. 
    -->
    <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://service.web.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://service.web.com/" name="PublishServiceService">
    <types>
    <xsd:schema>
    <xsd:import namespace="http://service.web.com/" schemaLocation="http://localhost:6789/hi?xsd=1"/>
    </xsd:schema>
    </types>
    <message name="sayHi">
    <part name="parameters" element="tns:sayHi"/>
    </message>
    <message name="sayHiResponse">
    <part name="parameters" element="tns:sayHiResponse"/>
    </message>
    <portType name="PublishService">
    <operation name="sayHi">
    <input wsam:Action="http://service.web.com/PublishService/sayHiRequest" message="tns:sayHi"/>
    <output wsam:Action="http://service.web.com/PublishService/sayHiResponse" message="tns:sayHiResponse"/>
    </operation>
    </portType>
    <binding name="PublishServicePortBinding" type="tns:PublishService">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
    <operation name="sayHi">
    <soap:operation soapAction=""/>
    <input>
    <soap:body use="literal"/>
    </input>
    <output>
    <soap:body use="literal"/>
    </output>
    </operation>
    </binding>
    <service name="PublishServiceService">
    <port name="PublishServicePort" binding="tns:PublishServicePortBinding">
    <soap:address location="http://localhost:6789/hi"/>
    </port>
    </service>
    </definitions>

    发布成功后,就可以根据服务端点来调用我们的服务

    如何调用Web Service:

      通过DOS命令选择盘符方便找到文件,通过DOS命令:wsimport -s . http://localhost:6789/hi?wsdl

      生成客户端类源文件文件如下图所示:

                   

    生成成功找到此文件

    新建一个新的项目将此文件copy进来

                       

    新建一个测试类

    参考 http://localhost:6789/hi?wsdl 中

    通过服务名创建对象并调用服务端口调用如下:

     1 package com.web.test;
     2 
     3 import com.web.service.PublishService;
     4 import com.web.service.PublishServiceService;
     5 
     6 public class ServiceTest {
     7     public static void main(String[] args) {
     8         //创建服务对象并获取端口
     9         PublishService port = new PublishServiceService().getPublishServicePort();
    10         //调用端口中的sayHi()方法
    11         String hi = port.sayHi("xiaoming");
    12         System.out.println(hi);
    13     }
    14 }

    这样就实现了在不同的程序中调用Web Service服务

    注意:当发布者的服务器关闭的时候调用服务会报错

    自己的总结,如有错误之处,还望各位大牛不吝赐教,谢谢。

  • 相关阅读:
    LeetCode(111) Minimum Depth of Binary Tree
    LeetCode(108) Convert Sorted Array to Binary Search Tree
    LeetCode(106) Construct Binary Tree from Inorder and Postorder Traversal
    LeetCode(105) Construct Binary Tree from Preorder and Inorder Traversal
    LeetCode(99) Recover Binary Search Tree
    【Android】通过经纬度查询城市信息
    【Android】自定义View
    【OpenStack Cinder】Cinder安装时遇到的一些坑
    【积淀】半夜突然有点想法
    【Android】 HttpClient 发送REST请求
  • 原文地址:https://www.cnblogs.com/hcl22/p/6189399.html
Copyright © 2011-2022 走看看