October 09, 2009 · Ralf Ebert » Archive » Java »

Calling web service APIs using Java, Spring, JAX-WS and Maven

This is a recipe for calling web service SOAP/WSDL-based APIs from a Java/Spring application that is built with Maven. As example, I’ll use the Campaign Monitor Web Service API.

Generating client classes

First step is to generate client classes from the WSDL file. This task can be left to the jaxws-maven-plugin. It suffices to copy the wsdl file to a dedicated web service definition folder and configure the plug-in to use this folder:


This generates the client classes automatically under target/jaxws/wsimport/java when the project is built. You can trigger the generation manually using:

mvn generate-sources

Customizing the client classes

Such a generation might yields errors like naming conflicts that require customization. Or you might want to customize package and class names. This is possible using a file wsdl-customization.xml in the same folder as the .wsdl:

<?xml version="1.0" encoding="utf-8"?>
        http://java.sun.com/xml/ns/jaxws http://java.sun.com/xml/ns/jaxws/wsdl_customizationschema_2_0.xsd
        http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd">

    <jaxws:package name="com.campaignmonitor.services" />

    <jaxws:bindings node="//wsdl:portType[@name='apiSoap']">
        <jaxws:class name="CampaignMonitorService" />

     <jaxws:bindings node="//xs:schema[@targetNamespace='http://api.createsend.com/api/']">
        <jaxws:bindings node="xs:complexType[@name='SubscriberUnsubscribe']">
            <jaxb:class name="SubscriberUnsubscribeType"/>
            <jaxb:package name="com.campaignmonitor.types"/>


Configuring the service as Spring Bean

The Spring framework provides a factory class JaxWsPortProxyFactoryBean to configure an instance of the generated interface CampaignMonitorService:

<bean id="campaignMonitorService" class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean">
    <property name="serviceInterface" value="com.campaignmonitor.services.CampaignMonitorService" />
    <property name="wsdlDocumentUrl" value="classpath:com/example/webservices/campaignmonitor.wsdl" />
    <property name="namespaceUri" value="http://api.createsend.com/api/" />
    <property name="serviceName" value="api" />
    <property name="endpointAddress" value="https://api.createsend.com/api/api.asmx" />

Thanks to Gunnar Morling for giving an example in his blog: Writing and testing JAX-WS clients using the Spring framework

Service wrappers

Often such APIs might be rather rudimentary and it might make sense to wrap them in a separate service. For the Campaign Monitor API I created a NewsletterService which handles subscribing to one subscriber list and can easily be extended with other operations:

public class NewsletterService {

    private static final Logger log = LoggerFactory.getLogger(NewsletterService.class);

    private CampaignMonitorService campaignMonitorService;
    private String apiKey;
    private String listId;

    public void subscribeNewsletter(String email) {
        SubscriberAddAndResubscribe request = new SubscriberAddAndResubscribe();
        Result r = campaignMonitorService.addAndResubscribe(request).getSubscriberAddAndResubscribeResult();

        if (r.getCode() != 0) {
            throw new RuntimeException(r.getCode() + " " + r.getMessage());

    public void setApiKey(String apiKey) {
        this.apiKey = apiKey;

    public void setListId(String listId) {
        this.listId = listId;


This is configured as a Spring bean and is used by other services that need to talk to the Campaign Monitor services:

<bean id="newsletterService" class="de.ralfebert.core.company.service.NewsletterService">
    <property name="apiKey" value="${campaignMonitor.apiKey}"/>
    <property name="listId" value="${campaignMonitor.listId}"/>
iOS training Next iOS training: 25. Februar - 01. März 2019, Stuttgart
Contact About me · Contact