What’s new in Spring Web Services 2.0

Spring Web Services is a framework for building Web Service Endpoints and Clients in a easy way. This post describes the new features and a quick walkthrough for constructing a Web Service using Spring Web Services.

Annotation-Driven


With Spring WS, Web Services can only be made using the “Contract First” approach, which is to say that first a (XSD) schema or WSDL must be set up, before you begin the code. The biggest difference with the previous versions and Spring WS 2.0 is the ease of configuration. In the Spring WS 1.x versions a typical context configuration looked like the following snippet:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean id="holidayEndpoint" class="nl.willem.ws.HolidayEndpoint" />

<bean class="org.springframework.ws.server.endpoint.adapter.GenericMarshallingMethodEndpointAdapter">
<constructor-arg ref="marshaller" />
</bean>

<bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>nl.willem.ws.generated.HolidayRequest</value>
</list>
</property>
</bean>

<bean class="org.springframework.ws.server.endpoint.mapping.PayloadRootAnnotationMethodEndpointMapping" />

<bean id="holiday" class="org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition">
<property name="schema" ref="schema" />
<property name="portTypeName" value="Holiday" />
<property name="locationUri" value="/holidayService/" />
</bean>

<bean id="schema" class="org.springframework.xml.xsd.SimpleXsdSchema">
<property name="xsd" value="classpath:hr.xsd" />
</bean>

</beans>

The same effect can now be achieved using precooked namespaces included in Spring WS 2.0:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:sws="http://www.springframework.org/schema/web-services"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xsi:schemaLocation="http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/web-services http://www.springframework.org/schema/web-services/web-services-2.0.xsd
http://www.springframework.org/schema/context >

<context:component-scan base-package="nl.willem.ws" />

<sws:annotation-driven />

<sws:dynamic-wsdl id="holiday" portTypeName="Holiday"
locationUri="/holidayService/">
<sws:xsd location="classpath:hr.xsd" />
</sws:dynamic-wsdl>

</beans>

The <sws:annotation-driven /> element scans for all the beans with @Endpoint annotation and mapped the methods annotated with @PayloadRoot. So far no difference with the previous version of Spring-WS. Still working the web this way yet and it is necessary to the argument of the method @RequestPayload fragment. This results in the following Endpoint code:
@Endpoint
public class HolidayEndpoint
{

@PayloadRoot(localPart = "HolidayRequest", namespace = "http://42.nl/hr/schemas")
public void makeHolidayRequest(@RequestPayload HolidayRequest holidayRequest)
{
System.out.println("Just ignore it");
}

}

The selected XML object mapping technology (in this case, JAXB) is also detected automatically on the classpath and does not need to be explicitly configured.

Testing


Another interesting feature in Spring WS, is the way to do component testing. This way of testing is similar to the new way of testing of MVC controllers as described in this post.

Final words


This concludes the brief overview of developing Endpoints in Spring Web Services 2.0, the logical evolutionary step made in the spirit of Spring MVC and 3.0. More information can be found by following these links:

No comments:

Post a Comment