zoukankan      html  css  js  c++  java
  • Spring Web Services 框架入门研究发布服务

    关于Spring Web Services框架 
    Spring web services(以下简称:Spring WS)框架是springframework东家SpringSource公司旗下的一个子项目。目前的版本是1.5M1,最新版本可以从spirngframework网站下载,项目地址是:http://www.springframework.org/node/567 
    由于怀着对spring框架的热爱,于是打算学习下这个框架。

     

    Spring Web Services框架的特点

    • Spring框架的支持,你可以重用Spring的所有特性。
    • Spring WS使用Contract First(自顶向下)的设计方式。Spring WS不支持Contract Last(自底向上)
    • 支持几乎所有的XML API,处理传入XML消息的时候就不限于JAX-P,可以是任意的XML API,选择你所擅长的。
    • 灵活的XML Marshalling,Object/XML mapping产品的支持,包括JAX-B1,2,XMLBean以及Castor等等
    • Security支持,集成了Acegi.实现web services 认证。

    Spring Web Services框架的分析

     

         1.为什么使用Contract First.

     

      最佳实践认为:使用自顶向下的设计方式也就是采用XML/XSD to JAVA可以获得更多的益处,包括以下几点.

    • 重用性,web services或者SOA的很大优势在于对业务的快速响应,那么设计与开发web services的时候如果能够在服务的重用上做足,做强,无疑在以后业务的响应上会带来莫大的好处,使用Contract First,使用XML/XSD定义服务,你可以获得重用,而java则很难做到这一点。
    • 性能,web servers的性能一直是众多人士关注的,采用Contract Last经常会由于java的引用造成内存中存在众多的reference,假设一个java 对象引用了5个以上的其他对上,再把这些对象转换成XML,可想而知。必然加大内存的开销,(XML里面表述起来就像有5个字节点一样,那么更多呢?)采用Contract First,你很明白的所想要的服务,你通过撰写XSD来描述你的服务,你很清楚你的引用。
    • 版本,使用Contract Last的时候,快速响应的服务是会经常修改来适应新的业务要求,你发布服务通过java类来开始,那么新的服务在java中意味着新的接口以及新的实现,那么怎么办?废弃原来的?也许原来的还在使用,那么你得保持两个服务。采用Contract Last,由于Contract的松散耦合,它允许你存在两套服务的同时,并且只有一个实现。

    这样造成了Contract Last的问题:自底向上生成经常会得到无法重用的类型定义以及多个定义为表示语义等效信息的类型。相比而言: XML 模式规范定义范围比 Java 更广的用于描述消息结构的构造。其中包括各个选择、限制的派生、Annotation 及其他。因此,与采用其他方式相比,使用 WSDL 和 XSD 定义接口并生成框架 Java 代码的方式更好

     

    比较二者,其实最大优劣的莫过于服务的变化性,Contract Last会让服务难于修改和快速变更,难于重用,用java开始设计,那么你最好保证你的服务是永久不改变的,或者事先你得反复审查你的服务接口。使其尽量保持不变性。

     

         2.例子引入

     

    Spring Web Servers提供了丰富的例子可供学习,下载其完整包可以在samples下面找到。这里也引用其中一个Echo sample介绍其开发过程。该例子实现web services client调用服务传送名字到服务器。然后服务提供者接收该名字,并附加信息返回给调用者。

    • 一切开始于XML.

    定义你的XML

    Xml代码  收藏代码
    1. <sch:EchoRequest xmlns:sch="http://www.upyaya.org/echo/schemas">  
    2.   <sch:Echo>  
    3.     <sch:Name>string</sch:Name>  
    4.   </sch:Echo>  
    5. </sch:EchoRequest>  

    使用XML Buddy转换成XSD.

     

    Xml代码  收藏代码
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <!-- Generated from echo.xml by XMLBuddy -->  
    3. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"  
    4.            targetNamespace="http://www.upyaya.org/echo/schemas"  
    5.            xmlns="http://www.upyaya.org/echo/schemas"  
    6.            elementFormDefault="qualified">  
    7.     <xs:element name="Echo">  
    8.         <xs:complexType>  
    9.             <xs:sequence>  
    10.                 <xs:element ref="Name"/>  
    11.             </xs:sequence>  
    12.         </xs:complexType>  
    13.     </xs:element>  
    14.     <xs:element name="EchoRequest">  
    15.         <xs:complexType>  
    16.             <xs:sequence>  
    17.                 <xs:element ref="Echo"/>  
    18.             </xs:sequence>  
    19.         </xs:complexType>  
    20.     </xs:element>  
    21.     <xs:element name="Name" type="xs:string"/>  
    22. </xs:schema>  

     

    当你熟悉了XSD的写法的时候,完全不用前面的XML开路。编辑一下XSD让其好看点,并未这个定义加上web services响应代码。结果如下:

    Xml代码  收藏代码
    1. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"  
    2.     xmlns:tns="http://www.upyaya.org/echo/schemas" elementFormDefault="qualified"  
    3.     targetNamespace="http://www.upyaya.org/echo/schemas">  
    4.     <xs:element name="EchoRequest">  
    5.         <xs:complexType>  
    6.             <xs:all>  
    7.                 <xs:element name="Echo" type="tns:EchoType"/>  
    8.             </xs:all>  
    9.         </xs:complexType>  
    10.     </xs:element>  
    11.     <xs:element name="EchoResponse">  
    12.         <xs:complexType>  
    13.             <xs:all>  
    14.                 <xs:element name="EchoResponse" type="tns:ReturnType"/>  
    15.             </xs:all>  
    16.         </xs:complexType>  
    17.     </xs:element>  
    18.     <xs:complexType name="EchoType">  
    19.         <xs:sequence>  
    20.             <xs:element name="Name" type="xs:string"/>  
    21.         </xs:sequence>  
    22.     </xs:complexType>  
    23.   
    24.     <xs:complexType name="ReturnType">  
    25.         <xs:sequence>  
    26.             <xs:element name="Message" type="xs:string"/>  
    27.         </xs:sequence>  
    28.     </xs:complexType>  
    29. </xs:schema>  

    至此你的服务就完成。EchoRequest用于发送请求,EchoResponse用于响应。

     

    • 后台服务

    定义后台服务接口。代码

    Java代码  收藏代码
    1. package org.upyaya.sample.echo.service;  
    2.   
    3. public interface EchoService {  
    4.     public String echo(String name);  
    5. }  

     

     

    实现如下:为了简单,略去后台的调用。

    Java代码  收藏代码
    1. package org.upyaya.sample.echo.service;  
    2.   
    3. import java.text.SimpleDateFormat;  
    4. import java.util.Calendar;  
    5.   
    6. import org.upyaya.sample.echo.domain.dao.EchoDao;  
    7.   
    8. public class EchoServiceImpl implements EchoService {  
    9.     //private EchoDao echoDao;  
    10.   
    11.     public String echo(String name) {  
    12.         if (name == null || name.trim().length() == 0) {  
    13.             return "echo back: -please provide a name-";  
    14.         }  
    15.         SimpleDateFormat dtfmt = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss a");  
    16.         return "echo back: name " + name + " received on "  
    17.                 + dtfmt.format(Calendar.getInstance().getTime());  
    18.     }  
    19.   
    20. /*  public EchoDao getEchoDao() { 
    21.         return echoDao; 
    22.     } 
    23.  
    24.     public void setEchoDao(EchoDao echoDao) { 
    25.         this.echoDao = echoDao; 
    26.     }*/  
    27. }  

     

    • ApplicationContext组合的纽带

    Spring的ApplicationContext就是Spring的魔法棒,指挥着组件间的合作。

     

    1) 几个类的介绍:

    • org.springframework.ws.transport.http.MessageDispatcherServlet,类比Spring MVC的DispacherServlet,它把其中的三个关键类WebServiceMessageReceiverHandlerAdapter,MessageDispatcher,WsdlDefinitionHandlerAdapter,分开来处理。该类放置在web.xml里面。这些类的功能可能的话在下篇的源码分析中介绍。
    • org.springframework.ws.soap.server.SoapMessageDispatcher,类比于Spring MVC的DispatherServlet.这里dispather的是传入的web services 消息到endpoint. 此类是WebServiceMessageReceiver的子类,MessageDispatcherServlet会在加载的时候去初始化它。他的功能是注册EndpointMapping,并使传入的消息mapping到特定的endpoint对象 此类通常定义在spring的application context里面。
    • Endpoint--MessageEndpoint,MethodEndpoint,PayloadEndpoint,类比于Spring MVC的Controller.功能是把传入的消息传递到服务器。然后转换成响应(如果有响应)。此类就像Spring MVC里面一样,需要自己实现。
    • EndpointMapping,类似于Spirng MVC的controller mapping,把消息mapping到特定的处理类(Endpoint的子类或者实现类)。常用的两个是:PayloadRootQNameEndpointMapping,SoapActionEndpointMapping
    • org.springframework.ws.wsdl.wsdl11.DynamicWsdl11Definition,此类为动态发布wsdl的定义类。

    2)Endpoint的实现,

     

    endpoint是把传入消息处理后转为响应的类,通过继承AbstractMarshallingPayloadEndpoint重写invokeInternal方法来实现,invokeInternal

    是这样的:protected Object invokeInternal(Object request) throws Exception,传入的是请求消息。返回的是响应消息。代码如下

    Java代码  收藏代码
    1. package org.upyaya.sample.echo.endpoint;  
    2.   
    3. import org.springframework.ws.server.endpoint.AbstractMarshallingPayloadEndpoint;  
    4. import org.upyaya.sample.echo.domain.schema.EchoRequest;  
    5. import org.upyaya.sample.echo.domain.schema.EchoResponse;  
    6. import org.upyaya.sample.echo.domain.schema.EchoType;  
    7. import org.upyaya.sample.echo.domain.schema.ReturnType;  
    8. import org.upyaya.sample.echo.service.EchoService;  
    9.   
    10. public class EchoMasharlingEndpoint extends AbstractMarshallingPayloadEndpoint {  
    11.     private EchoService echoService;  
    12.   
    13.     public EchoService getEchoService() {  
    14.         return echoService;  
    15.     }  
    16.   
    17.     public void setEchoService(EchoService echoService) {  
    18.         this.echoService = echoService;  
    19.     }  
    20.       
    21.     protected Object invokeInternal(Object request) throws Exception {  
    22.         EchoRequest echoRequest = (EchoRequest)request;  
    23.         EchoType echoType = echoRequest.getEcho();  
    24.         System.out.println("-------here----------");  
    25.         System.out.println(echoType.getName());  
    26.         String returnMessage = echoService.echo(echoType.getName());  
    27.           
    28.         EchoResponse response = new EchoResponse();  
    29.         ReturnType returnType = new ReturnType();  
    30.         returnType.setMessage(returnMessage);  
    31.         response.setEchoResponse(returnType);  
    32.         return response;  
    33.     }  
    34. }  

     

    注:本例采用的是Marshalling方式。因此需要使用JAX-B的API来对消息进行转换,JAX-B的eclipse插件可以轻松的实现XSD->JAVA.插件地址:https://jaxb-workshop.dev.java.net/

     

         3.总结和附件

    Spring Web services的优缺点。

    1)优点方面

    • Spring这个流行框架的支持。
    • 继承了Spring框架的一贯优点,采用依赖注入方式。
    • 集成较多的处理工具。XML API. 消息处理比较完善。
    • 安全性的支持比较好。主要支持Acegi,也支持SimpleTextPassword。不过目前的SAAJ框架存在bug,简单认证还有一定的问题。
    • 良好的API文档说明和例子学习。

    2)缺点方面

    • 客户端的支持比较简单,需要加强。
    • 由于以前没咋用其他框架,缺点方面需要大家指出。

    暂时做个总结吧

     

     

  • 相关阅读:
    欧拉函数的递推形式
    Hadoop运行startdfs.sh报ERROR: Attempting to operate on hdfs as root错误的解决方法
    Solr+ZooKeeper运行报错
    Linux的查找命令
    Solr报警告java.nio.file.NoSuchFileException: /solr/xxx_shard_replica_xx/../../../../contrib/extraction/lib
    利用脚本批量操作Tomcat集群
    大数据组件的日志的时区问题
    利用脚本批量操作ZooKeeper集群
    Solr报错org.apache.solr.common.SolrException: undefined field text
    Solr/SolrCloud报警告Your ZK connection string ( hosts) is different from the dynamic ensemble config ( hosts)的解决方法
  • 原文地址:https://www.cnblogs.com/chenying99/p/2392717.html
Copyright © 2011-2022 走看看