Webservice with Spring Web Services 2.0 (M3)

Some time ago I had to define a web service and write the web service implementation for a spring 3.0 project. The operations needed were quite simple.

In the past I have usually used axis 1 to offer web services. The results have not been very satisfying because the development has always been quite complex and the generated wsdl was not very clean. In a perfect developer world I would expect to write an easy configuration defining which existing services should be accessible and doing a convention overconfiguration for the mapping of the parameters. This configuration could be an xml configuration file or annotations at the java class. Something similar to the spring REST support would be nice.

Because spring was already used in the project and spring is usually simple and powerful, I decided to try spring web services. Also testing the newest spring technology is for my company OPITZ-CONSULTING, which is a spring partner, important 😉

For this example I chose:
– a method returning a version information string
– a method with a string and a date parameter returning a boolean

These really are basical needs for a web service.

After some research I found out that spring web services 2.0, which was at M3 and is now released, is the right version to be combined with spring 3.0.

First we define the xsd or the operation request and responses:

The java methods of the spring bean containing the service has to be annotated. 

This is the version I would like to look my java class like.
– The Endpoint annotation defines that the java class is a web service endpoint.
– The Namespace annotation defines the prefix t assigned to the given namespace.
– The PayloadRoot defines the name of the request part of the soap message and the namespace.
– With the XPathParam annotations are defined how the parameters are extracted from the received soap xml message. The parameter is an XPath-expression selecting the parameter.
– The ResponesPayload annotation tells spring web services to handle the response as a web service response message.

At the moment the documentation is very modest, most of the information was gained by reverse engineering the implementation. But I’m sure this will change in future.

The common spring webservice configuration looks like this:

– We use annotations to define the endpoints
– The mapping of the method parameters is done by XPath-Expressions
– The conversions of the xml datatypes to the java world is done by a conversion service. More about this later.
– The mapping of the return values is done by a default mapping handler using the conversion service. More about this later.
– The example is deployed on JBoss but by using the JBoss message factory implementation causes a loss of the return value during the web service call.

The spring configuration for our TestService:

Here is nothing special.

Now to the two custom generic implementations. I don’t know why such an implementation is not already provided with spring (or in case it is provided, I haven’t found it yet). They make the implementation and the usage much easier.

The XMLConversionServiceFactory provides a service converting java types to default xml types. These are the default spring converters plus additional converters for java.util.Date and java.util.Calendar.

 

 

The SimplePayloadMethodReturnValueHandler is a generic MethodProcessor converting the return value of the java method call to the web service call payload.