Publishing Webservices with Java 6 and Spring-WS

Lately I had to add a webservice interface to our XML importer component and so I was taking a closer look on some webservice frameworks. While reading several feature lists and technical documentations I came across this description of Spring-WS. Chapter 17.5.6 deals with using Spring-WS together with the Java 6 JDK webservice features, published in a build-in HTTP-Server. Because the attached code samples looked really simple, I decided to give it a shot.

First of all I added the needed dependencies to Maven's pom.xml: There were actually only two of them: "spring-ws" and "spring-web". I wondered why I don't have to add the dependency of an Java / XML binding framework, which is usually needed to marshall Java objects into XML and vice versa, but then I found out that JAXB (Java Architecture for XML Binding) is already included in the Java 6 JDK.

Second of all I had to define the webservice's endpoint where all incoming requests are processed. I created a class with one simple web method and added some annotations to mark it as a webservice. The attribute "serviceName" is used to define the name - the service will later be available under this name.

All methods of this class should be annotated with "@Webmethod". If a method must not be accessible as a webmethod (like the setter in this example) is has to have its "exclude" attribute set to "true". All other methods will be published as web methods for the service.

public class ImportServiceEndpoint { private XmlImporter xmlImporter; 
 public String import(String xml) {
 // process import and return import status
  public void setXmlImporter(IXmlImporter xmlImporter) {
    this.xmlImporter = xmlImporter;

Finally I created a spring configuration to publish the service and register the endpoint. The "SimpleJaxWsServiceExporter" takes all classes that are annotated as "@WebService" and tries to publish them on startup. It even uses a build-in http server which will be started after the spring-context created the bean. Basically the "SimpleJaxWsServiceExporter" takes care of everything.

<bean class="org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter" >
  <property name="baseAddress" value="http://localhost:8081/" />
<bean id="importServiceEndpoint" class="">
  <property name="xmlImporter" ref="xmlImporter" />

When the server is up and running you can simply call the automatically generated WSDL. It will be available at baseUrl /serviceName ? wsdl - in this example this would be http://localhost:8081/importService?wsdl.

To come to a conclusion I was pretty amazed how easy it can be to publish a webservice without creating any XML schema files or java class stubs. It might not be the right approach if you need high flexibility (for example on generating the WSDL) but in my opinion it is perfect if you just want to publish a simple webservice using Java 6.

See also:

Sven Hoffmann

Sven Hoffmann

Thu Aug 12 06:11:00 CEST 2010 • Thu Aug 12 06:11:00 CEST 2010

Eclipse, Java, Web Technology