使用Spring WS 2.0开发基于SOAP的web service

经过1年的开发之后,SpringWeb service项目最近发布了一个新的主要版本Spring WS 2.0。这个版本要求使用它的工程使用的是Java 5或以上版本及Spring 3.0或以上版本。但是升级到Spring WS 2.0是十分值得的,因为Spring WS 2.0为Java开发者提供了很多好处。

    Spring WS 2.0中的新特性包括:

  • 升级后的更加灵活的@EndPoint编程模型
  • 新的集成测试框架,提供对服务器端和客户端的测试。
  • Java 5 API(泛型,可变数目参数)等

    Spring WS是Springportfolio其中一个项目,可以用来开发基于Spring的、面向文档的SOAP Web service。和其他的Java SOAP开发工具不过的是,Spring WS只支持约定优先的Web service开发风格。Spring WS关注与XML消息,并且它不是代码生成器(处理从XSD动态生成WSDL之外)。

    在过去的几周,我正在学习SpringWS 2.0,本文中我将分享我的一点经验,向大家介绍怎样使用Spring WS 2.0创建一个简单的SOAP Web service。


创建一个新的Spring WS 2.0工程

    使用Maven原型,很容易就可以创建一个新的Spring WS工程。如果你的机器上安装了Maven 2,只需要执行以下的命令:

  1. mvn archetype:create-DarchetypeGroupId=org.springframework.ws-DarchetypeArtifactId=spring-ws-archetype -DarchetypeVersion=2.0.0.RELEASE-DgroupId=com.shekhar.usermanagement -DartifactId=profileService
复制代码
    这个命令会创建一个基于Maven2的Spring WS工程,带有所有需要的配置。除了pom.xml之外,原型还会创建spring-ws-servlet.xml和web.xml文件。Web.xml文件是标准的Web应用程序布署描述符,这个文件中将所有的输入请求都映射到MessageDispatcherServlet。Spring-ws-servlet.xml文件包含所有的Spring-WS相关的bean,例如endpoints,WebServiceMessageReceivers,拦截器等等。这个文件的名字是web.xml文件中定义的servlet的名字后面加上-servlet.xml。在web.xml文件中servlet的名字是spring-ws,因此这个XML文件的名字叫做spring-ws-servlet.xml。


Spring WS 2.0中的新特性

    本节将介绍Spring WS2.0中最值得一提的新特性。


注解驱动的Endpoint模型

    第一个新特性是在spring-ws-servlet.xml文件中只需要一行配置。这一行配置(下面会显示)配置了注解驱动的Spring WS endpoint模型。因此在Spring WS 2.0中,不需要在spring-ws-context.xml文件中定义任何的bean来配置@Endpoing编程模型。

    作为例子,让我们编写一个简单的基于SOAP的web service,该webservice会创建用户信息。由于Spring WS遵从约定优先的开发风格,我们应该首先编写XML约定。我们的web service的XML schema定义如下:

  1. <?xmlversion="1.0" encoding="UTF-8"?>
  2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  3.     elementFormDefault="qualified"targetNamespace="http://shekhar.com/usermanagement/schemas"
  4.     xmlns:schemas="http://shekhar.com/usermanagement/schemas">
  5.     <xs:element name="UserProfileCreateRequest">
  6.         <xs:complexType>
  7.             <xs:sequence>
  8.                 <xs:elementname="UserProfile" type="schemas:UserProfile" />
  9.             </xs:sequence>
  10.         </xs:complexType>
  11.     </xs:element>

  12.     <xs:complexTypename="UserProfile">
  13.         <xs:sequence>
  14.             <xs:elementname="firstName" type="xs:string" />
  15.             <xs:elementname="lastName" type="xs:string" />
  16.             <xs:elementname="age" type="xs:integer" />
  17.         </xs:sequence>
  18.     </xs:complexType>

  19.     <xs:elementname="UserProfileCreateResponse">
  20.         <xs:complexType>
  21.             <xs:sequence>
  22.                 <xs:elementname="message" type="xs:string" />
  23.             </xs:sequence>
  24.         </xs:complexType>
  25.     </xs:element>
  26. </xs:schema>
复制代码

使用sws:dynamic-wsdl来自动生成WSDL

    Spring WS让我们可以选择从XSD动态生成WSDL。在2.0版之前,我们必须在context文件中定义一个bean来动态生成WSDL。在SpringWS 2.0中,我们可以通过使用<sws:dynamic-wsdl>标签来动态生成WSDL。

  1. <sws:dynamic-wsdlid="profile" portTypeName="UserManagement"locationUri="/profileService"   targetNamespace="http://shekhar.com/usermanagement/schemas">
  2. <sws:xsdlocation="/WEB-INF/userManagement.xsd"/>
  3. </sws:dynamic-wsdl>
复制代码
    我们也可以使用这个标签来发表已经存在的WSDL。如果我们将这个应用程序布署在如Jetty或Tomcat的服务器上,就可以在http://localhost:8080/profileService/profile.wsdl处看到该文件。


升级了的、更加灵活的@Endpoint模型

    现在我们已经生成了约定(也就是WSDL),但是还没有编写会在服务器端被调用的代码。在Spring WS中,我们要编写endpoint来处理输入的XML消息。一个endpoint就是一个使用了@Endpoint注解的类。在endpoint类中,我们要创建一个或多个方法来处理输入请求的XML消息。处理方法可以接受很多类型的参数和返回值,例如JAXB2,dom4j,XPATH,SAX,STAX等等。也可以将这些类型组合使用(例如,可以使用DOM对象的参数,但是返回JAXB2的响应对象)。

    下面就是Spring WS引入的重要的特性之一。处理方法可以使用@PayloadRoot或@SoapAction注解来指定哪种类型的消息是该方法能够处理的。可以从SpringSource文档中获得完整的支持的参数和返回类型的列表

下面是一个endpoint的例子:

  1. importorg.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.ws.server.endpoint.annotation.Endpoint;
  3. import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
  4. import org.springframework.ws.server.endpoint.annotation.RequestPayload;
  5. import org.springframework.ws.server.endpoint.annotation.ResponsePayload;
  6. import com.shekhar.usermanagement.profile.UserProfileCreateRequest;
  7. import com.shekhar.usermanagement.profile.UserProfileCreateResponse;
  8. @Endpoint
  9. public class UserProfileEndpoint {
  10.     private UserService service;
  11.     @Autowired
  12.     public UserProfileEndpoint(UserServiceservice) {
  13.         this.service = service;
  14.     }

  15.     @PayloadRoot(localPart ="UserProfileCreateRequest", namespace ="http://shekhar.com/usermanagement/schemas")
  16.     @ResponsePayload
  17.     public UserProfileCreateResponsecreate(@RequestPayload UserProfileCreateRequest request) {
  18.         String message =service.createUser(request.getUserProfile());
  19.         UserProfileCreateResponse response= new UserProfileCreateResponse();
  20.         response.setMessage(message);
  21.         return response;
  22.     }
  23. }
复制代码
    在上面的例子中,@PayloadRoot组件将所有的命名空间为http://shekhar.com/usermanagement/schemas并且localPart为”UserProfileCreateRequest” 的输入请求映射到create 方法。这个方法中我们使用的是JAXB2 对象作为参数和返回值。可以使用Maven 2 的插件来从XSD 生成JAXB2 对象。

小结

    从上面的简单的例子可以看出,使用Spring WS 2.0能够十分容易的创建基于SOAP的web service。升级后的@Endpoint编程模型十分灵活,而且只需要很少的配置。开发人员不必要编写冗长的配置代码,只需要在应用程序的context XML文件中添加几个XML标签就行了。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值