SOAP Services in JAVA

  • By
  • September 21, 2019
  • JAVA Programming
SOAP Services in JAVA

SOAP Services in Java

SOAP Web Services are a popular way in enterprises to expose an enterprise system’s capabilities towards other enterprise systems. It allows the systems to interface in a robust way and allows utilization of the Web Service standards around transactions and security, to name a few, to add on resiliency to the service implementation.

In my earlier blog, “What are web-services” from Java Course In Pune, I have explained what is the definition of webservices, what is API and the brief introduction of RESTful Webservices and Soap webservices.

This was further detailed out in the blog “RESTful Services in Java” of Java Classes In Pune, where the concept of web service was explained with the help of implementation of RESTful service.

Now, in this blog, I will give the full introduction of SOAP webservices.

We will understand the following aspects of these popular services:

  • SOAP Services and their use of HTTP protocol and HTTP POST method
  • SOAP Message structure
  • Implementation of SOAP WebServices in Java using Spring framework

So, let’s get started.

For Free Demo classes Call: 8237077325
Registration Link: Click Here!

SOAP Web Service

SOAP is an abbreviation for Simple Object Access Protocol. It’s a messaging protocol that specifies request and response message/document exchanges based on XML usually over application layer protocol like HTTP (most used) or SMTP.

Web Service that are based on SOAP use WSDL (Web Services Description Language) document to describe the web service interface – the operations provided by the web service, request and response message structures provided by these operations and the binding to the protocol used and endpoint of the web service.

The consumers or client side send request SOAP XMLs (usually over HTTP/S) and the server side receives the request, passes the request parameters to the underlying application function. This function processes the request and generates the response. The response is marshalled into a SOAP XML response and sent back to the client.

SOAP Web Services also have standards for aspects like security, addressing and transactions. These can be added on to the SOAP Web Services if required.

Due to the use of standard networking protocols like HTTP, its easier to get the web service to be accessible across any network as the exchanges can easily pass through existing network elements as well as firewalls and proxies. Also, the use of XML allows complex data structures to be easily represented as well as allow internationalized data exchange.

One of the bigger disadvantages of SOAP Web Services is that the interface description tends to be complex and the messages too verbose. Also, the XML parsing, marshalling/unmarshalling process makes the interaction slow and requires parsers on both client and server sides. This makes the SOAP client and SOAP Server-side implementations heavier.

SOAP Message Structure

Soap Message Structure

For Free Demo classes Call: 8237077325
Registration Link: Click Here!

WebService Definition using WSDL

For providing technical details about web service and to share it with consumers and across enterprise systems, the service definition is provided using something called a WSDL document.

WSDL stands for Web Service Description Language.

WSDL is an XML based document that provides technical details about the web service. Some of the useful information in WSDL document are service operations and their name, port types, service endpoint, binding, parameters of the service operations.

Webservice Definition Using Wsdl

SOAP WebService Implementation using Spring

Creating a SOAP WebService using Spring Framework is a popular way due to the ease of implementation. Utilizing Spring allows the abstraction of a lot of boilerplate code.

There are two distinct implementation methodologies followed for creating SOAP Web Services:

  • Contract First

Here the Web Service Interface is defined first. The SOAP message structure is defined and the WSDL interface is defined – its operations and request/response messages, its port types, its binding and the service end point.

Only after this the service implementation is done. This allows for a cleaner implementation, where the interface message objects which the consumer is dependent on is separate from the internal implementation classes. This helps in delinking the internal application changes which can happen in the future from the interface and in turn the consumer. So you don’t have to force the consumer of the service to update the interface files whenever you do an internal change of the application and helps increase the solution’s robustness.

  • Contract Last

Contract Last is the bottom up approach where the service implementation is done. The interface is created last, where the WSDL is reverse generated from the language’s classes. This has the benefit in scenarios where the same implementation needs to be exposed in different integration mechanisms – SOAP, REST, message queues, etc.

Spring Web Service Framework supports the Contract First implementation methodology.

The program below demonstrates how we can achieve this.

SOAP Service with Database

This program would utilize a database to explain the SOAP Service implementation and operations.

We would be using Spring Boot framework. Also you could use Spring initializer for creating the default project.

Step 1

First create a maven project soapWebServices.

We need the spring-boot-starter-web-services dependency as we would utilize Spring’s SOAP Web Service framework and annotations, which we would explain in the later steps.

The dependency allows the import of the spring boot as well as the spring-ws-core libraries (also spring-core framework comes as well due to it being a core dependency of the spring framework).

For Free Demo classes Call: 8237077325
Registration Link: Click Here!

For the generation of the WSDL document itself, we would also need the wsdl4j (WSDL 4 Java) dependency. This aids in generating/updating the WSDL document that Spring Web Service framework generates for us.

Also, since we are integrating with a MySQL database using Spring DAO, we need the spring-jdbc and mysql-connector-java database driver dependencies to be added in.

<?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 https://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.8.RELEASE</version>

                        <relativePath/> <!– lookup parent from repository –>

            </parent>

            <groupId>com.soap</groupId>

            <artifactId>soapWebServices</artifactId>

            <version>0.0.1-SNAPSHOT</version>

            <name>soapWebServices</name>

            <description>Project for SOAP Web Service using Spring Boot</description> 

            <dependencies>

                        <dependency>

                                    <groupId>org.springframework.boot</groupId>

                                    <artifactId>spring-boot-starter-web-services</artifactId>

                        </dependency>

                        <dependency>

                                    <groupId>org.springframework</groupId>

                                    <artifactId>spring-jdbc</artifactId>

                        </dependency>                       <dependency>

                                    <groupId>wsdl4j</groupId>

                                    <artifactId>wsdl4j</artifactId>

                        </dependency>

                        <dependency>

                            <groupId>mysql</groupId>

                            <artifactId>mysql-connector-java</artifactId>

                            <version>5.1.38</version>

                        </dependency>

                        <dependency>

                                    <groupId>org.springframework.boot</groupId>

                                    <artifactId>spring-boot-starter-test</artifactId>

                                    <scope>test</scope>

                        </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>2.5.0</version>

                                                <executions>

                                                            <execution>

                                                                        <id>xjc</id>

                                                                        <goals>

                                                                                    <goal>xjc</goal>

                                                                        </goals>

                                                            </execution>

                                                </executions>

                                                <configuration>

                                                            <sources>

                                                                        <source>${project.basedir}/src/main/resources/schemas</source>

                                                            </sources>

                                <clearOutputDir>false</clearOutputDir>

                                                </configuration>

                                    </plugin>

                        </plugins>

            </build>

 

</project>

Step 2

Create the XML Schema that defines the message structure.

This xsd file is the one that Spring web service with expose and export as the WSDL.

Create src/main/resources/schemas/client.xsd file.

The getClientRequest is the request element of the XML that would come from the consumer of the service. It would have a clientId element that is an integer value representing the unique identity of the Client required to be fetched.

The getClientResponse is the response element of the response XML message that would be sent back to the consumer of the service. It would have a client element representing the client the input clientId identifier represents. The element would contain clientId, clientName, clientCity and clientServices.

<?xml version=”1.0″ encoding=”UTF-8″?>

<schema xmlns=”http://www.w3.org/2001/XMLSchema” xmlns:xs=”http://www.w3.org/2001/XMLSchema” xmlns:appns=”http://www.example.org/client” targetNamespace=”http://www.example.org/client” xmlns:tns=”http://www.example.org/client” elementFormDefault=”qualified”>

            <xs:element name=”getClientRequest”>

                <xs:complexType>

                    <xs:sequence>

                        <xs:element name=”clientId” type=”xs:int”/>

                    </xs:sequence>

                </xs:complexType>

            </xs:element>

            <xs:element name=”getClientResponse”>

                <xs:complexType>

                    <xs:sequence>

                        <xs:element name=”client” type=”appns:client”/>

                    </xs:sequence>

                </xs:complexType>

            </xs:element>

            <xs:complexType name=”client”>

                        <xs:sequence>

                                    <xs:element name=”clientId” type=”xs:int”/>

                                    <xs:element name=”clientName” type=”xs:string”/>

                                    <xs:element name=”clientCity” type=”xs:string”/>

                                    <xs:element name=”clientServices” type=”xs:string”/>

                        </xs:sequence>

            </xs:complexType>

</schema>

Step 3

Generate the Java classes based on the XML schema.

To do this, utilize the jaxb2-maven-plugin and run the xjc (XML to Java compiler) goal during build phase. Add it to the maven pom file in the build phase. This utility would generate the XML schema’s domain java classes that we can use in our service implementation to read the request message and set the response message.

The xjc utility is run on the schema defined in the step above at ${project.basedir}/src/main/resources/schemas

The java classes go into the target/generated-sources/jaxb directory by default.

 

            <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>2.5.0</version>

                                                <executions>

                                                            <execution>

                                                                        <id>xjc</id>

                                                                        <goals>

                                                                                    <goal>xjc</goal>

                                                                        </goals>

                                                            </execution>

                                                </executions>

                                                <configuration>

                                                            <sources>

                                                                        <source>${project.basedir}/src/main/resources/schemas</source>

                                                            </sources>

                                <clearOutputDir>false</clearOutputDir>

                                                </configuration>

                                    </plugin>

                        </plugins>

            </build>

For Free Demo classes Call: 8237077325
Registration Link: Click Here!

Step 4

Create a model class

package com.soap.soapWebServices; 

public class Client { 

            private int clientid; 

            private String clientname; 

            private String clientcity; 

            private String clientservices;                 

            public int getClientid() {

                        return clientid;

            }

            public void setClientid(int clientid) {

                        this.clientid = clientid;

            }

            public String getClientname() {

                        return clientname;

            }

            public void setClientname(String clientname) {

                        this.clientname = clientname;

            }

            public String getClientcity() {

                        return clientcity;

            }

            public void setClientcity(String clientcity) {

                        this.clientcity = clientcity;

            }

            public String getClientservices() {

                        return clientservices;

            }

            public void setClientservices(String clientservices) {

                        this.clientservices = clientservices;

            }              

}

Step 5

Create ClientDAO class for database:

package com.soap.soapWebServices; 

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.List; 

import org.springframework.jdbc.core.BeanPropertyRowMapper;

import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.jdbc.core.RowMapper; 

public class ClientDAO { 

            JdbcTemplate template;              

            public void setTemplate(JdbcTemplate template) { 

                this.template = template; 

            }            

            public Client getClientById(int id){ 

                String sql=”select * from clienttable where clientid=?”; 

                return template.queryForObject(sql, new Object[]{id},new BeanPropertyRowMapper<Client>(Client.class)); 

            } 

            public List<Client> getAllClients(){ 

                return template.query(“select * from clienttable”,new RowMapper<Client>(){ 

                    public Client mapRow(ResultSet rs, int row) throws SQLException { 

                        Client c=new Client(); 

                        c.setClientid(rs.getInt(1)); 

                        c.setClientname(rs.getString(2)); 

                        c.setClientcity(rs.getString(3)); 

                        c.setClientservices(rs.getString(4)); 

                        return c; 

                    } 

                }); 

            } 

            public int insertClient(Client c) {                    

                        String query=”insert into clienttable(clientid,clientname,clientcity,clientservices) values(?,?,?,?)”;

                        return template.update(query,c.getClientid(),c.getClientname(),c.getClientcity(),c.getClientservices());

                        }

            public int updateClient(Client c) {                  

                        String query=”update clienttable set clientname=?,clientcity=?,clientservices=? where clientid=?”;

                        return template.update(query,c.getClientname(),c.getClientcity(),c.getClientservices(),c.getClientid());

                        } 

            public int deleteClient(int id) {

                       

                        String query=”delete from clienttable where clientid=?”;

                        return template.update(query,id);

            }

}

Step 6

For the DAO and MYSQL driver initialization, we have also created a spring application context file.

Store it in the src/main/resources/applicationContext.xml

<?xml version=”1.0″ encoding=”UTF-8″?> 

<beans xmlns=”http://www.springframework.org/schema/beans”   

    xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”   

    xmlns:p=”http://www.springframework.org/schema/p”   

    xmlns:context=”http://www.springframework.org/schema/context”

    xmlns:mvc=”http://www.springframework.org/schema/mvc”  

    xsi:schemaLocation=”http://www.springframework.org/schema/beans   

            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   

            http://www.springframework.org/schema/context   

            http://www.springframework.org/schema/context/spring-context-3.0.xsd

            http://www.springframework.org/schema/mvc

            http://www.springframework.org/schema/mvc/spring-mvc.xsd”>    

            <bean id=”ds” class=”org.springframework.jdbc.datasource.DriverManagerDataSource”> 

            <property name=”driverClassName” value=”com.mysql.jdbc.Driver”></property> 

            <property name=”url” value=”jdbc:mysql://localhost/mydatabase”></property> 

            <property name=”username” value=”user”></property> 

            <property name=”password” value=”password”></property> 

            </bean>            

            <bean id=”jt” class=”org.springframework.jdbc.core.JdbcTemplate”> 

            <property name=”dataSource” ref=”ds”></property> 

            </bean>              

            <bean id=”dao” class=”com.soap.soapWebServices.ClientDAO”> 

            <property name=”template” ref=”jt”></property> 

            </bean> 

</beans> 

Step 7

Load spring application context file.

The easiest way is to create a Configuration file and import the spring context file.

package com.soap.soapWebServices; 

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.ImportResource; 

@Configuration

@ImportResource({“classpath*:applicationContext.xml”})

public class XMLConfiguration { 

} 

This class helps in loading the spring context file we created earlier. @ImportResource({“classpath*:applicationContext.xml”}) imports the applicationContext.xml spring configuration file form the resources folder that we created in the earlier step.

For Free Demo classes Call: 8237077325
Registration Link: Click Here!

Step 8

Create ClientEndpoint class

package com.soap.soapWebServices; 

import org.springframework.beans.factory.annotation.Autowired;

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 org.example.client.GetClientRequest;

import org.example.client.GetClientResponse; 

@Endpoint

public class ClientEndpoint {

            private static final String NAMESPACE_URI = “http://www.example.org/client”; 

            @Autowired 

               ClientDAO dao; 

            @Autowired

            public ClientEndpoint(ClientDAO clientDAO) {

                        this.dao = clientDAO;

            }

             @PayloadRoot(namespace = NAMESPACE_URI, localPart = “getClientRequest”)

            @ResponsePayload

            public GetClientResponse getClient(@RequestPayload GetClientRequest request) {

                        GetClientResponse response = new GetClientResponse();

                        Client daoClient = dao.getClientById(request.getClientId());

                        System.out.println(“Reached method”+request.getClientId());

                        org.example.client.Client intClient = new org.example.client.Client();

                        intClient.setClientId(daoClient.getClientid());

                        intClient.setClientName(daoClient.getClientname());

                        intClient.setClientCity(daoClient.getClientcity());

                        intClient.setClientServices(daoClient.getClientservices());

                        response.setClient(intClient);

                        return response;

            }

}

@Endpoint registers the ClientEndpoint class with Spring Web Service as a candidate class to process the incoming SOAP message.

@PayloadRoot helps Spring WS to decide which SOAP message can be processed by the class’s methods. In the ClientEndpoint class, getClient function can process the message with http://www.example.org/client namespace and getClientRequest local message part.

The @RequestPayload annotation then indicates that the request message would be mapped to the getClient method’s request parameter.

@ResponsePayload then informs Spring WS that it needs to map the GetClientResponse value to the response message in the response payload.

Step 9

Create ClientWebServiceConfig class

package com.soap.soapWebServices; 

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 ClientWebServiceConfig 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 = “client”)

            public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema clientSchema) {

                        DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();

                        wsdl11Definition.setPortTypeName(“ClientPort”);

                        wsdl11Definition.setLocationUri(“/ws”);

                        wsdl11Definition.setTargetNamespace(“http://www.example.org/client”);

                        wsdl11Definition.setSchema(clientSchema);

                        return wsdl11Definition;

            } 

            @Bean

            public XsdSchema clientSchema() {

                        return new SimpleXsdSchema(new ClassPathResource(“schemas/client.xsd”));

            }

}

This class helps with the handling of SOAP messages. It also helps in dispatching of the SOAP messages to the mapped Endpoints. Also the service location url and message schema is provided here so that the WSDL definition can be created by Spring WS.

@EnableWs enables Spring WS SOAP service definition.

The WsConfigurerAdapter class allows us to adapt the web service configuration via the ClientWebServiceConfig implementation to handle the SOAP as per our preference.

The MessageDispatcherServlet servlet allows for automatic detection of the different message endpoints and beans and dispatch the SOAP messages received on the service uri and port to the right one.

DefaultWsdl11Definition exposes the standard WSDL1.1 service and document as provided by the  XsdSchema. Here the schema is the clientSchema as provided in schemas/client.xsd.

We also provide the service endpoint uri as /ws. This would translate to the SOAP service endpoint of http(s)://<host>:<port>/ws/ .

The WSDL document would be available at http(s)://<host>:<port>/ws/client.wsdl. For applications deployed and accessed locally on the machine on port 8080 for example, the uri would be:

http://localhost:8080/ws/client.wsdl

The portname used in the WSDL is ClientPort and the WSDL target namespace is http://www.example.org/client.

Open the wsdl document by accessing http://localhost:8080/ws/client.wsdl

You would see the service address and the details as provided above in the wsdl document.

<?xml version=”1.0″ encoding=”UTF-8″ standalone=”no”?>

<wsdl:definitions xmlns:wsdl=”http://schemas.xmlsoap.org/wsdl/” xmlns:sch=”http://www.example.org/client” xmlns:soap=”http://schemas.xmlsoap.org/wsdl/soap/” xmlns:tns=”http://www.example.org/client” targetNamespace=”http://www.example.org/client”>

  <wsdl:types>

    <schema xmlns=”http://www.w3.org/2001/XMLSchema” xmlns:appns=”http://www.example.org/client” xmlns:xs=”http://www.w3.org/2001/XMLSchema” elementFormDefault=”qualified” targetNamespace=”http://www.example.org/client”>

            <xs:element name=”getClientRequest”>

                <xs:complexType>

                    <xs:sequence>

                        <xs:element name=”clientId” type=”xs:int”/>

                    </xs:sequence>

                </xs:complexType>

            </xs:element>

            <xs:element name=”getClientResponse”>

                <xs:complexType>

                    <xs:sequence>

                        <xs:element name=”client” type=”appns:client”/>

                    </xs:sequence>

                </xs:complexType>

            </xs:element>

            <xs:complexType name=”client”>

                        <xs:sequence>

                                    <xs:element name=”clientId” type=”xs:int”/>

                                    <xs:element name=”clientName” type=”xs:string”/>

                                    <xs:element name=”clientCity” type=”xs:string”/>

                                    <xs:element name=”clientServices” type=”xs:string”/>

                        </xs:sequence>

            </xs:complexType>

</schema>

  </wsdl:types>

  <wsdl:message name=”getClientResponse”>

    <wsdl:part element=”tns:getClientResponse” name=”getClientResponse”>

    </wsdl:part>

  </wsdl:message>

  <wsdl:message name=”getClientRequest”>

    <wsdl:part element=”tns:getClientRequest” name=”getClientRequest”>

    </wsdl:part>

  </wsdl:message>

  <wsdl:portType name=”ClientPort”>

    <wsdl:operation name=”getClient”>

      <wsdl:input message=”tns:getClientRequest” name=”getClientRequest”>

    </wsdl:input>

      <wsdl:output message=”tns:getClientResponse” name=”getClientResponse”>

    </wsdl:output>

    </wsdl:operation>

  </wsdl:portType>

  <wsdl:binding name=”ClientPortSoap11″ type=”tns:ClientPort”>

    <soap:binding style=”document” transport=”http://schemas.xmlsoap.org/soap/http”/>

    <wsdl:operation name=”getClient”>

      <soap:operation soapAction=””/>

      <wsdl:input name=”getClientRequest”>

        <soap:body use=”literal”/>

      </wsdl:input>

      <wsdl:output name=”getClientResponse”>

        <soap:body use=”literal”/>

      </wsdl:output>

    </wsdl:operation>

  </wsdl:binding>

  <wsdl:service name=”ClientPortService”>

    <wsdl:port binding=”tns:ClientPortSoap11″ name=”ClientPortSoap11″>

      <soap:address location=”http://localhost:9091/ws”/>

    </wsdl:port>

  </wsdl:service>

</wsdl:definitions>

Step 10

Create SoapWebServicesApplication class to launch the Spring Boot application

package com.soap.soapWebServices;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class SoapWebServicesApplication { 

            public static void main(String[] args) {

                        SpringApplication.run(SoapWebServicesApplication.class, args);

            }

 }

Invoking the Service

Use Restlet Client or Postman tool to invoke the SOAP Web Service that was created above.

Ivoke

Spring Annotations Explained

There are a few spring annotations used in the application code snippets above. These are explained in brief below. Its recommended to refer to the Spring documentation for a detained explanation for the same.

For Free Demo classes Call: 8237077325
Registration Link: Click Here!

 

Annotation Description Example
@Endpoint Indicates that the annotated class is a web service endpoint – i.e. it has a method that can process the SOAP request message and in turn provide a response.

An endpoint is created by annotating a class with the @Endpoint. In the class, one or more methods can be defined that handle the incoming XML request. Spring supports a wide variety of parameter types (such as DOM elements, JAXB2 objects, etc) indicated by @RequestPayload or @XPathParam. You indicate the sort of messages a method can handle by using another annotation (typically @PayloadRoot).

This is a specialization of @Component and allows the implementation classes to be autodetected through classpath scanning by Spring.

@Endpoint

public class ClientEndpoint {

}

 

When a SOAP request is received, ClientEndpoint is one of the classes that would be considered for handling the request.

@EnableWs The @EnableWs annotation enables support for the @Endpoint and related Spring Ws configurations. @EnableWs

@Configuration

public class ClientWebServiceConfig extends WsConfigurerAdapter {

}

 

@PayloadRoot Indicates what sort of SOAP messages can be handled by an endpoint method.

The annotation comes with a namespace and localPart values. These mark the method with a fully qualified name.

Whenever a SOAP message comes with a payload root element with the qualified name, the method is invoked.

@PayloadRoot(namespace = NAMESPACE_URI, localPart = “getClientRequest”)

@ResponsePayload

public GetClientResponse getClient(@RequestPayload GetClientRequest request) {

}

 

The method accepts a SOAP request payload that a getClientRequest element local name with NAMESPACE_URI namespace prefix.

@RequestPayload @RequestPayload is added to endpoint method to indicate what sort of message is taken as a parameter in the method. This allows Spring WS to understand the parameter that needs to be bound to request payload and the conversion of payload to the correct parameter type.

The supported parameter types can be TrAX, DOM Element, dom4j, JDOM, XOM, StAX, JAXB2, OXM.

public GetClientResponse getClient(@RequestPayload GetClientRequest request) {

}

 

 

Map request message payload to the GetClientRequest parameter.

@ResponsePayload @ResponsePayload is added to endpoint method to map the return value to the response message payload.

This allows Spring WS to understand that the return value needs to be bound to the response payload and to perform the correct conversion of return type to the payload.

The supported response types can be TrAX, W3C DOM Element, dom4j, JDOM, XOM, JAXB2, OXM

@PayloadRoot(namespace = NAMESPACE_URI, localPart = “getClientRequest”)

@ResponsePayload

public GetClientResponse getClient(@RequestPayload GetClientRequest request) {

}

 

Map GetClientResponse to the response message payload.

@Configuration @Configuration informs Spring that the annotated class provides Spring Configuration information. @Configuration

@ImportResource({“classpath*:applicationContext.xml”})

public class XMLConfiguration {

 

}

 

In Conclusion

I hope this blog would have helped you understand SOAP Web Services and explained the concepts around it. Also, the examples would have shown how to create your SOAP Web Services using Java and Spring framework. So, go ahead and try for yourselves.

Looking forward to your experiences and comments at Java Training In Pune.

Author: Kiran Tiwari

Designation: Software Trainer

Company:  Seven Mentor Pvt. Ltd.

kirru

Call the Trainer and Book your free demo Class for JAVA now!!!

call icon

© Copyright 2019 | Sevenmentor Pvt Ltd.

 

Submit Comment

Your email address will not be published. Required fields are marked *

*
*