`

WebService介绍

    博客分类:
  • J2EE
阅读更多
记得初次接触WebService是在07年的一个日本政府项目中。后来自己研究过在Websphere下开发WebService.今天,有点时间来介绍Webservice。
Key Points:
 WebService定义
 WebService协议栈
 WebService工作流程
 WebService特点
 WebService例子

一 WebService 定义:
   我们可以这样定义:通过Soap(Simple Object Access Protocal)在Web上提供的软件服务,使用WSDL(Web Service Description Language)文件进行说明,并通过UDDI(Unified Description, Discovery and Integration)进行注册。
   有三个概念介绍下:
1. XML:
WebService要最终实现在异构平台上不同应用程序之间相互通信和共享数据,就得以一个共同的数据标准和数据格式来传递。这种数据标准和格式就是XML。
   2. SOAP:
     是基于XML的不依赖于传输协议的应用层协议,应用在应用程序中方便的传输数据文件。而传输协议,既可以是Http或TCP,也可以是SMTP或POP3,还可以是应用程序中特定的一些协议。
    正因为如此,SOAP可以满足于在不同系统中来发送和接受XML数据。
   3. WSDL:
     是用XML语言描述的网路服务方式。一个WSDL文档定义服务为一个端点或网络端口的集合,  端点及消息的抽象定义与它们具体的网络实现和参数格式绑定分离。这样就可以抽象定义:消息---需要交换的数据的抽象描述;端口类型---操作的抽象集合。因此一个完整的WSDL文档在定义网络服务的时候包含以下的7个元素:
   类型---使用某种类型系统(如XSD)定义数据类型。
   消息---通信数据抽象的所有类型的定义。
   操作---服务支持的动作的抽象描述。
   端口类型---一个操作的抽象集合,该操作由一个或多个端点支持。
   绑定---针对一个特定端口类型的具体的协议规范和数据格式规范。
   端口---一个单一的端点,定义成一个绑定和一个网络地址的连接。
   服务---相关的端点的集合。
   4. UDDI
     是一个基于Web的、分布式的、为WebService提供的信息注册中心的实现标准规范,同时也包含一组使企业能将自身提供的WebServices注册,已使其它企业能够发现的访问协议的实现标准。
     UDDI的核心组件是UDDI业务注册,它使用一个XML文档来描述企业及其提供的WebServices。从概念上讲,UDDI业务注册所提供的 信息包含三个部分:白页、黄页和绿叶。
二 Webservices协议栈
应用扩展层
应用层
服务注册查找层
服务描述层
消息封装层
传输层

三 webService工作流程
   其工作流程可以用下图表示:

1. 服务提供商将其服务(Services)发布到UDDI注册中心(Service broker)。使用到的技术包含UDDI。
2. 服务请求者通过查找UDDI注册中心的被发布服务的登记信息来发现并定位到服务。使用到技术包含WSDL和UDDI。
3. 服务请求者绑定服务提供商并使用其特定的服务。使用的技术包含WSDL和SOAP。
开发流程如下:
  当开发者开发一个新的Webservice,可以通过登录到提供UDDI搜索引擎的Web页面,并在UDDI注册表中找到自己需要的WebService,然后在UDDI注册表内,或通过UDDI注册表中的连接找到该Webservice的调用细节,该调用细节采用WSDL描述。开发人员通过开发工具编辑该细节,然后在应用程序中加上对WSDL定义的WebService的调用逻辑。最后这个应用可以通过Soap来调用指定的Webservice。

四. WebService的特点
   1. 互操作性
    任何webservice都可以实现互访,原因是有了SOAP,并且所有的软件厂商都对此提供支持。
2. 普遍性
Webservice使用广泛的传输协议与XML消息报文格式进行通讯。因此,所有任何支持这些技术的计算设备都可以访问Webservice。
  3.低成本
     由于有大的软件厂商支持Webservice,所以对于以往的COM组件和JavaBean都可以被升级为WebService。
WebService体系结构的主要优点之一:允许在不同平台上,以不同语言编写的各种程序以基于标准的方式相互通信,同时使用标准的web协议:XML,HTTP和TCP/IP.
五.WebService的Sample:
  1. WebService WSDL:
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://webservice.cmm.webapp" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://webservice.cmm.webapp" xmlns:intf="http://webservice.cmm.webapp" xmlns:tns1="http://cmmw0001.webservice.vo.cmm" xmlns:tns2="http://webservice.vo.cmm" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <wsdl:types>
  <schema elementFormDefault="qualified" targetNamespace="http://webservice.cmm.webapp" xmlns="http://www.w3.org/2001/XMLSchema">
   <import namespace="http://cmmw0001.webservice.vo.cmm"/>
   <import namespace="http://webservice.vo.cmmp"/>
   <element name="cmmw0001">
    <complexType>
     <sequence>
      <element name="request" type="tns1:CMMW0001Request"/>
     </sequence>
    </complexType>
   </element>
   <complexType name="ArrayOf_tns1_CMMW0001HimokuInfo">
    <sequence>
     <element maxOccurs="11" minOccurs="0" name="item" type="tns1:CMMW0001HimokuInfo"/>
    </sequence>
   </complexType>
   <element name="cmmw0001Response">
    <complexType>
     <sequence>
      <element name="cmmw0001Return" type="tns1:CMMW0001Response"/>
     </sequence>
    </complexType>
   </element>
</schema>
  <schema elementFormDefault="qualified" targetNamespace="http://cmmw0001.webservice.vo.cmm" xmlns="http://www.w3.org/2001/XMLSchema">
   <import namespace="http://webservice.cmm.webapp"/>
   <import namespace="http://webservice.vo.cmm"/>
   <complexType name="CMMW0001KeyInfo">
    <sequence>
     <element name="haibun_year" nillable="true" type="xsd:int"/>
     <element name="jigyo_code" nillable="true" type="xsd:string"/>
     <element name="koji_no" nillable="true" type="xsd:string"/>
     <element name="saimu_kbn" nillable="true" type="xsd:string"/>
     <element name="syumuka" nillable="true" type="xsd:string"/>
     <element name="year" nillable="true" type="xsd:int"/>
     <element name="yosan_type" nillable="true" type="xsd:string"/>
    </sequence>
   </complexType>
   <complexType name="CMMW0001HimokuInfo">
    <sequence>
     <element name="himoku_all" nillable="true" type="xsd:long"/>
     <element name="himoku_code" nillable="true" type="xsd:string"/>
     <element name="himoku_kurkosi" nillable="true" type="xsd:long"/>
     <element name="himoku_later" nillable="true" type="xsd:long"/>
     <element name="himoku_prev" nillable="true" type="xsd:long"/>
     <element name="himoku_this" nillable="true" type="xsd:long"/>
    </sequence>
   </complexType>
   <complexType name="CMMW0001YhInfo">
    <sequence>
     <element name="biko" nillable="true" type="xsd:string"/>
     <element name="disaster_type" nillable="true" type="xsd:string"/>
     <element name="haibun_date" nillable="true" type="xsd:int"/>
     <element name="haibun_kind" nillable="true" type="xsd:string"/>
     <element name="henko_cnt" nillable="true" type="xsd:int"/>
     <element name="himoku_info" nillable="true" type="impl:ArrayOf_tns1_CMMW0001HimokuInfo"/>
     <element name="jutaku_code" nillable="true" type="xsd:string"/>
     <element name="jutaku_name" nillable="true" type="xsd:string"/>
     <element name="jutaku_type" nillable="true" type="xsd:string"/>
     <element name="koji_num" nillable="true" type="xsd:string"/>
     <element name="rokasen_code" nillable="true" type="xsd:string"/>
     <element name="rokasen_name" nillable="true" type="xsd:string"/>
     <element name="seko_ed_code" nillable="true" type="xsd:string"/>
     <element name="seko_ed_name" nillable="true" type="xsd:string"/>
     <element name="seko_st_code" nillable="true" type="xsd:string"/>
     <element name="seko_st_name" nillable="true" type="xsd:string"/>
     <element name="sikko_kikan" nillable="true" type="xsd:string"/>
     <element name="syutai" nillable="true" type="xsd:string"/>
     <element name="tanto_group" nillable="true" type="xsd:string"/>
     <element name="torikeshi_kbn" nillable="true" type="xsd:string"/>
     </sequence>
   </complexType>
   <complexType name="CMMW0001RequestBody">
    <sequence>
     <element name="key_info" nillable="true" type="tns1:CMMW0001KeyInfo"/>
     <element name="yh_info" nillable="true" type="tns1:CMMW0001YhInfo"/>
    </sequence>
   </complexType>
   <complexType name="CMMW0001Request">
    <sequence>
     <element name="collabo_root" nillable="true" type="tns1:CMMW0001RequestBody"/>
     <element name="common_head" nillable="true" type="tns2:RequestCommonHead"/>
    </sequence>
   </complexType>
   <complexType name="CMMW0001Response">
    <sequence>
     <element name="common_head" nillable="true" type="tns2:ResponseCommonHead"/>
    </sequence>
   </complexType>
  </schema>
  <schema elementFormDefault="qualified" targetNamespace="http://webservice.vo.cmm" xmlns="http://www.w3.org/2001/XMLSchema">
   <import namespace="http://webservice.cmm.webapp"/>
   <import namespace="http://cmmw0001.webservice.vo.cmm"/>
   <complexType name="RequestCommonHead">
    <sequence>
     <element name="collabodst" nillable="true" type="xsd:string"/>
     <element name="collabomode" nillable="true" type="xsd:string"/>
     <element name="collabosrc" nillable="true" type="xsd:string"/>
     <element name="infoid" nillable="true" type="xsd:string"/>
     <element name="securekey" nillable="true" type="xsd:string"/>
    </sequence>
   </complexType>
   <complexType name="ResponseCommonHead">
    <sequence>
     <element name="collabodst" nillable="true" type="xsd:string"/>
     <element name="collabomode" nillable="true" type="xsd:string"/>
     <element name="collabosrc" nillable="true" type="xsd:string"/>
     <element name="detail" nillable="true" type="xsd:string"/>
     <element name="errmsg" nillable="true" type="xsd:string"/>
     <element name="infoid" nillable="true" type="xsd:string"/>
     <element name="replydate" nillable="true" type="xsd:dateTime"/>
     <element name="result" nillable="true" type="xsd:string"/>
     <element name="status" nillable="true" type="xsd:string"/>
    </sequence>
   </complexType>
  </schema>
</wsdl:types>
<wsdl:message name="cmmw0001Request">
      <wsdl:part element="impl:cmmw0001" name="parameters"/>
 </wsdl:message>
 <wsdl:message name="cmmw0001Response">
      <wsdl:part element="impl:cmmw0001Response" name="parameters"/>
</wsdl:message>
<wsdl:portType name="ZaimuCollaboWebService">
      <wsdl:operation name="cmmw0001">
         <wsdl:input message="impl:cmmw0001Request" name="cmmw0001Request"/>
         <wsdl:output message="impl:cmmw0001Response" name="cmmw0001Response"/>
      </wsdl:operation>
</wsdl:portType>

   <wsdl:binding name="ZaimuCollaboWebServiceSoapBinding" type="impl:ZaimuCollaboWebService">
      <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
      <wsdl:operation name="cmmw0001">
         <wsdlsoap:operation soapAction=""/>
         <wsdl:input name="cmmw0001Request">
            <wsdlsoap:body use="literal"/>
         </wsdl:input>
         <wsdl:output name="cmmw0001Response">
            <wsdlsoap:body use="literal"/>
         </wsdl:output>
      </wsdl:operation>
   </wsdl:binding>

   <wsdl:service name="ZaimuCollaboWebServiceService">
      <wsdl:port binding="impl:ZaimuCollaboWebServiceSoapBinding" name="ZaimuCollaboWebService">
         <wsdlsoap:address location="http://localhost:9080/koujiweb/services/ZaimuCollaboWebService"/>
      </wsdl:port>
   </wsdl:service>
</wsdl:definitions>

2  ZaimuCollaboWebService.java
import java.rmi.RemoteException;
import java.sql.SQLException;
import cmm.common.exception.WebServiceException;
import cmm.delegate.webservice.cmmw0001.CMMW0001DelegateFactory;
import cmm.delegate.webservice.cmmw0001.IFCMMW0001Delegate;
import cmm.vo.webservice.RequestCommonHead;
import cmm.vo.webservice.ResponseCommonHead;
import cmm.vo.webservice.cmmw0001.CMMW0001Request;
import cmm.vo.webservice.cmmw0001.CMMW0001Response;
public class ZaimuCollaboWebService
{
  public CMMW0001Response cmmw0001(CMMW0001Request request)
  {
    CMMW0001Response rtn = new CMMW0001Response();

    String uniqueKey = request.getCommon_head().getInfoid();

    IFCMMW0001Delegate delegate = CMMW0001DelegateFactory.getInstance(uniqueKey);
    try
    {
      rtn = delegate.service(uniqueKey, request);
      rtn.setCommon_head(new ResponseCommonHead(request.getCommon_head()));
    } catch (RemoteException e) {
      ResponseCommonHead commonHead = new ResponseCommonHead(request.getCommon_head());
      processException(commonHead, e);
      rtn.setCommon_head(commonHead);
    }

    return rtn;
  }

  private void processException(ResponseCommonHead commonHead, RemoteException e)
  {
    Throwable detail = e.detail;

    if (detail instanceof WebServiceException)
    {
      WebServiceException webServiceException = (WebServiceException)detail;

      commonHead.setResult("1");
      commonHead.setStatus(webServiceException.getStatus());
      commonHead.setDetail(webServiceException.getDetail());
      commonHead.setErrmsg(webServiceException.getErrmsg());
    }
    else if (detail instanceof SeriousSQLException)
    {
      SQLException sqlException = ((SeriousSQLException)detail).getException();

      commonHead.setResult("1");
      commonHead.setStatus("30799999");
      commonHead.setDetail(sqlException.getSQLState());
      commonHead.setErrmsg(sqlException.getMessage());
    }
    else
    {
      commonHead.setResult("1");
      commonHead.setStatus("30799999");
      commonHead.setErrmsg(detail.getMessage());
    }
  }
}

3  CMMW001Request.java
import java.io.Serializable;
import cmm.vo.webservice.RequestCommonHead;

public class CMMW0001Request implements Serializable {
	
	private RequestCommonHead _Common_head;
	private CMMW0001RequestBody _Collabo_root;

	public RequestCommonHead getCommon_head() {
		return _Common_head;
	}

	
	public void setCommon_head(RequestCommonHead common_head) {
		_Common_head = common_head;
	}

	public CMMW0001RequestBody getCollabo_root() {
		return _Collabo_root;
	}

	public void setCollabo_root(CMMW0001RequestBody collabo_root) {
		_Collabo_root = collabo_root;
	}

	public String toString() {
		return (new StringBuffer("CMMW0001Request:["))
			.append("Common_head=").append(_Common_head).append(" ")
			.append("Collabo_root=").append(_Collabo_root).append(" ")
			.append("]")
			.toString();
	}
}

4  CMMW001Response.java
import java.io.Serializable;
import cmm.vo.webservice.ResponseCommonHead;

public class CMMW0001Response implements Serializable {

	private ResponseCommonHead _Common_head;
	public ResponseCommonHead getCommon_head() {
		return _Common_head;
	}
	public void setCommon_head(ResponseCommonHead common_head) {
		_Common_head = common_head;
	}
	public String toString() {
		return (new StringBuffer("CMMW0001Response:["))
			.append("Common_head=").append(_Common_head).append(" ")
			.append("]")
			.toString();
	}
}

5  ResponseCommonHead.java
import java.io.Serializable;

public class RequestCommonHead implements Serializable {

	private String _Infoid;
	private String _Collabosrc;
	private String _Collabodst;
	private String _Collabomode;
	private String _Securekey;

	public String getInfoid() {
		return _Infoid;
	}
	public void setInfoid(String infoid) {
		_Infoid = infoid;
	}
	public String getCollabosrc() {
		return _Collabosrc;
	}
	public void setCollabosrc(String collabosrc) {
		_Collabosrc = collabosrc;
	}
	public String getCollabodst() {
		return _Collabodst;
	}
	public void setCollabodst(String collabodst) {
		_Collabodst = collabodst;
	}
	public String getCollabomode() {
		return _Collabomode;
	}
	public void setCollabomode(String collabomode) {
		_Collabomode = collabomode;
	}
	public String getSecurekey() {
		return _Securekey;
	}
	public void setSecurekey(String securekey) {
		_Securekey = securekey;
	}
	public String toString() {
		return (new StringBuffer("RequestCommonHead:["))
			.append("Infoid=").append(_Infoid).append(" ")
			.append("Collabosrc=").append(_Collabosrc).append(" ")
			.append("Collabodst=").append(_Collabodst).append(" ")
			.append("Collabomode=").append(_Collabomode).append(" ")
			.append("Securekey=").append(_Securekey).append(" ")
			.append("]")
			.toString();
	}
}

6  CMMW001RequestBody.java
import java.io.Serializable;

public class CMMW0001RequestBody implements Serializable {
	
	private CMMW0001KeyInfo _Key_info;
	private CMMW0001YhInfo _Yh_info;

	public CMMW0001KeyInfo getKey_info() {
		return _Key_info;
	}
	public void setKey_info(CMMW0001KeyInfo key_info) {
		_Key_info = key_info;
	}
	public CMMW0001YhInfo getYh_info() {
		return _Yh_info;
	}

	public void setYh_info(CMMW0001YhInfo yh_info) {
		_Yh_info = yh_info;
	}
	public String toString() {
		return (new StringBuffer("CMMW0001RequestBody:["))
			.append("Key_info=").append(_Key_info).append(" ")
			.append("Yh_info=").append(_Yh_info).append(" ")
			.append("]")
			.toString();
	}
}

WebService的介绍就此结束。
  • 大小: 28.5 KB
1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics