23

I have WSDL and schema files provided by client. I need to create Spring-boot SOAP web service with this WSDL file. I have google it and all the examples that I can find, they have auto-generate the wsdl with spring.How can I use my WSDL to generate the SOAP service?

user3496599
  • 1,207
  • 2
  • 12
  • 28
  • 1
    I suggest you check this: http://spring.io/guides/gs/producing-web-service/ It explains how to create a contract first SOAP service. – daniel.eichten Nov 18 '15 at 08:42
  • 6
    @hrrgttnchml here they used Schema file and generate the wsdl through the code. My requirement is I have WSDL and need to write a service for that WSDL without generating a new one. – user3496599 Nov 18 '15 at 08:50
  • 2
    I usually don't follow that route but I suggest you have a look at wsdl2java of the CXF framework. – daniel.eichten Nov 18 '15 at 08:54
  • Just hand compile the WSDL using the xjc tool and get on with it. – duffymo Oct 02 '16 at 00:49
  • @user3496599 Have you figured how to do so? I am having the same problem – JayC Jan 19 '17 at 17:23

5 Answers5

31

Here are the common steps to follow to use your existing wsdl with Spring-Ws and Spring-boot.

Config class

@EnableWs
@Configuration
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/*");
    }

    //http://localhost:8080/ws/services.wsdl --bean name is set to 'services'
    @Bean(name = "services")
    public Wsdl11Definition defaultWsdl11Definition() {
        SimpleWsdl11Definition wsdl11Definition = new SimpleWsdl11Definition();
        wsdl11Definition.setWsdl(new ClassPathResource("/schema/MyWsdl.wsdl")); //your wsdl location
        return wsdl11Definition;
    }
}
  1. In your pom.xml use 'jaxb2-maven-plugin' plugin to generate classes from your wsdl.
  2. In Spring-WS, you have to write endpoint yourself. No code generation for endpoints. Its easy to write. you can follow tutorial/guide on spring website.
Ketan
  • 982
  • 1
  • 8
  • 16
  • 6
    Nice, this was usefull! I see the expected url is "http://localhost:8080/ws/services.wsdl". What if one have to have an explisit url, like http://localhost:8080/ws/services/basic?wsdl? – baron5 Feb 29 '16 at 08:43
  • This was really helpful! But how can I test that my endpoints work exactly as the provided wsdl file describe? – Syngularity Apr 17 '20 at 10:35
  • Thanks this was helpful. I further found https://codenotfound.com/spring-ws-example.html helpful to implement this. – maheeka Jul 01 '20 at 05:19
9

There are a number of options for exposing a web service starting from a WSDL file and using Spring Boot. You would typically generate your Java classes from the WSDL definition. There are a number of JAXB Maven plugins that will support you in doing this.

In addition when using Spring Boot make sure you take advantage of the spring-boot-starters in order to automatically manage the different needed dependencies.

One approach is to use Spring Web Services in combination with the maven-jaxb2-plugin plugin. I've created a step by step tutorial which explains how to do this using Spring-WS starting from a WSDL file for both consumer and provider.

Another alternative is to use a framework like Apache CXF in combination with the cxf-codegen-plugin plugin. CXF also comes with it's own CXF Spring Boot starter called cxf-spring-boot-starter-jaxws. In order to get you started I've compiled an example which uses the CXF starter in combination with Spring Boot to create a web service starting from a WSDL file.

CodeNotFound
  • 1,051
  • 2
  • 22
  • 47
4

The easiest way is to simply use the cxf-spring-boot-starter incl. it's companion Maven plugin, they will take care of generating mostly everything needed from your wsdl and schema files. Here's a full example: https://github.com/codecentric/spring-samples/tree/master/cxf-boot-simple.

Using the starter in your pom.xml, you just have to place the wsdl & schema files in src/main/resources and you're mostly done. Here's a full example pom.xml:

<?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>de.codecentric.soap</groupId>
    <artifactId>cxf-boot-simple</artifactId>
    <version>2.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>cxf-boot-simple</name>
    <description>Demo project for using Spring Boot Starter CXF</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>cxf-spring-boot-starter</artifactId>
            <version>2.1.2.RELEASE</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>
            <plugin>
                <groupId>de.codecentric</groupId>
                <artifactId>cxf-spring-boot-starter-maven-plugin</artifactId>
                <version>2.0.0.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
jonashackt
  • 12,022
  • 5
  • 67
  • 124
3

You can create WebServiceConfiguration java class in your packages.

@EnableWs
@Configuration
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, "/ProjectName/*");
    }
    @Bean(name = "wsdlname")
    public DefaultWsdl11Definition defaultWsdl11Definition (XsdSchema cityRequestSchema) {
        DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
        wsdl11Definition.setRequestSuffix("ByCountry");
        wsdl11Definition.setResponseSuffix("City");
        wsdl11Definition.setPortTypeName("Hotelport");
        wsdl11Definition.setLocationUri("/ProjectName");
        wsdl11Definition.setTargetNamespace("http://spring.io/guides/gs-producing-web-service");
        wsdl11Definition.setSchema(cityRequestSchema);
        return wsdl11Definition;

    }

    @Bean
public XsdSchema cityRequestSchema() {
    return new SimpleXsdSchema(new ClassPathResource("CityRequest.xsd"));
}

After run as spring boot app...then copy paste this url in your browser. http://localhost:8080/ProjectName/wsdlname.wsdl

noted:localhost:8080 to replace with your tomcat port

0
  • First define the define XSD for Request and Response.

  • Then Configuring the Endpoint. (i.e Create a Bean class and a controller class)

  • Then Configure the Message Dispatcher Servlet to Receive the Request.

  • Then add wsdl4j dependency to the pom.xml.

  • Then add the web service config class as below.

    @Bean(name = "students")
      public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema studentsSchema) {
        DefaultWsdl11Definition definition = new DefaultWsdl11Definition();
        definition.setPortTypeName("StudentPort");
        definition.setTargetNamespace("http://in28minutes.com/students");
        definition.setLocationUri("/ws");
        definition.setSchema(studentsSchema);
        return definition;
      }
    
      @Bean
      public XsdSchema studentsSchema() {
        return new SimpleXsdSchema(new ClassPathResource("student-details.xsd"));
      }
    

Then if you start the spring boot and access the wsdl url then you can able to see the wsdl link. Please refer this blog[1] for detailed information.

[1] https://dzone.com/articles/creating-a-soap-web-service-with-spring-boot-start

TechDog
  • 3,039
  • 1
  • 24
  • 30
Senthuran
  • 1,583
  • 2
  • 15
  • 19