Spring Cloud OpenFeign With Web Services#
Using OpenFeign For Web Services#
- In some projects, sometime we will meet some user stories that need us to make a connection to the SOAP service from our spring boot service by giving a file
.wsdl
. So This post will guild you step by step to archive it easily with the support ofSpring Cloud Feign
.
Prepare The Environment#
- To make an example we need an online WebService, 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
Spring cloud fiegn
and SOAP inpom.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 |
|
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 31 32 |
|
<!-- pom.xml plugin for WSDL -->
<build>
<plugins>
<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.cloud.openfeign.web.services.models.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 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.
Configuration#
- In this step we need to create a configuration class to configure the
Encoder
andDecoder
of Feign Client which we will use for the feign client soap adapter later. - I will name this configuration class as FeignSOAPConfiguration
FeignSOAPConfiguration | |
---|---|
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 |
|
- Then in the Main Class, we need to enable OpenFeign Client by adding
@EnableFeignClients
annotation as below.
OpenFeignWebServicesApplication.java | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Creating Adapter#
-
So in this step we will crate an interface feign client adapter to call to our SOAP service.
-
Remember that we have to put the class
FeignSOAPConfiguration
into the attributeconfiguration
of annotation@FeignClient
. Don't for get give it a name with attributename
and put the url of SOAP service (the url that we used to savecountries.wsdl
from browser). -
Because generated java classes (
CountryName.java
andCountryNameResponse.java
) are formatted with xml so we have to define theconsumes
(Input) andproduces
(Output) astext/xml
type.
```java linenums="1" title="CountryClientAdapter.java"
package com.springboot.cloud.openfeign.web.services.api;
import com.springboot.cloud.openfeign.web.services.configuration.FeignSOAPConfiguration; import com.springboot.cloud.openfeign.web.services.models.gen.CountryName; import com.springboot.cloud.openfeign.web.services.models.gen.CountryNameResponse; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody;
/* this @FeignClient includes 3 attributes: name of @FeignClient, url of SOAP service, configuration class for this FeignClient / @FeignClient(name = "soap.client.example", url = "${country.client.soap.url}", configuration = FeignSOAPConfiguration.class) public interface CountryClientAdapter {
/**
@RequestBody (CountryName) and CountryNameResponse are the Object classes
with format xml that was generated from the wsdl file.
So the attribute consumes and produces must be "text/xml"
*/
@PostMapping(value = "", consumes = MediaType.TEXT_XML_VALUE, produces = MediaType.TEXT_XML_VALUE)
CountryNameResponse get(@RequestBody CountryName countryName);
}
```
```yaml linenums="1" title="application.yml" country: client: soap: package: com.example.workflow.models.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.
```java linenums="1" title="CountryCodeHandler.java"
package com.springboot.cloud.openfeign.web.services.service;
import com.springboot.cloud.openfeign.web.services.api.CountryClientAdapter;
import com.springboot.cloud.openfeign.web.services.models.gen.CountryName;
import com.springboot.cloud.openfeign.web.services.models.gen.CountryNameResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class CountryCodeHandler {
@Autowired
private CountryClientAdapter countryClientAdapter;
public CountryNameResponse getCountryName(String countryISO) {
CountryName countryName = new CountryName();
countryName.setSCountryISOCode(countryISO);
return countryClientAdapter.get(countryName);
}
}
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