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 chooseSave As
. After that you save the file with type.wsdl
into any package insrc/main/resources
of your spring boot project as the images below:
Dependencies#
- We need to add some dependencies for SOAP in
pom.xml
file. In which, we need to note about the declaration ofgeneratePackage
which is the package that contains all generated java classes when we run commandmvn compile
and theschemaDirectory
which is the path to ourcountries.wsdl
file 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 line
then runmvn compile
, then Maven will read the filecountries.wsdl
to generate java classes and put them into thepackage
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.
Creating Adapter#
- So in this step we will define a soap service adapter named
CountryClient
which are extended fromWebServiceGatewaySupport
. - You need to note that generated java classes as
CountryName.java
andCountryNameResponse.java
are formatted with xml.
CountryClient.java | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Configuration#
- create a
CountryClientConfig
that we will config the soap request withsoapURl
andcontextPath
. In which,contextPath
is the package that contains all our generated java classes andsoapURl
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 |
|
application.yml | |
---|---|
1 2 3 4 5 |
|
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 |
|
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 |
|
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