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,只需要执行以下的命令:
- mvn archetype:create-DarchetypeGroupId=org.springframework.ws-DarchetypeArtifactId=spring-ws-archetype -DarchetypeVersion=2.0.0.RELEASE-DgroupId=com.shekhar.usermanagement -DartifactId=profileService
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定义如下:
- <?xmlversion="1.0" encoding="UTF-8"?>
- <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
- elementFormDefault="qualified"targetNamespace="http://shekhar.com/usermanagement/schemas"
- xmlns:schemas="http://shekhar.com/usermanagement/schemas">
- <xs:element name="UserProfileCreateRequest">
- <xs:complexType>
- <xs:sequence>
- <xs:elementname="UserProfile" type="schemas:UserProfile" />
- </xs:sequence>
- </xs:complexType>
- </xs:element>
- <xs:complexTypename="UserProfile">
- <xs:sequence>
- <xs:elementname="firstName" type="xs:string" />
- <xs:elementname="lastName" type="xs:string" />
- <xs:elementname="age" type="xs:integer" />
- </xs:sequence>
- </xs:complexType>
- <xs:elementname="UserProfileCreateResponse">
- <xs:complexType>
- <xs:sequence>
- <xs:elementname="message" type="xs:string" />
- </xs:sequence>
- </xs:complexType>
- </xs:element>
- </xs:schema>
使用sws:dynamic-wsdl来自动生成WSDL
Spring WS让我们可以选择从XSD动态生成WSDL。在2.0版之前,我们必须在context文件中定义一个bean来动态生成WSDL。在SpringWS 2.0中,我们可以通过使用<sws:dynamic-wsdl>标签来动态生成WSDL。
- <sws:dynamic-wsdlid="profile" portTypeName="UserManagement"locationUri="/profileService" targetNamespace="http://shekhar.com/usermanagement/schemas">
- <sws:xsdlocation="/WEB-INF/userManagement.xsd"/>
- </sws:dynamic-wsdl>
升级了的、更加灵活的@Endpoint模型
现在我们已经生成了约定(也就是WSDL),但是还没有编写会在服务器端被调用的代码。在Spring WS中,我们要编写endpoint来处理输入的XML消息。一个endpoint就是一个使用了@Endpoint注解的类。在endpoint类中,我们要创建一个或多个方法来处理输入请求的XML消息。处理方法可以接受很多类型的参数和返回值,例如JAXB2,dom4j,XPATH,SAX,STAX等等。也可以将这些类型组合使用(例如,可以使用DOM对象的参数,但是返回JAXB2的响应对象)。
下面就是Spring WS引入的重要的特性之一。处理方法可以使用@PayloadRoot或@SoapAction注解来指定哪种类型的消息是该方法能够处理的。可以从SpringSource文档中获得完整的支持的参数和返回类型的列表。
下面是一个endpoint的例子:
- importorg.springframework.beans.factory.annotation.Autowired;
- import org.springframework.ws.server.endpoint.annotation.Endpoint;
- import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
- import org.springframework.ws.server.endpoint.annotation.RequestPayload;
- import org.springframework.ws.server.endpoint.annotation.ResponsePayload;
- import com.shekhar.usermanagement.profile.UserProfileCreateRequest;
- import com.shekhar.usermanagement.profile.UserProfileCreateResponse;
- @Endpoint
- public class UserProfileEndpoint {
- private UserService service;
- @Autowired
- public UserProfileEndpoint(UserServiceservice) {
- this.service = service;
- }
- @PayloadRoot(localPart ="UserProfileCreateRequest", namespace ="http://shekhar.com/usermanagement/schemas")
- @ResponsePayload
- public UserProfileCreateResponsecreate(@RequestPayload UserProfileCreateRequest request) {
- String message =service.createUser(request.getUserProfile());
- UserProfileCreateResponse response= new UserProfileCreateResponse();
- response.setMessage(message);
- return response;
- }
- }
小结
从上面的简单的例子可以看出,使用Spring WS 2.0能够十分容易的创建基于SOAP的web service。升级后的@Endpoint编程模型十分灵活,而且只需要很少的配置。开发人员不必要编写冗长的配置代码,只需要在应用程序的context XML文件中添加几个XML标签就行了。