# How to create SOAP webservice using Spring-WS & Spring Boot

## Overview

In this tutorial, we’ll see how to create a SOAP webservice using Spring-WS and Spring Boot. This post will focus only on pure Java based approach using annotations.

1. JDK 1.8 +
2. Maven
3. IDE

## Approach

SOAP services can be developed with two methods

1. Contract First : Define WSDL and Schema before writing any code.
2. Contract Last: Auto-generate the WSDL and schemas from the java classes.

Spring-ws only supports the contract-first approach

## Project setup

You can clone this project from Github to kick start the project

Create a maven project and add the following dependencies in the pom.xml

## Schema Design

Contract first approach requires us to define the schema. And then we’ll use Spring-ws auto-generate WSDL out of the schema.

## Generate Java Classes

Now, we’ll jaxb2-maven-plugin to generate the java classes from the schema. Add the below plugin in your pom.xml

• schemaDirectory : location of the schema
• outputDirectory: where we want our java classes.
• clearOutputDir : making this true will delete the classes every time you compile the project

Now, we’ll generate the classes by issuing a following maven command.

$mvn clean install  Now you can see the auto-generated classes in you project folder. ## Setup Endpoint Now, we’ll setup an endpoint in our Java code to serve the request. Create a class and annotate with @Endpoint For the purpose of this tutorial, I added very simple code to return a success response. • @Endpoint: This indicates that this class is a web service endpoint • @PayloadRoot: This indicates that incoming soap request for this method will have defined local part and namespace. It will basically try to match the RootElement of your xml message. • @ResponsePayload: This indicates that method will return a payload. ## Configure Servlet Bean & WSDL Definition • Define the ServletRegistrationBean in configuration to register a servlet which will listen the incoming requests. • Define the configuration for WSDL Definition • portTypeName : Interface name • locationUri : URL to expose service • targetNamespace: Target name space for the WSDL elements • schema: Location of the schema • @Bean(name = “customer”) :Name of this bean will be used the wsdl name. ## Bootstrap as Spring Boot Application Add following plugin in pom.xml to make the jar spring boot compatible. Define the main method which will allow this application to run using Spring Boot.This class should be in the root package always for the component scan. ## Deploy the service $ mvn spring-boot:run


Now, you can see the WSDL at the following location
http://localhost:8080/ws/customer.wsdl

## Test the service

• Import the wsdl in SOAP-UI & soap-ui will auto generate request structure for the request.

• Send request to service

# Developing REST Service using Spring Boot

## Overview

The tutorial will show, how to develop a REST API with Spring. We’ll look into the Spring Boot solution for creating the application.

We’ll primarily use Java configuration in this tutorial.

## Pre-requisties

• JDK 1.8
• Maven
• IDE (for ease of use)

## Setup maven project

Creat a maven project and add the required dependencies and plugins in pom.xml

We will use the dependencyManagement to define a standard version of an artifact to use across multiple projects.

We will allow spring-boot-starter-parent to manage the version of the artifacts.

Now we’ll add the spring-boot-starter-web dependency.

Spring Boot Starter Web includes:

• spring-web module
• spring-webmvc module
• tomcat starter

Spring-web & Spring-webmvc are required for spring appplication while tomcat starter is required to run web application directly without explicitly installing any server.

Spring Boot Maven Plugin provides Spring Boot support in Maven, letting you package executable jar or war archives and run an application “in-place”.

### Creating a Spring Boot Application

Create a class with main method. Annotate the class with @SpringBootApplication. Call the run method of SpringApplication class

@SpringBootApplication includes the functionality of following annotations:

• @Configuration
• @EnableAutoConfiguration
• @ComponentScan.

By default, it will perform the component scan in base package. So make sure SpringBootWebApplication class is present in the base package.

### Creating a REST Controller

Create a class with a meaningful name based on your requirement & annotate the class with @RestController. The @RestController is the central artifact in the entire Web Tier of the RESTful API.

### Create a GET method to accept requests

Create a method named home to accept the request on ‘/‘ url and show the index.html

### Create a POST method to accept requests

Create a method named home to accept the request on ‘/‘ url and show the index.html

### Configuration using properties file

Create a file application.properties inside resources folder of application and following properties.

server.port : to change the default port 8080 to any other port number.

logging.level.org.springframework.web : to change the log level default INFO to DEBUG

# Developing SOAP Web service using Apache CXF and Apache Camel

### Overview

In this post, we will walk through the steps to develop a SOAP service using Java and we will also leverage Spring , Apache Camel & Apache CXF.

### Pre-requisties

1) JDK 1.8 for code development
2) Maven for dependency management
3) JBoss Fuse 6.3 for deployment

### Code Development

#### Maven Project with required dependencies

Create a maven project using IDE and add following dependencies in the pom.xml

About the components used in pom.xml

Project structure

#### Designing Schema

You can refer this post for detail discussion on designing schema

Below is the schema which we will use in this example
src/main/resources/wsdl/customer-service.wsdl

#### Designing WSDL

Below is the WSDL which we will use in this example
src/main/resources/wsdl/customer-service.xsd

#### XML Based Spring configuration

We’ll use the xml based spring configuration for this example to expose the service.

##### Define the CXF Endpoint

Now, we’ll define the CXF Endpoint to expose the service

• serviceName & endpointName should be same as specified in wsdl,
• wsdlURL relative to resources folder or it can a http url also.
##### Define the CamelContext

All the camel routes will be written in the camel context, so we will create a camelContext in the same file.

##### Define the dataFormats

We will define the data format to convert the soap xml payload into java object and vice versa. Context path is the root folder where you have all the auto-generated classes (WSDL & schema objects created using codegen-plugin)

##### Define the camel route

We’ll define the camel route based on the logic, in this example route is pretty small & simple, but trust me at enterprise level it can be bit complex and longer.

• cxf component & bean id is used to lookup the cxfEndpoint.
• camel:from will allow to consume the request(s) incoming the cxf endpoint.
• camel:unmarshal is used for converting soap payload to java objects
• camel:process is used for the logic to be performed in the service
• camel:marshal is used for converting java object to soap xml.

This is how your routes file will look like
src/main/resources/META-INF/spring/bundle-context-routes.xml

#### Response Processor

For this example, we will write a processor which stores the employee data in map and return customer ID.

Create CustomerResponseProcessor.java in src/main/resources

#### Code Build

Now we’ll compile the code & package it as .jar using maven.

#### Deploy on JBoss Fuse

Once we have the jar file ready with us, we’ll deploy it JBoss Fuse 6.3. You can refer this post to learn more about the deployments on JBoss Fuse.

Run following command for the deployment

JBossFuse:admin@root> install -s mvn:com.customer.service/customer-service-impl/1.0.0


Now you can check the service url by visiting below url
http://localhost:8181/cxf

# How to create a WSDL file

## Introduction

WSDL stands for Web Services Description Language. WSDL is used to describe web services and also known as Contract. It is the base building block to create a SOAP webservice in Contract-First Approach.

Creation of WSDL consist of following:

• Schema Design
• WSDL Design

In the below example, we are creating a WSDL which contain a single operation createCustomer. You can follow the similar approach to add more operation in the WSDL.

## Schema Design

First of all, we will create a schema file to define the request, response and fault structure.

### XML Definition

Create a file of .xsd extension, in this case we are using customer-service.xsd

## WSDL Design

### Definitions

Create a file of .wsdl extension, in this case we are using customer-service.wsdl

### Schema Import

Import the schema which we have created in above example.

### Messages

Defines the data elements for each operation

### PortType

Describes the operations that can be performed and the messages involved.

### Binding

Defines the protocol and data format for each port type

• binding element has two attributes - name and type. The name attribute (you can use any name you want) defines the name of the binding, and the type attribute points to the port for the binding, in this case the “CustomerServicePort” port.

• soap:binding element has two attributes - style and transport. The style attribute can be “rpc” or “document”. In this case we use document. The transport attribute defines the SOAP protocol to use. In this case we use HTTP.

• operation element defines each operation that the portType exposes. For each operation the corresponding SOAP action has to be defined. You must also specify how the input and output are encoded. In this case we use “literal”.

### Service

<wsdl:service name="CustomerService">
<wsdl:port name="CustomerServicePort" binding="tns:CustomerServiceBinding">
</wsdl:port>
</wsdl:service>


# SOAP - Simple Object Access Protocol

## What is SOAP ?

Simple Object Access Protocol is a messaging protocol specification for exchanging structured information. Its purpose is to provide extensibility, neutrality and independence.

• SOAP uses XML for its message format.
• SOAP relies on application layer protocols such as HTTP, SMTP, TCP, UDP, or JMS for message negotiation and transmission.

SOAP consists of three parts

• an envelope, which defines the message structure and how to process it
• a set of encoding rules for expressing instances of application-defined datatypes
• a convention for representing procedure calls and responses

## SOAP building blocks

A SOAP message is an ordinary XML document containing the following elements:

Element Description Required
Envelope Identifies the XML document as a SOAP message Yes
Body Contains call, and response information Yes
Fault Provides information about errors that occurred while processing the message No