Skip to content

Spring Boot With Web Services#

Integrate With Web Services#

  • In some projects, sometime we will meet some user stories that need us to make a connection to the Web Services from our spring boot service by giving a file .wsdl. So This post will guild you step by step to archive it easily.

Prepare The Environment#

  • To make an example we need an online Web Service, so we can go to free SOAP service urls and choose one to make an example.
  • In this example I will choose country list SOAP service because it is the one that is working well.
  • So you need go to country list SOAP service by your browser. Then Right Click and choose Save As. After that you save the file with type .wsdl into any package in src/main/resources of your spring boot project as the images below:

 #zoom

 #zoom

Dependencies#

  • We need to add some dependencies for SOAP in pom.xml file. In which, we need to note about the declaration of generatePackage which is the package that contains all generated java classes when we run command mvn compile and the schemaDirectory which is the path to our countries.wsdl file of us.
pom.xml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
    ...

    <dependency>
            <groupId>org.springframework.ws</groupId>
            <artifactId>spring-ws-core</artifactId>
            <version>3.1.4</version>
    </dependency>

    <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
            <version>5.3.23</version>
    </dependency>

    ...
pom.xml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
...

    <build>
        <plugins>
            <!-- pom.xml plugin -->
            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.14.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <schemaLanguage>WSDL</schemaLanguage>
                    <generateDirectory>${project.basedir}/src/main/java</generateDirectory>
                    <!-- the package that will contain our generated java classes -->
                    <generatePackage>com.springboot.project.web.services.model.gen</generatePackage>
                    <!-- the path where we put the wsdl file -->
                    <schemaDirectory>${project.basedir}/src/main/resources</schemaDirectory>
                    <schemaIncludes>
                            <include>countries.wsdl</include>
                    </schemaIncludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

Generate Java Classes From WSDL File#

  • So let's open the terminal/command line then run mvn compile, then Maven will read the file countries.wsdl to generate java classes and put them into the package that we have already defined in the pom.xml of Step 2.
  • If you note that, java classes have been configured with many annotation of XML, So these class will be formatted as xml type when we transfer them to SOAP service.

 #zoom

Creating Adapter#

  • So in this step we will define a soap service adapter named CountryClient which are extended from WebServiceGatewaySupport.
  • You need to note that generated java classes as CountryName.java and CountryNameResponse.java are formatted with xml.
CountryClient.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
package com.springboot.project.web.services.api;  

import com.springboot.project.web.services.model.gen.CountryName;  
import com.springboot.project.web.services.model.gen.CountryNameResponse;  
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;  

public class CountryClient extends WebServiceGatewaySupport {  

    public CountryNameResponse getCountry(String countryISO) {  
        CountryName request = new CountryName();  
        request.setSCountryISOCode(countryISO);  
        return (CountryNameResponse) getWebServiceTemplate()  
                .marshalSendAndReceive(request);  
    }  

}

Configuration#

  • create a CountryClientConfig that we will config the soap request with soapURl and contextPath. In which, contextPath is the package that contains all our generated java classes and soapURl is the WebServiceUrl that we call to.
CountryClientConfig.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.springboot.project.web.services.config;

import com.springboot.project.web.services.api.CountryClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;

@Configuration
public class CountryClientConfig {
    /**
     `contextPath` is the `package` that contains all our `generated java classes`
     */
    @Value("${country.client.soap.package}")
    private String contextPath;

    /**
     `soapURl` is the `WebServiceUrl` that we call to
     */
    @Value("${country.client.soap.url}")
    private String soapURl ;


    @Bean
    public Jaxb2Marshaller marshaller() {
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setContextPath(contextPath);
        return marshaller;
    }

    @Bean
    public CountryClient countryClient(Jaxb2Marshaller marshaller) {
        CountryClient client = new CountryClient();
        client.setDefaultUri(soapURl);
        client.setMarshaller(marshaller);
        client.setUnmarshaller(marshaller);
        return client;
    }
}
application.yml
1
2
3
4
5
country:  
  client:  
    soap:  
      package: com.springboot.project.web.services.model.gen  
      url: http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso?WSDL

Create Service#

  • Let's create a simple Service which use CountryClientAdapter.java as below.
CountryCodeHandler.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
package com.springboot.project.web.services.service;

import com.springboot.project.web.services.api.CountryClient;
import com.springboot.project.web.services.model.gen.CountryNameResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CountryCodeHandler {

    @Autowired
    private CountryClient countryClient;

    public CountryNameResponse getCountryName(String countryISO) {
        return countryClient.getCountry(countryISO);
    }

}

Create Controller#

  • Let's create a simple Service which use CountryCodeHandler.java as below.
WebServicesController.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.springboot.project.web.services.controller;

import com.springboot.project.web.services.model.gen.CountryNameResponse;
import com.springboot.project.web.services.service.CountryCodeHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class WebServicesController {

    @Autowired
    private CountryCodeHandler countryCodeHandler;

    @GetMapping(path = "/v1/soap/country/{countryISO}", produces = MediaType.APPLICATION_XML_VALUE)
    public ResponseEntity<CountryNameResponse> getCountryNameByISO(@PathVariable("countryISO") String countryISO) {
        return ResponseEntity.ok(this.countryCodeHandler.getCountryName(countryISO));
    }

}

Testing#

  • Finally, let's start our spring boot application project and call the exported api by postman to test.
  • The result we be showed as below, please not that the we use 3 ISO country code for test Ex: VNM, IDN

 #zoom

See Also#

References#