Contract-first webservice with Spring Boot and JAX-WS RI

This tutorial shows you how to create a contract-first SOAP web service with JAX-WS RI and Spring Boot. You will define the service contract first and then implement it.

Get the sources for this tutorial

What to do

At first, set up your pom.xml file. Besides stuff for Spring Boot, you’ll include jaxws-rt (the runtime) and jaxws-spring (a helper library for integrating jaxws-rt with Spring). Exclude the Spring dependencies from jaxws-spring to avoid conflicts. You will also need the jaxws-maven-plugin to help you generate the service classes.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=""
<!– JAX-WS –>
<!– Spring JAX-WS Integration –>

view raw
hosted with ❤ by GitHub

Next, you want to create the service contract by defining the Web Service (WSDL) and Type Definitions (XSD). If you don’t know how to read WSDL documents, here is an excellent introduction.

<definitions xmlns:soap="" xmlns:tns=""
xmlns:xsd="" xmlns=""
targetNamespace="" name="GreetingService">
<xsd:import namespace="" schemaLocation="hello.xsd"/>
<message name="greeting">
<part name="parameters" element="tns:greeting"/>
<message name="greetingResponse">
<part name="parameters" element="tns:greetingResponse"/>
<portType name="GreetingService">
<operation name="greeting">
<input message="tns:greeting"/>
<output message="tns:greetingResponse"/>
<binding name="GreetingServicePortBinding" type="tns:GreetingService">
<soap:binding transport="" style="document"/>
<operation name="greeting">
<soap:operation soapAction=""/>
<soap:body use="literal"/>
<soap:body use="literal"/>
<service name="GreetingService">
<port name="GreetingServicePort" binding="tns:GreetingServicePortBinding">
<soap:address location="http://localhost:8080/hello"/>

view raw
hosted with ❤ by GitHub

<xs:schema xmlns:tns="" xmlns:xs="" version="1.0"
<xs:element name="greeting" type="tns:greeting"/>
<xs:element name="greetingResponse" type="tns:greetingResponse"/>
<xs:complexType name="greeting" />
<xs:complexType name="greetingResponse">
<xs:element name="message" type="xs:string" minOccurs="0"/>

view raw
hosted with ❤ by GitHub

Run mvn clean compile to generate the Service API. You should see in the target/generated-sources/wsimport folder the service interface and type classes.

The Application class is the main entry to your application. As you’ll see, you will import an xml config file (jaxwsconfig.xml) which contains bean wiring for JAX-WS RI. Also, you need to register WSSpringServlet to receive incoming requests.

package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ImportResource;
@ImportResource(locations = "jaxwsconfig.xml")
public class Application {
public static void main(String[] args) throws Exception {, args);
public ServletRegistrationBean servletRegistrationBean() {
return new ServletRegistrationBean(new WSSpringServlet(), "/hello");

view raw
hosted with ❤ by GitHub

The GreetingServiceImpl class implements the web service. It references the service endpoint interface which tells the JAX-WS runtime to use it as an explicit interface (read more about SEIs here). Through the @Component annotation you can make sure it gets picked up, added to the Spring context and will be given a name. You will reference the service by the name later to let it handle the web service requests.

package com.example.greetingservice;
import org.springframework.stereotype.Component;
import javax.jws.WebService;
@Component(value = "greetingService")
@WebService(endpointInterface = "com.example.greetingservice.GreetingService")
public class GreetingServiceImpl implements GreetingService {
public String greeting() {
return "Hello World!";

Lastly, you have to add an xml file for wiring the service on the endpoint:

Build and run it

Build it: mvn clean package
Run it: java -jar helloJaxws-0.0.1-SNAPSHOT.jar

Now open http:/localhost:8080/hello?wsdl to see the generated wsdl file. Your webservice has been deployed, and you could now create a client for it.

What to do next?

Here are some hints what topics you could explore next.

  • Test your webservice with SoapUI
  • See what other alternatives to RI are available

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s