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 Clickand chooseSave As. After that you save the file with type.wsdlinto any package insrc/main/resourcesof your spring boot project as the images below:
Dependencies#
- We need to add some dependencies for SOAP in
pom.xmlfile. In which, we need to note about the declaration ofgeneratePackagewhich is the package that contains all generated java classes when we run commandmvn compileand theschemaDirectorywhich is the path to ourcountries.wsdlfile of us.
| pom.xml | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
| 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 | |
Generate Java Classes From WSDL File#
- So let's open the
terminal/command linethen runmvn compile, then Maven will read the filecountries.wsdlto generate java classes and put them into thepackagethat 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.
Creating Adapter#
- So in this step we will define a soap service adapter named
CountryClientwhich are extended fromWebServiceGatewaySupport. - You need to note that generated java classes as
CountryName.javaandCountryNameResponse.javaare formatted with xml.
| CountryClient.java | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
Configuration#
- create a
CountryClientConfigthat we will config the soap request withsoapURlandcontextPath. In which,contextPathis the package that contains all our generated java classes andsoapURlis 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 | |
| application.yml | |
|---|---|
1 2 3 4 5 | |
Create Service#
- Let's create a simple Service which use
CountryClientAdapter.javaas below.
| CountryCodeHandler.java | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
Create Controller#
- Let's create a simple Service which use
CountryCodeHandler.javaas 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 | |
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



