Exposing a JAX-WS endpoint with Spring

Published: Wednesday, 9 June 2010

Requirements

I’ve assumed we are using Spring, and that you’ve successfully generated the web services Java interface.

See notes on WSDL to Java for how to create the interface from a WSDL.

Maven

Edit maven pom.xml, and add the following dependencies:

<dependencies>
  <dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-core</artifactId>
    <version>${cxf.version}</version>
  </dependency>
  <dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http</artifactId>
    <version>${cxf.version}</version>
  </dependency>
  <dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-bindings-soap</artifactId>
    <version>${cxf.version}</version>
  </dependency>
  <dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxws</artifactId>
    <version>${cxf.version}</version>
  </dependency>
</dependencies>

web.xml

Add the CXF servlet

  <servlet>
    <servlet-name>CXFServlet</servlet-name>
    <servlet-class>
        org.apache.cxf.transport.servlet.CXFServlet
    </servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>CXFServlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
  </servlet-mapping>

Java implementation

Create a Java class that implements the port type that was generated for the WSDL.

In this example assume it is com.magicmonster.sample.cxf.EchoServiceImpl

package com.magicmonster.sample.cxf;

import javax.jws.WebParam;
import java.util.List;

public class EchoServiceImpl implements SampleEchoServicePortType {
    private String prefix = "";

    /**
     * append all the phone numbers
     * @param body input
     * @return concatentated list of phone numbers
     */
    public String sampleEchoService(@WebParam(partName = "body", name = "body", targetNamespace = "") EchoInputMessage body) {
        List<PhoneNumberType> phoneNumberTypeList = body.getPhoneNumbers();
        StringBuffer sb =new StringBuffer(prefix);
        for (PhoneNumberType phoneNumberType : phoneNumberTypeList) {
            sb.append(phoneNumberType.getDescription());
            sb.append(":");
            sb.append(phoneNumberType.getNumber());
        }
        return sb.toString();
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }
}

Spring beans

Create a new XML config for your web services and include it. e.g. This is web-services.xml:

<?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:jaxws="http://cxf.apache.org/jaxws"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
  <import resource="classpath:META-INF/cxf/cxf.xml" />
  <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
  <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

  <jaxws:endpoint id="echoService"
    implementor="com.magicmonster.sample.cxf.EchoServiceImpl"
    address="/Echo" />
</beans>

It is imported by the main Spring context:

  <import resource="web-service.xml"/>

Deploy

Deploy the webapp and then you should be able to see your webservice at http://localhost/services/