Deploying RESTful Service on Cloudfoundry

In this post, we will deploy RESTful service on Cloudfoundry using Pivotal Cloud Foundry (PCF) Dev. As creating a restful web service is not a part of this post, I already created employee-service which have a static backend and available for clone from GitHub.

Before deploying it on Cloudfoundry, let’s have a brief about it.


Cloud Foundry is an open source cloud platform as a service (PaaS) on which developers can build, deploy, run and scale applications on public and private cloud models. It is VMware originally created by VMware and now it is part of Pivotal Software.

Now let’s set up lightweight PCF on our local workstation using PCF Dev, following below steps:

Step 1: Download and install cf-cli-installer_6.22.2_osx.pkg  in a directory, for me it’s /Users/ArpitAggarwal/cloudfoundry

$ cd /Users/ArpitAggarwal/cloudfoundry/ 
$ sudo installer -pkg ./cf-cli-installer_6.22.2_osx.pkg -target /

Step 2: Test if Cloudfoundry CLI installed successfully:

$ cf help

Step 3: Next we will download and install PCF Dev in the same directory we created earlier, as follows:

$ cd /Users/ArpitAggarwal/cloudfoundry/
$ unzip
$ ./pcfdev-v0.22.0+PCF1.8.2-osx

Start 4: Start PCF Dev:

$ cd /Users/ArpitAggarwal/cloudfoundry/
$ cf dev start

Above command starts a single virtual machine on our workstation running PCF.

Step 5: Clone employee-service from GitHub in a directory, for me it’s /Users/ArpitAggarwal/

$ cd /Users/ArpitAggarwal/
$ git clone

Step 6: Update the employee-service with manifest.yml:

$ cd /Users/ArpitAggarwal/employee-service
$ touch manifest.yml

manifest.yml created above is used by PCF for deployment to local workstation or on public cloud.

Step 7: Copy the below content in manifest.yml:

- name: empoyee-service
  memory: 1G
  random-route: true
  path: build/libs/empoyee-service-0.0.1.war

name attribute specified above is the name of an application.
path attribute is the directory location of an application.
buildpack attribute points to the java-buildpack, which is used by PCF for running JVM-based applications.

More about manifest.yml you can explore here.

Step 8: Next, we will build the application and push it to Cloudfoundry local workstation after login, as follows:

$ cd /Users/ArpitAggarwal/empoyee-service
$ empoyee-service git:(master) ./gradlew clean build
$ empoyee-service git:(master) ✗ cf login -a --skip-ssl-validation
API endpoint:

Email> user
Password> pass

$ cf push

cf push command specified above push an app or syncs changes to an existing app to the URL

Is application successfully deployed on Cloudfoundry?

Let’s verify it viewing the recent deployment logs where we can look server start up status, as follows:

$ cd /Users/ArpitAggarwal/empoyee-service
$ cf logs empoyee-service --recent

We can also verify application deployment executing GET and POST request against it, as follows:

GET Request to get all employees:

curl -i -H "Accept: application/json" -H "Content-Type: application/json"

POST Request to create an employee:

curl -H "Content-Type: application/json" -X POST -d '{"name": "Arpit Aggarwal","email":""}'

The complete source code is hosted on github.

Writing and Consuming SOAP Webservice with Spring

In the era of RESTful Web Services, I got a chance to consume SOAP Web Service. To do the same I chosen Spring, reason being we are already using Spring as backend framework in our project and secondly it provides an intuitive way to interact service(s) with well-defined boundaries to promote reusability and portability through WebServiceTemplate.

Assuming you already know about SOAP Web Services, let’s start creating hello-world soap service running on port 9999 and client to consume the same, following below steps:

Step 1: Go to and create a new project soap-server adding the Web starters, based on the following image:


Step 2: Edit to publish the hello-world service at Endpoint – http://localhost:9999/service/hello-world, as follows:

package com.arpit.soap.server.main;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.arpit.soap.server.service.impl.HelloWorldServiceImpl;

public class SoapServerApplication implements CommandLineRunner {

	private String servicePort;

	public void run(String... args) throws Exception {
		Endpoint.publish("http://localhost:" + servicePort
				+ "/service/hello-world", new HelloWorldServiceImpl());

	public static void main(String[] args) {, args);

Step 3: Edit to specify the application name, port and port number of hello-world service, as follows:


## Soap Service Port

Step 4: Create additional packages as com.arpit.soap.server.service and com.arpit.soap.server.service.impl to define the Web Service and it’s implementation, as follows:

package com.arpit.soap.server.service;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

import com.arpit.soap.server.model.ApplicationCredentials;

public interface HelloWorldService {

	@WebMethod(operationName = "helloWorld", action = "")
	String helloWorld(final String name,
			@WebParam(header = true) final ApplicationCredentials credential);


@WebService specified above marks a Java class as implementing a Web Service, or a Java interface as defining a Web Service interface.

@WebMethod specified above marks a Java method as a Web Service operation.

@WebParam specified above customize the mapping of an individual parameter to a Web Service message part and XML element.

package com.arpit.soap.server.service.impl;

import javax.jws.WebService;

import com.arpit.soap.server.model.ApplicationCredentials;
import com.arpit.soap.server.service.HelloWorldService;

@WebService(endpointInterface = "com.arpit.soap.server.service.HelloWorldService")
public class HelloWorldServiceImpl implements HelloWorldService {

	public String helloWorld(final String name,
			final ApplicationCredentials credential) {
		return "Hello World from " + name;

Step 5: Move to soap-server directory and run command: mvn spring-boot:run. Once running, open http://localhost:9999/service/hello-world?wsdl to view the WSDL for the hello-world service.

Next, we will create soap-client which will consume our newly created hello-world service.

Step 6: Go to and create a new project soap-client adding the Web, Web Services starters, based on the following image:


Step 7: Edit to create a request to hello-world web service, sending the same to soap-server along with header and get the response from it, as follows:

package com.arpit.soap.client.main;


import javax.xml.bind.JAXBElement;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.xml.transform.StringSource;

import com.arpit.soap.server.service.ApplicationCredentials;
import com.arpit.soap.server.service.HelloWorld;
import com.arpit.soap.server.service.HelloWorldResponse;
import com.arpit.soap.server.service.ObjectFactory;

public class SoapClientApplication implements CommandLineRunner {

	private WebServiceTemplate webServiceTemplate;

	private String serviceSoapAction;

	private String serviceUserId;

	private String serviceUserPassword;

	public static void main(String[] args) {, args);

	public void run(String... args) throws Exception {
		final HelloWorld helloWorld = createHelloWorldRequest();
		final JAXBElement<HelloWorldResponse> jaxbElement = (JAXBElement<HelloWorldResponse>) sendAndRecieve(helloWorld);
		final HelloWorldResponse helloWorldResponse = jaxbElement.getValue();

	private Object sendAndRecieve(HelloWorld seatMapRequestType) {
		return webServiceTemplate.marshalSendAndReceive(seatMapRequestType,
				new WebServiceMessageCallback() {
					public void doWithMessage(WebServiceMessage message)
							throws IOException, TransformerException {
						SoapMessage soapMessage = (SoapMessage) message;
						soapMessage.setSoapAction(serviceSoapAction); soapheader = soapMessage
						final StringWriter out = new StringWriter();
								getHeader(serviceUserId, serviceUserPassword),
								new StreamResult(out));
						Transformer transformer = TransformerFactory
						transformer.transform(new StringSource(out.toString()),

	private Object getHeader(final String userId, final String password) {
		final https.aggarwalarpit_wordpress.ObjectFactory headerObjectFactory = new https.aggarwalarpit_wordpress.ObjectFactory();
		final ApplicationCredentials applicationCredentials = new ApplicationCredentials();
		final JAXBElement<ApplicationCredentials> header = headerObjectFactory
		return header;

	private HelloWorld createHelloWorldRequest() {
		final ObjectFactory objectFactory = new ObjectFactory();
		final HelloWorld helloWorld = objectFactory.createHelloWorld();
		return helloWorld;


Step 8: Next, create additional package com.arpit.soap.client.config to configure WebServiceTemplate, as follows:

package com.arpit.soap.client.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

public class ApplicationConfig extends WebMvcConfigurerAdapter {

	private String serviceEndpoint;

	private String marshallerPackagesToScan;

	private String unmarshallerPackagesToScan;

	public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
		return new PropertySourcesPlaceholderConfigurer();

	public SaajSoapMessageFactory messageFactory() {
		SaajSoapMessageFactory messageFactory = new SaajSoapMessageFactory();
		return messageFactory;

	public Jaxb2Marshaller marshaller() {
		Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
		return marshaller;

	public Jaxb2Marshaller unmarshaller() {
		Jaxb2Marshaller unmarshaller = new Jaxb2Marshaller();
		return unmarshaller;

	public WebServiceTemplate webServiceTemplate() {
		WebServiceTemplate webServiceTemplate = new WebServiceTemplate(
		return webServiceTemplate;

	public HttpComponentsMessageSender messageSender() {
		HttpComponentsMessageSender httpComponentsMessageSender = new HttpComponentsMessageSender();
		return httpComponentsMessageSender;

Step 9: Edit to specify the application name, port and hello-world soap web service configurations, as follows:


## Soap Service Configuration


service.endpoint specified above is the URL provided to the service user to invoke the services exposed by the service provider.

service.soap.action specifies which process or program that need to be called when a request is sent by the service requester and also defines the relative path of the process/program. specifies the packages to scan at time of marshalling before sending the request to the server. specifies the packages to scan at time of unmarshalling after receiving the request from the server.

Now, we will generate Java Objects from WSDL using wsimport and copy it to the soap-client project executing below command on the terminal:

wsimport -keep -verbose http://localhost:9999/service/hello-world?wsdl

Step 10: Move to soap-client directory and run command: mvn spring-boot:run. Once command finishes we will see “Hello World from Arpit” as response from hello-world soap service on console.

While running if you are getting error as – Unable to marshal type “com.arpit.soap.server.service.HelloWorld” as an element because it is missing an @XmlRootElement annotation then add @XmlRootElement(name = “helloWorld”, namespace = “ “) to the com.arpit.soap.server.service.HelloWorld, where namespace should be matched from xmlns:ser defined in soap envelope, as below:

<soapenv:Envelope xmlns:soapenv="" xmlns:ser="">

The complete source code is hosted on github.