`

JAVA web service --- overview

 
阅读更多

Web service的出现是为了解决不同平台上的软件应用系统间的相互通信,比如发布在Unix上的用JAVA开发的付费系统想调用.Net开发的发布在NT 上的库存系统的某个功能。早些时,这种需求叫做EAI(企业软件集成)。后来,人们就想,既然这种需求会在以后出现,为什么不在开发的时候就考虑进去呢,比如在开发库存系统的时候,除了用户可以通过GUI查询到库存之外,还给别的系统提供了一个程序接口。这种设计被叫做SOA(面向服务的架构).【除了 Web service能够完成这种需求,另外经常用到的就是Messaging,消息中间件】

说到Web service,必须说说WSDL和SOAP。WSDL描述了提供 service的家伙都能干些什么,SOAP是来往通信的内容应该是什么样的格式。这两个东西能够大红大紫,是因为它们站在了巨人XML的肩膀上,尤其是WSDL。

WSDL的全称是 web service description language,是XML格式,根节点是definitions,它有五个子节点,types,message,portType,binding和service。它们之间的关系是types被message用,message被portType用,portType被binding用,binding被service用。

拿到一个WSDL文件后,最好是从service开始看。
service:描述了这个service的名称,这个service的地址,还有用到的binding('bingding=')。
e.g.
<wsdl:service name="WSCXFProviderService">
<wsdl:port name="WSCXFProviderPort"
binding="tns:WSCXFProviderServiceSoapBinding">
<soap:address
location="http://localhost:8080/WSCXFProviderPort" />
</wsdl:port>
</wsdl:service>

binding:描述调用webservice时候的通信协议和SOAP包形成的一些细节。【transport,style,use】
bingding通过 'type='关联到portType,一个binding里可以有多个operation,可以把operation理解为这个web service提供的一个功能,包含了style和use的定义(函数).
e.g.
<wsdl:binding name="WSCXFProviderServiceSoapBinding"
type="tns:WSCXFProvider">
<soap:binding style="document"
transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="testWS">
<soap:operation soapAction="" style="document" />
<wsdl:input name="testWS">
<soap:body use="literal" />
</wsdl:input>
<wsdl:output name="testWSResponse">
<soap:body use="literal" />
</wsdl:output>
<wsdl:fault name="IllegalArgumentException">
<soap:fault name="IllegalArgumentException"
use="literal" />
</wsdl:fault>
</wsdl:operation>
</wsdl:binding>

Style可以为document和rpc:

RPC:传输的request是对要调用的方法的描述,方法名称,参数...

Document:传输的request是符合在type中对参数的定义的一个'文档',不需要有方法的描述.

use可以为literal和encding:

encoding:传输的request中的参数是soap规定的类型,每个element带有'xsd'指定的类型,如 <a xsi:type="xsd:int">1</a>...

Document:传输的requestelement中element没有类型说明,接受端根据WSDL中定义的type进行检查.



portType:描述了这个service能够提供些什么样的功能,和这些功能交互时候的消息格式。
同样,一个portType里可以有多个operation,可以把portType理解为JAVA中的接口(Interface),operation就是这些接口定义的函数。
e.g.
<wsdl:portType name="WSCXFProvider">
<wsdl:operation name="testWS">
<wsdl:input name="testWS" message="tns:testWS"></wsdl:input>
<wsdl:output name="testWSResponse"
message="tns:testWSResponse">
</wsdl:output>
<wsdl:fault name="IllegalArgumentException"
message="tns:IllegalArgumentException">
</wsdl:fault>
</wsdl:operation>
</wsdl:portType>

message:定义了调用某个功能时候的request,response,falt的格式。
e.g.
<wsdl:message name="testWS">
<wsdl:part name="parameters" element="tns:testWS"></wsdl:part>
</wsdl:message>
<wsdl:message name="IllegalArgumentException">
<wsdl:part name="IllegalArgumentException"
element="tns:IllegalArgumentException">
</wsdl:part>
</wsdl:message>
<wsdl:message name="testWSResponse">
<wsdl:part name="parameters" element="tns:testWSResponse">
</wsdl:part>
</wsdl:message>

types:定义了message里request,response,falt的具体type。
【不在message里直接定义types,是types可以重用】
e.g.
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://test.cxf.bt.com/"
attributeFormDefault="unqualified" elementFormDefault="unqualified"
targetNamespace="http://test.cxf.bt.com/">
<xs:element name="testWS" type="tns:testWS" />
<xs:element name="testWSResponse" type="tns:testWSResponse" />
<xs:complexType name="testWS">
<xs:sequence>
<xs:element minOccurs="0" name="arg0"
type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="testWSResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return"
type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:element name="IllegalArgumentException"
type="tns:IllegalArgumentException" />
<xs:complexType name="IllegalArgumentException">
<xs:sequence />
</xs:complexType>
</xs:schema>
</wsdl:types>

SOAP:simple object access protocal,把它叫做protocal,经常让人误解为通信协议,其实它应该是一种格式协议。

Envelope 元素是 SOAP 消息的根元素。
可选的 SOAP Header 元素可包含有关 SOAP 消息的应用程序专用信息(比如认证、支付等)。如果 Header 元素被提供,则它必须是 Envelope 元素的第一个子元素。
必需的 SOAP Body 元素可包含打算传送到消息最终端点的实际 SOAP 消息。
Body里面是消息的消息体(payload)或者出错时候的一个SOAP Fault 元素,如果已提供了 Fault 元素,则它必须是 Body 元素的子元素。在一条 SOAP 消息中,Fault 元素只能出现一次。

e.g.根据上面WSDL的SOAP request:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:test="http://test.cxf.bt.com/">
<soapenv:Header/>
<soapenv:Body>
<test:testWS>
<arg0>message passed in</arg0>
</test:testWS>
</soapenv:Body>
</soapenv:Envelope>
返回的一个SOAP response:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:testWSResponse xmlns:ns2="http://test.cxf.bt.com/">
<return>Returned: message passed in</return>
</ns2:testWSResponse>
</soap:Body>
</soap:Envelope>
出错时候包含fault的response:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns1:Fault xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/">
<faultcode>ns1:Server</faultcode>
<faultstring>illegal argument.</faultstring>
<detail>
<ns1:IllegalArgumentException xmlns:ns1="http://test.cxf.bt.com/"/>
</detail>
</ns1:Fault>
</soap:Body>
</soap:Envelope>

分享到:
评论

相关推荐

    Microservices for Java EE Architects-DVT Press(2016).azw3

    The first chapter defines microservices architecture and contrasts it with a layered web application architecture described in the Java EE Architect’s Handbook. I’ll summarize the benefits and costs...

    Building RESTful Web Services with Java EE 8

    By the end of this book, you will have implemented several working web services and have a thorough understanding of the Java EE 8 APIs required for lightweight web service development. What you will ...

    The Java EE 6 Tutorial Basic Concepts 4th Edition

    Chapter 7: Using JavaServer Faces Technology in Web Pages 113 Setting Up a Page 113 Adding Components to a Page Using HTML Tags 114 Using Core Tags 143 Chapter 8: Using Converters, Listeners...

    Web Services Essentials

    Web Services Essentials provides a quick overview, Java tutorials with sample code, samples of the XML documents underlying the service, and explanations of freely-available Java APIs.

    JavaSE-6.0-英文手册(2008/11/30_FullUpdate)

    Java SE 6 Overview New Features and Enhancements (available only on the java.sun.com website) Java Language Java Programming Language Virtual Machine Virtual Machine Base Libraries java.lang...

    Mastering+Resin.rar

    Chapter 9 - Hessian Binary Web Service Protocol Chapter 10 - Parsing Chapter 11 - Database Connectivity Chapter 12 - Debugging and Logging Chapter 13 - Security Chapter 14 - Page Caching ...

    RESTful.Java.Web.Services.Security

    A step-by-step guide portraying the importance of securing a RESTful web service with simple examples applied to real-world scenarios In Detail This book will serve as a practical companion for you to...

    servlet2.4doc

    Overview Package Class Tree Deprecated Index Help PREV NEXT FRAMES NO FRAMES A B C D E F G H I J L P R S U V -------------------------------------------------------------------------------- A ...

    jdk-9.0.1_doc-all 最新版

    Defines the Java API for XML-Based Web Services (JAX-WS), and the Web Services Metadata API. java.xml.ws.annotation Defines a subset of the Common Annotations API to support programs running on the ...

    Learning Android: Develop Mobile Apps Using Java and Eclipse(第二版)

    Chapter 15 Networking and Web Overview Quick Example Networking Basics HTTP API Apache HTTP Client HttpUrlConnection Networking in the Background using AsyncTask and AsyncTaskLoader Summary Chapter 16...

    liunx soa 集群安装配置手册 英文版

    Oracle Web Services Manager (OWSM) Oracle Business Rules Oracle Application Server (includes Oracle HTTP Server, Oracle Containers for J2EE (OC4J or J2EE Server), Oracle Enterprise Manager 10g ...

    Xamarin 4.x Cross-Platform Application Development [Third Edition ,2017]

    We will walk you through a popular application, complete with a back-end web service and native features such as GPS location, camera, push notifications, and other core features. Additionally, you'...

    Spring.MVC.Cookbook.1784396419

    Over 40 recipes for creating cloud-ready Java web applications with Spring MVC About This Book Configure Spring MVC to build logic-less controllers that transparently support the most advanced web ...

    SAP PO/PI教程 Process Orchestration The Comprehensive Guide

    11.7.2 Accessing Events through a Java Message Service API 11.8 Debugging and Troubleshooting SAP BPM Processes 11.8.1 Place Breakpoints in the Process 11.8.2 Add a Debug Configuration 11.9 Tuning...

    Java.for.Bioinformatics.and.Biomedical.Applications.(2007).part1.rar

    It next provides an overview of emerging architectural trends such as Web Services and Service-Oriented Architecture. The book is not as much about the J2EE platform as it is about its application to...

    BlazeDS开发者指南

    Chapter 10: Connecting to the Java Message Service (JMS) About JMS 141 Configuring the Messaging Service to connect to a JMSAdapter 143 Part 6: Administering BlazeDS applications Chapter 11: Logging ...

    mastering-spring-cloud2018

    An active internet connection Java 8+ Docker Maven Git client Chapter 1, Introduction to Microservices, will introduce you to the ...on these platforms using command-line tools or web console.

    Java/JavaEE 学习笔记

    Java/JavaEE 学习笔记 作者在杰普学习时的学习笔记,是J2ee初学者必备手册,是大家学习J2EE开发的很好的参考笔记。 Java/JavaEE 学习笔记 内容目录: Unix 学习笔记..........7 ...Web Service学习笔记.....388

    OSGI in Action

    Providing a web service 494 ■ Consuming a web service 499 Distributing services 502 15.3 Summary 510 appendix A Building bundles 513 appendix B OSGi standard services 528 index 531

    Apache Geronimo 2.1_ Quick Reference.pdf

    Java Authentication and Authorization Service (JAAS) 150 Java Authorization Contract for Containers (JACC) 150 The Common Secure Interoperability Version 2 (CSIv2) protocol 150 Securing the server ...

Global site tag (gtag.js) - Google Analytics