A warm welcome to tech-maze, in today's article, we will see how we can consume SOAP api in better and concise manner.
Today most of the new developer uses Rest api, Graphql whenever their project interact with different systems, this makes their life easier and implementing rest based code is also quite straight forward, but sometime we have to interact with a system which is old, monolithic and uses SOAP services.
People who has worked only on Rest api and Graphql find it a little bit difficult to work on SOAP, because the very first problem we face is to choose which configuration will be useful and easy as there are various ways to consume the SOAP api
second problem while consuming SOAP api in current technology/version, it gives lots of error and it makes the implementation process tedious and time consuming.
For example if we use latest version of spring boot and latest jdk, we don't have much example how to consume SOAP api with latest technology, and when we try to consume SOAP api in old ways, it gives alien type of error which really takes time to resolve.
In this article I have shown how SOAP api can be consumed with minimal changes in the code, I have tried to reduce the complexity as much as possible.
For simplicity I have consumed the services in main class itself, same can be used anywhere in the code (in general we create separate package and write a class and provide methods for DB related operation, api related operation, same can be done easily in this class, we only have to put the code which we have written in main class to the class which we have created under separate package)
I'll explain how this can be achieved easily, without spending more time in it.
STEP 1 : -
provide following dependency in pom.xml -
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-ws-core</artifactId>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>javax.xml.soap</groupId>
<artifactId>javax.xml.soap-api</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.messaging.saaj</groupId>
<artifactId>saaj-impl</artifactId>
<version>1.5.2</version>
</dependency>
spring-ws-core -- this has used here to provide webservices capability to our project
jaxb-impl/javax.xml.soap-api -- both has used to provide marshalling and unmarshalling of request and response in XML format.
saaj-impl -- this allows a client to send messages directly to the recipient using SOAP Connection object, which provides a point-to-point connection to the intended recipient
add following plugin and configuration where we have to pass the wsdl url : -
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.14.0</version>
<configuration>
<schemaLanguage>WSDL</schemaLanguage>
<generatePackage>com.concretepage.wsdl</generatePackage>
<schemas>
<schema>
</url>
</schema>
</schemas>
</configuration>
Under the configuration we have to provide generatedPackage, this is the placeholder for the package where we want all the classes should be stored when classes generated from the wsdl.
This package location is user specific.
Under schema more specifically in the url tag, we have to provide the complete wsdl url.
here first step is over, lets move to second step
STEP 2 :-
In this step we will create a class where we have to provide properties of marshalling/unmarshalling. here we have to mention the package name where we have to perform marshalling and then we have to set whole package for marshalling and unmarshalling when a client fire an api call.
public Jaxb2Marshaller marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath("com.concretepage.wsdl");
return marshaller;
}
@Bean
public CountryInformationClient countryInfoClient(Jaxb2Marshaller marshaller) {
CountryInformationClient client = new CountryInformationClient();
client.setDefaultUri(
);
client.setMarshaller(marshaller);
client.setUnmarshaller(marshaller);
return client;
}
that's all we have to do for consuming a soap api, now we are ready to fire api calls.
Consuming of webservice is done in the class "CountryInformationClient" which extends class "WebServiceGatewaySupport", now the question arises why do we have to extend this class, actually its not mandatory to know what it does, but still its good to know why do we need this.
WebServiceGatewaySupport : this class internally create a template for marshalling and unmarshalling of request/response, so that we don't have to do that explicitly.
one more thing we have to remember, before running the server, we need to run "maven clean" command to generate the classes and then "maven install" to regenerate the dependencies and remove conflicts, if still it gives any error then update the maven project, it will resolve all the issues.
Now run main class and check the output in the console.
That's all folks, hope this will help at some extent, for more details please have a look in the below GitHub project, I will come up with another article shortly.
Git location for the demo : https://github.com/rohitmodi07/springboot-soap-consume
A Big thank you for checking out my article, this really encourage me to come up with more topics.
Here are some resources you might be interested ------------------------
Commentaires