Let’s create a simple hello world with and Spring Boot + Apache CXF (JAX-WS) project. Is not so common to find and example on the web, so I’m going to provide it.
Firstly, we need to declare some dependencies on our pom.xml
:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.tassioauad</groupId> | |
<artifactId>springbootsoap</artifactId> | |
<version>0.1</version> | |
<packaging>jar</packaging> | |
<name>springbootsoap</name> | |
<description>Simples Spring Boot + Apache CXF (JAX-WS) project </description> | |
<parent> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-parent</artifactId> | |
<version>1.5.9.RELEASE</version> | |
<relativePath/> | |
</parent> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | |
<java.version>1.8</java.version> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>org.apache.cxf</groupId> | |
<artifactId>cxf-spring-boot-starter-jaxws</artifactId> | |
<version>3.1.12</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-test</artifactId> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-maven-plugin</artifactId> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
I’d like to highlight the dependency with artefactid cxf-spring-boot-starter-jaxws
, which provides the Apache CXF for Spring Boot. As a pattern in Spring projects, we will create and WebServiceConfig.java
and set the Endpoint and publish it as we usually do in JAX-WS projects.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.cxf.Bus; | |
import org.apache.cxf.jaxws.EndpointImpl; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import javax.xml.ws.Endpoint; | |
@Configuration | |
public class WebServiceConfig { | |
@Autowired | |
private Bus bus; | |
@Bean | |
public Endpoint endpoint() { | |
EndpointImpl endpoint = new EndpointImpl(bus, new HelloPortImpl()); | |
endpoint.publish("/Hello"); | |
return endpoint; | |
} | |
} |
As we can see, we will always do commons configurations of JAX-WS but following the Spring Boot patterns. So, if you’ve worked with Spring Boot and with JAX-WS, it will be simple.
____________________________________________________
OBS:
I can imagine that somebody would ask yourself about the org.apache.cxf.Bus
. So, I’d like to break the tutorial just to bring a quote of what it is according to the Apache CXF documentation:
The bus, being CXF’s backbone, is a provider of shared resources to the CXF runtime. Examples for such shared resources include WSDL managers and binding factory managers. The bus can easily be extended to include your own custom resources or services, or you can replace default resources like the HTTP destination factory (based on Jetty) with your own (possibly based on another web container such as Apache Tomcat).
This extensibility is made possible by dependency injection; the default bus implementation is based on Spring Framework, which wires the runtime components together for you.

____________________________________________________
No other configuration is necessary to our hello world, so, now we only need to create our class to receive the SOAP request.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.jws.WebMethod; | |
import javax.jws.WebParam; | |
import javax.jws.WebResult; | |
import javax.jws.WebService; | |
import javax.xml.ws.RequestWrapper; | |
import javax.xml.ws.ResponseWrapper; | |
import java.util.logging.Logger; | |
@WebService(serviceName = "HelloService", portName = "HelloPort", targetNamespace = "https://tassioauad.com/") | |
public class HelloPort { | |
private static final Logger LOG = Logger.getLogger(HelloPort.class.getName()); | |
@WebResult(name = "return", targetNamespace = "") | |
@RequestWrapper(localName = "sayHello", targetNamespace = "https://tassioauad.com/", className = "tassioauad.com.SayHello") | |
@WebMethod(action = "urn:SayHello") | |
@ResponseWrapper(localName = "sayHelloResponse", targetNamespace = "https://tassioauad.com/", className = "tassioauad.com.SayHelloResponse") | |
public java.lang.String sayHello(@WebParam(name = "myname", targetNamespace = "") String myname) { | |
try { | |
return "Hello, " + myname + "!"; | |
} catch (java.lang.Exception ex) { | |
ex.printStackTrace(); | |
throw new RuntimeException(ex); | |
} | |
} | |
} |
I’ve used the same annotation the we use in any JAX-WS project. So, compile and be happy!
Endpoint: http://localhost:<port>/services/Hello?wsdl
____________________________________________________
OBS:
We cannot use @Autowired to inject dependencies inside the HelloPort because it’s not managed as bean for Spring. I will post some workarounds to help with it!
