Spring Boot Microservice Deployment in Pivotal Web Services With ClearDB MySQL Database

DZone 's Guide to

Spring Boot Microservice Deployment in Pivotal Web Services With ClearDB MySQL Database

This tutorial will help you learn how to use the Cloud Foundry CLI, then create and deploy a Spring Boot microservices app with Flyway database migration.

Free Resource

In this article, we are going to see how to create a new Pivotal Web Services account. We'll learn about installing the Cloud Foundry CLI, create a Spring Boot microservice app with Flyway database migration, and deploy the microservice in Pivotal Web Services with a ClearDB MySQL database service.


  1. Pivotal Account

  2. Pivotal CLI version: 6.35.2

  3. JDK version: 1.8.0_162

  4. Gradle version: 4.6

  5. Spring Boot version: 2.0

Create a New Pivotal Web Services Account

Here is the step by step procedure to create a new Pivotal Web Services account. If you have a Pivotal account already, please skip this.

Note: I used my personal email - john.smith.kite.1990@gmail.com - to create a Pivotal Web Services account.

Step 1: Go to this link to do the signup process with the Pivotal account - https://account.run.pivotal.io/z/uaa/sign-up.

Step 2: After the signup, Pivotal sends the verification link to your email.

Step 3: After verifying your Email ID, you will be redirected to the Pivotal login page.

Step 4: Provide your Email ID and password, then log into your Pivotal account.

Step 5: After a successful login, your Pivotal account landing page looks like below.

Pivotal Landing page

Step 6: Click the Pivotal Web Services link and agree to the Terms of Service for Pivotal Web Services.

Pivotal Terms and Conditions

Step 7: Next, click "Claim Your Trial" button and complete the Mobile Verification process to claim your trial account.

Note: You can upgrade your account at any time.

Pivotal claiming your Trail

Step 8: Provide your mobile verification code.

Pivotal mobile verification

Step 9: After successful mobile verification, create your Trail ORG (Project).

Pivotal org create

Step 10: Provide your Organization name or Project Name then click "Start Free Trial" button. After that, Pivotal will take you the default "development" space landing page.


Installing Cloud Foundry CLI

Here we are going to install the Cloud Foundry Command Line Interface on Windows. Cloud Foundry CLI is used for pushing and managing your apps and to create/bind your services.

Step 1: On the organization page, click "Tools" from the left side menu.

Step 2: Download the Windows 64 bit version CLI tool (whichever version you want based on your OS).

CLI download

Step 3: After downloading, extract the Zip file. Then install the cf_installer file.CLI file

Step 4: Choose your installation options (who should this application be installed for?).

Install option 1

Step 5: Provide the path where CF CLI should be installed.

install option 2

Step 6: Now CF CLI is ready to install. Click the "Install" button.

Install option 3

Step 7: After the successful install, click the "Finish" button and close the installation window.

Install option 4

Step 8: To verify the installation, check the CF CLI version by executing this command on your machine:

cf version

Create a Spring Boot Microservice App With Flyway Database Migration

Here we are going to create a Spring Boot app with the following dependencies using Gradle build.

Spring Boot - Project MetaData is given below.

Artifact Coordinates:

  • Group: com.example.projects

  • Artifact: pivotalservice

Add the following dependencies in the build.gradle file to build our Spring Boot application.


Spring Boot Starter Web - to write a REST endpoint.

MySQL Connector Java - to connect your application with MySQL.

Spring Boot Starter JDBC - to create a JDBC Connection to access the MySQL database.

Flyway DB core - for MySQL database migrations.

The build.gradle file is given below:

buildscript {
 ext {
  springBootVersion = '2.0.0.RELEASE'
 repositories {
 dependencies {

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.example.projects'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {

dependencies {

The main Spring Boot application class file is given below:

package com.example.projects.pivotalservice;

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

public class PivotalserviceApplication {

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

Create a model to set/get the Users under the com.example.projects.pivotalservice.model package.

package com.example.projects.pivotalservice.model;

public class Users {

 private String id;
 private String name;

 public String getId() {
  return id;

 public void setId(String id) {
  this.id = id;

 public String getName() {
  return name;

 public void setName(String name) {
  this.name = name;


Create a DAO Repository class file which contains two methods to fetch the list of users and insert a new user under the com.example.projects.pivotalservice.dao package by using JdbcTemplate.

package com.example.projects.pivotalservice.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.example.projects.pivotalservice.model.Users;

public class PivotalDAOService {

 JdbcTemplate jdbcTemplate;

 public List < Users > getUsers() {
  Collection < Map < String, Object >> rows = jdbcTemplate.queryForList("SELECT * FROM USERS");
  List < Users > usersList = new ArrayList < > ();
  rows.stream().map((row) -> {
   Users user = new Users();
   user.setName((String) row.get("NAME"));
   return user;
  }).forEach((ss) -> {
  return usersList;

 public void createUser(Users user) {
  jdbcTemplate.update((Connection connection) -> {
   PreparedStatement preparedStatement = null;
   try {
    preparedStatement = connection.prepareStatement("INSERT INTO USERS (NAME) VALUES (?)");
    preparedStatement.setString(1, user.getName());
   } catch (Exception e) {
    if (preparedStatement != null) {
    throw (e);
   return preparedStatement;

Provide/Configure the DataSource to connect the MySQL database in the application.yml file.

    name: pivotalservice
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost/PIVOTALSERVICE?autoreconnect=true
    username: root
    password: root
    testOnBorrow: true
    testWhileIdle: true
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 30000
    validationQuery: SELECT 1
    max-active: 15
    max-idle: 10
    max-wait: 8000

Create a REST Controller Class file which contains two request mapping methods - "GET" and "POST" methods - to fetch the list of users and create a new user to the database table under the package name is com.example.projects.pivotalservice.controller.

package com.example.projects.pivotalservice.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.example.projects.pivotalservice.dao.PivotalDAOService;
import com.example.projects.pivotalservice.model.Users;

public class PivotalServiceController {

 PivotalDAOService pivotalDAOService;

 @RequestMapping(value = "/users", method = RequestMethod.GET)
 public List < Users > getUsers() {
  return pivotalDAOService.getUsers();

 @RequestMapping(value = "/users", method = RequestMethod.POST)
 public String createUser(@RequestBody Users user) {
  return "User created successfully";

Create a V1__Initial.sql file for flyway database migration under the src/main/resources/db/migration directory. This SQL file should contain the SQL schema to create a USERS table and default insert user SQL statements.



Before deploying your application into the Pivotal Web Services, please run it on your local machine and make sure your DB and API are working fine.

Build your application on your local machine by using the gradle clean build command.

After the build is successful, go to the build/libs directory run the jar file by using the command java -jar <jarfile>.

Now, hit the GET and POST API(s) and make sure the API is working fine.

curl -X GET \
curl -X POST \
  http://localhost:8080/users \
  -d '{

Deploy the Microservice in Pivotal Web Service With ClearDB MySQL

If Cloud Foundry CLI is installed on your machine, then try to login into your Pivotal Web Service account by using CLI.

To log in to the Pivotal account by using CLI, execute the below command on your command prompt.

cf login

Provide your Email ID and Password at the command prompt and set your API Endpoint, organization, and space.

cf login

Create a ClearDB MySQL Service Instance

To create a Clear DB MySQL service instance, execute the below command in your command prompt:

cf create-service cleardb spark mysql

cf mysql service

where mysql - instance name

Configure the Deployment Manifest File

Create a manifest file for your Spring Boot app at the application root path. The manifest file should be a YAML file.

The manifest.yml file is given below:

  - name: pivotalservice
    buildpack: java_buildpack
    path: build/libs/pivotalservice-0.0.1-SNAPSHOT.jar
      - mysql

Now, go to the application path at the command prompt and push your application to Pivotal Web Services.

Push your application by using the below command

cf push 

cf push success

The cf push command uses your manifest.yml file to create the application attributes and push your changes into the Pivotal Web Services.

Finally, the cf push command will push your application it started. You can find the application status and routes on the command prompt itself.

The application route for this app is http://pivotalservice.cfapps.io.

Now, you can test your endpoints.


curl -X GET \

The API response is given below

 "id": "1",
 "name": "John"
}, {
 "id": "2",
 "name": "Smith"


curl -X POST \
  http://pivotalservice.cfapps.io/users \
  -H 'content-type: application/json' \
  -d '{

The API response is given below:

User created successfully

Again, hit the GET API.

curl -X GET \

The API response is given below:

 "id": "1",
 "name": "John"
}, {
 "id": "2",
 "name": "Daniel"
}, {
 "id": "11",
 "name": "Daniel"

Note: Clear DB MySQL service will create the database automatically and create a schema from the V1__Initial.sql file.

Also, our Pivotal application connects to the Clear DB MySQL service to the application automatically based on the JDBC URL, Username, and Password we mentioned in the application.yml file.

To view the Service ENV variables, please execute the below command:

commnd env

The Service ENV list is given below.

service env list

Thank you.

cloud foundry cli, deployment, flyway, gradle, microservices, mysql, pivotal web services, spring boot, tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}