Creating a SOAP web service with Spring Boot

Creating a SOAP web service with Spring Boot

Creating a SOAP web service with Spring Boot

Main Application Spring Boot File:


package com.ngdeveloper;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class CouponApplication {

public static void main(String[] args) {
SpringApplication.run(CouponApplication.class, args);
}
}

 

pom.xml file:

This below pom.xml file contains the plugin to generate the output directory files from the xsd file, so create the pom.xml, xsd file, endpoints then by starting your spring boot application you will be able to find the other required files generated.

These are the files will be generated automatically with this heading info,


//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.7
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2019.01.23 at 03:41:28 PM IST
//

  • CouponDetails
  • GetCouponDetailsRequest
  • GetCouponDetailsResponse
  • ObjectFactory
  • package-info

 


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.ngdeveloper</groupId>
<artifactId>Coupon</artifactId>
<version>1.0</version>
<name>Coupon</name>
<description>Coupon Web service</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaDirectory>${project.basedir}/src/main/resources/</schemaDirectory>
<outputDirectory>${project.basedir}/src/main/java</outputDirectory>
<clearOutputDir>false</clearOutputDir>
</configuration>
</plugin>
</plugins>
</build>

</project>

Create a xsd file for your request and responses,

This below coupon-details.xsd file is the request and response file,


<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://ngdeveloper.com/coupon"
targetNamespace="http://ngdeveloper.com/coupon" elementFormDefault="qualified">

<xs:element name="GetCouponDetailsRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:int" />
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="GetCouponDetailsResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="CouponDetails" type="tns:CouponDetails" />
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:complexType name="CouponDetails">
<xs:sequence>
<xs:element name="id" type="xs:int" />
<xs:element name="title" type="xs:string" />
<xs:element name="code" type="xs:string" />
</xs:sequence>
</xs:complexType>

</xs:schema>

 

Creating an endpoint:


package com.ngdeveloper.ws.endpoints;

import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;

import com.ngdeveloper.CouponDetails;
import com.ngdeveloper.GetCouponDetailsRequest;
import com.ngdeveloper.GetCouponDetailsResponse;

@Endpoint
public class CouponDetailsEndPoint {

@PayloadRoot(namespace = "http://ngdeveloper.com/coupon", localPart = "GetCouponDetailsRequest")
@ResponsePayload
public GetCouponDetailsResponse processCouponDetailsRequest(@RequestPayload GetCouponDetailsRequest request) {
GetCouponDetailsResponse response = new GetCouponDetailsResponse();
CouponDetails couponDetails = new CouponDetails();
couponDetails.setId(request.getId());
couponDetails.setTitle("10% OFF on Paytm");
couponDetails.setCode("PYTM10");
response.setCouponDetails(couponDetails);
return response;
}
}

 

WebserviceConfig file:

This is the configuration file in spring boot for soap webservice and load the xsd file.

 


package com.ngdeveloper;

import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.ws.config.annotation.EnableWs;
import org.springframework.ws.config.annotation.WsConfigurerAdapter;
import org.springframework.ws.transport.http.MessageDispatcherServlet;
import org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition;
import org.springframework.xml.xsd.SimpleXsdSchema;
import org.springframework.xml.xsd.XsdSchema;

@EnableWs
@Configuration
public class WebServiceConfig extends WsConfigurerAdapter {
@Bean
public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
MessageDispatcherServlet servlet = new MessageDispatcherServlet();
servlet.setApplicationContext(applicationContext);
servlet.setTransformWsdlLocations(true);
return new ServletRegistrationBean(servlet, "/ws/*");
}

@Bean(name = "coupon")
public DefaultWsdl11Definition defaultCouponWsdl11Definition(XsdSchema couponSchema) {
DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
wsdl11Definition.setPortTypeName("CouponPort");
wsdl11Definition.setLocationUri("/ws");
wsdl11Definition.setTargetNamespace("http://ngdeveloper.com/coupon");
wsdl11Definition.setSchema(couponSchema);
return wsdl11Definition;
}

@Bean
public XsdSchema couponSchema() {
return new SimpleXsdSchema(new ClassPathResource("coupon-details.xsd"));
}
}

 

Add these below lines in application.properties to log both input request and output response,

logging.level.org.springframework.web=DEBUG
logging.level.org.springframework.ws.client.MessageTracing.sent=DEBUG
logging.level.org.springframework.ws.server.MessageTracing.sent=DEBUG
logging.level.org.springframework.ws.client.MessageTracing.received=TRACE
logging.level.org.springframework.ws.server.MessageTracing.received=TRACE

 

 

Testing created SOAP web service:

Install wizdler plugin to test SOAP web services directly from your chrome browser.

You can also use SOAP UI tool (both free and pro tools available here)

 

Open your wsdl link in chrome browser then click wizdler icon to pass the request.

 

How to generate java classes from xsd:

This below plugin is going to help to generate java classes from xsd file.

Add the below plugin to your pom,xml,

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaDirectory>${project.basedir}/src/main/resources/</schemaDirectory>
<outputDirectory>${project.basedir}/src/main/java/</outputDirectory>
<clearOutputDir>false</clearOutputDir>
</configuration>
</plugin>

Note: If you have this plugin also, then it may not work, in that case comment the below section till to generate the class files from the xsd resources.

<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.14.0</version>

Run

mvn jaxb2:xjc

(or)

jaxb2:xjc (if you are running from eclipse)

to generate the java classes from xsd schema.

 

Happy coding!

 

 

229 total views, 4 views today

About Mirthbees 672 Articles
Blogger, Innovator, Developer and Enterpreneur. Founder of GuruRecharge, Readymade online recharge script. Lead Developer in Bookrate.in

Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.