Wednesday, January 20, 2016

Generate a Spring Boot SOAP Webservice from a provided WSDL

I was faced with a task to generate a SOAP Web Service in Spring Boot. The system we wanted to integrate with provided us with a WSDL file to be hosted in our end, so that, that system can call the services in our system.


Producing a SOAP Web service in Spring Boot is pretty straight forward if you follow the instructions on their website: https://spring.io/guides/gs/producing-web-service/.

However, the problem I was facing is that , instead of an XSD, I had a WSDL.

In order to generate the API's and host the WSDL, there were slight changes from the usual process as mentioned in the above link.

Here are those changes.

1) In the Maven POM file

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
        <version>1.6</version>
        <executions>    
               <execution>      
<id>xjc</id>      
<goals>
          <goal>xjc</goal>
      </goals>
      </execution>
</executions>
    <configuration>  
 <packageName>com.abc.webservice.api</packageName>
   <wsdl>true</wsdl>
     <xmlschema>false</xmlschema>
    <schemaFiles>service.wsdl</schemaFiles>
    <schemaDirectory>${project.basedir}/src/main/resources/</schemaDirectory>
    <outputDirectory>${project.basedir}/src/main/java</outputDirectory>
  <clearOutputDir>false</clearOutputDir>
   </configuration>
</plugin>


2) The service.wsdl has to be kept in the /src/main/resources/ folder.

3)Note changes in the WebServiceConfig
@Configuration @EnableWs
public class WebServiceConfig extends WsConfigurerAdapter {
 @Bean
 public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
  MessageDispatcherServlet servlet = new MessageDispatcherServlet();
  servlet.setApplicationContext(applicationContext);
  servlet.setTransformWsdlLocations(true);
  return new ServletRegistrationBean(servlet, "/ws/*");
 }

 @Bean(name = "countries")
 public Wsdl11Definition wsdlDefinition() {
  SimpleWsdl11Definition wsdl11Definition = new SimpleWsdl11Definition(); 
wsdl11Definition.setWsdl(new ClassPathResource("service.wsdl"));
return wsdl11Definition; }