0% found this document useful (0 votes)
109 views337 pages

SpringBoot Notes

Spring Boot is an open-source Java framework designed for creating microservices and production-ready applications with minimal configuration. It offers advantages such as easy deployment, scalability, and reduced development time, while automatically configuring applications based on dependencies. The document also covers how to create a Spring Boot application using Maven and Gradle, as well as deploying it on a Tomcat server.

Uploaded by

suresh709210
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views337 pages

SpringBoot Notes

Spring Boot is an open-source Java framework designed for creating microservices and production-ready applications with minimal configuration. It offers advantages such as easy deployment, scalability, and reduced development time, while automatically configuring applications based on dependencies. The document also covers how to create a Spring Boot application using Maven and Gradle, as well as deploying it on a Tomcat server.

Uploaded by

suresh709210
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Spring Boot - Introduction

Spring Boot is an open source Java-based framework used to


create a micro Service. It is developed by Pivotal Team and is
used to build stand-alone and production ready spring
applications. This chapter will give you an introduction to Spring
Boot and familiarizes you with its basic concepts.

What is Micro Service?


Micro Service is an architecture that allows the developers to
develop and deploy services independently. Each service running
has its own process and this achieves the lightweight model to
support business applications.

Advantages

Micro services offers the following advantages to its developers −

 Easy deployment
 Simple scalability
 Compatible with Containers
 Minimum configuration
 Lesser production time

What is Spring Boot?


Spring Boot provides a good platform for Java developers to
develop a stand-alone and production-grade spring application
that you can just run. You can get started with minimum
configurations without the need for an entire Spring configuration
setup.

Advantages

Spring Boot offers the following advantages to its developers −

 Easy to understand and develop spring applications


 Increases productivity
 Reduces the development time

Goals

Spring Boot is designed with the following goals −


 To avoid complex XML configuration in Spring
 To develop a production ready Spring applications in an
easier way
 To reduce the development time and run the application
independently
 Offer an easier way of getting started with the application

Why Spring Boot?


You can choose Spring Boot because of the features and benefits
it offers as given here −

 It provides a flexible way to configure Java Beans, XML


configurations, and Database Transactions.
 It provides a powerful batch processing and manages REST
endpoints.
 In Spring Boot, everything is auto configured; no manual
configurations are needed.
 It offers annotation-based spring application
 Eases dependency management
 It includes Embedded Servlet Container

How does it work?


Spring Boot automatically configures your application based on
the dependencies you have added to the project by
using @EnableAutoConfiguration annotation. For example, if
MySQL database is on your classpath, but you have not
configured any database connection, then Spring Boot auto-
configures an in-memory database.

The entry point of the spring boot application is the class


contains @SpringBootApplication annotation and the main
method.

Spring Boot automatically scans all the components included in


the project by using @ComponentScan annotation.

Spring Boot Starters


Handling dependency management is a difficult task for big
projects. Spring Boot resolves this problem by providing a set of
dependencies for developers convenience.
For example, if you want to use Spring and JPA for database
access, it is sufficient if you include spring-boot-starter-data-
jpa dependency in your project.

Note that all Spring Boot starters follow the same naming
pattern spring-boot-starter- *, where * indicates that it is a type
of the application.

Examples

Look at the following Spring Boot starters explained below for a


better understanding −

Spring Boot Starter Actuator dependency is used to monitor


and manage your application. Its code is shown below −

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

Spring Boot Starter Security dependency is used for Spring


Security. Its code is shown below −

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

Spring Boot Starter web dependency is used to write a Rest


Endpoints. Its code is shown below −

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

Spring Boot Starter Thyme Leaf dependency is used to


create a web application. Its code is shown below −

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

Spring Boot Starter Test dependency is used for writing Test


cases. Its code is shown below −

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>

Auto Configuration
Spring Boot Auto Configuration automatically configures your
Spring application based on the JAR dependencies you added in
the project. For example, if MySQL database is on your class path,
but you have not configured any database connection, then
Spring Boot auto configures an in-memory database.

For this purpose, you need to


add @EnableAutoConfiguration annotation
or @SpringBootApplication annotation to your main class file.
Then, your Spring Boot application will be automatically
configured.

Observe the following code for a better understanding −

import [Link];
import
[Link]
uration;

@EnableAutoConfiguration
public class DemoApplication {
public static void main(String[] args) {
[Link]([Link],
args);
}
}

Spring Boot Application


The entry point of the Spring Boot Application is the class
contains @SpringBootApplication annotation. This class should
have the main method to run the Spring Boot
application. @SpringBootApplication annotation includes Auto-
Configuration, Component Scan, and Spring Boot Configuration.

If you added @SpringBootApplication annotation to the class,


you do not need to add the @EnableAutoConfiguration,
@ComponentScan and @SpringBootConfiguration annotation
. The @SpringBootApplication annotation includes all other
annotations.

Observe the following code for a better understanding −

import [Link];
import
[Link]
ation;

@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
[Link]([Link],
args);
}
}

Component Scan
Spring Boot application scans all the beans and package
declarations when the application initializes. You need to add
the @ComponentScan annotation for your class file to scan your
components added in your project.

Observe the following code for a better understanding −

import [Link];
import
[Link];

@ComponentScan
public class DemoApplication {
public static void main(String[] args) {
[Link]([Link],
args);
}
}

Spring Boot - Quick Start


This chapter will teach you how to create a Spring Boot
application using Maven and Gradle.

Prerequisites
Your system need to have the following minimum requirements to
create a Spring Boot application −

 Java 7
 Maven 3.2
 Gradle 2.5

Spring Boot CLI


The Spring Boot CLI is a command line tool and it allows us to run
the Groovy scripts. This is the easiest way to create a Spring Boot
application by using the Spring Boot Command Line Interface. You
can create, run and test the application in command prompt
itself.

This section explains you the steps involved in manual installation


of Spring Boot CLI. For further help, you can use the following
link: [Link]
docs/current-SNAPSHOT/reference/htmlsingle/#getting-started-
installing-springboot

You can also download the Spring CLI distribution from the Spring
Software repository
at: [Link]
rence/htmlsingle/#getting-started-manual-cli-installation

For manual installation, you need to use the following two folders

 [Link]
 [Link]

After the download, unpack the archive file and follow the steps
given in the [Link] file. Not that it does not require any
environment setup.

In Windows, go to the Spring Boot CLI bin directory in the


command prompt and run the command spring –-version to
make sure spring CLI is installed correctly. After executing the
command, you can see the spring CLI version as shown below −

Run Hello World with Groovy

Create a simple groovy file which contains the Rest Endpoint


script and run the groovy file with spring boot CLI. Observe the
code shown here for this purpose −

@Controller
class Example {
@RequestMapping("/")
@ResponseBody
public String hello() {
"Hello Spring Boot"
}
}

Now, save the groovy file with the name [Link]. Note that
in this example, we saved the groovy file inside the Spring Boot
CLI bin directory. Now run the application by using the
command spring run [Link] as shown in the screenshot
given below −

Once you run the groovy file, required dependencies will


download automatically and it will start the application in Tomcat
8080 port as shown in the screenshot given below −
Once Tomcat starts, go to the web browser and hit the
URL [Link] and you can see the output as
shown.

Spring Boot - Bootstrapping


This chapter will explain you how to perform bootstrapping on a
Spring Boot application.

Spring Initializer
One of the ways to Bootstrapping a Spring Boot application is by
using Spring Initializer. To do this, you will have to visit the Spring
Initializer web page [Link] and choose your Build,
Spring Boot Version and platform. Also, you need to provide a
Group, Artifact and required dependencies to run the application.

Observe the following screenshot that shows an example where


we added the spring-boot-starter-web dependency to write
REST Endpoints.
Once you provided the Group, Artifact, Dependencies, Build
Project, Platform and Version, click Generate Project button.
The zip file will download and the files will be extracted.

This section explains you the examples by using both Maven and
Gradle.

Maven
After you download the project, unzip the file. Now,
your [Link] file looks as shown below −

<?xml version = "1.0" encoding = "UTF-8"?>


<project xmlns = "[Link]
xmlns:xsi = "[Link]
instance"
xsi:schemaLocation =
"[Link]
[Link]

<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>demo</name>
<description>Demo project for Spring
Boot</description>

<parent>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-parent</artifactId>
<version>[Link]</version>
<relativePath/> <!-- lookup parent from
repository -->
</parent>

<properties>

<[Link]>UTF-8</[Link]
ceEncoding>
<[Link]>UTF-8</[Link]
[Link]>
<[Link]>1.8</[Link]>
</properties>

<dependencies>
<dependency>
<groupId>[Link]</groupId>

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

<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

Gradle
Once you download the project, unzip the file. Now
your [Link] file looks as shown below −

buildscript {
ext {
springBootVersion = '[Link]'
}
repositories {
mavenCentral()
}
dependencies {
classpath("[Link]:spring-boot-
gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: '[Link]'

group = '[Link]'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
mavenCentral()
}
dependencies {
compile('[Link]:spring-boot-
starter-web')
testCompile('[Link]:spring-boot-
starter-test')
}

Class Path Dependencies


Spring Boot provides a number of Starters to add the jars in our
class path. For example, for writing a Rest Endpoint, we need to
add the spring-boot-starter-web dependency in our class path.
Observe the codes shown below for a better understanding −

Maven dependency

<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
Gradle dependency

dependencies {
compile('[Link]:spring-boot-
starter-web')
}

Main Method
The main method should be writing the Spring Boot Application
class. This class should be annotated
with @SpringBootApplication. This is the entry point of the
spring boot application to start. You can find the main class file
under src/java/main directories with the default package.

In this example, the main class file is located at


the src/java/main directories with the default
package [Link]. Observe the code shown
here for a better understanding −

package [Link];

import [Link];
import
[Link]
ation;

@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
[Link]([Link],
args);
}
}

Write a Rest Endpoint


To write a simple Hello World Rest Endpoint in the Spring Boot
Application main class file itself, follow the steps shown below −
 Firstly, add the @RestController annotation at the top of
the class.
 Now, write a Request URI method
with @RequestMapping annotation.
 Then, the Request URI method should return the Hello
World string.

Now, your main Spring Boot Application class file will look like as
shown in the code given below −

package [Link];

import [Link];
import
[Link]
ation;
import
[Link];
import
[Link];

@SpringBootApplication
@RestController

public class DemoApplication {


public static void main(String[] args) {
[Link]([Link],
args);
}
@RequestMapping(value = "/")
public String hello() {
return "Hello World";
}
}

Create an Executable JAR


Let us create an executable JAR file to run the Spring Boot
application by using Maven and Gradle commands in the
command prompt as shown below −

Use the Maven command mvn clean install as shown below −


After executing the command, you can see the BUILD
SUCCESS message at the command prompt as shown below −

Use the Gradle command gradle clean build as shown below −

After executing the command, you can see the BUILD


SUCCESSFUL message in the command prompt as shown below

Run Hello World with Java


Once you have created an executable JAR file, you can find it
under the following directories.
For Maven, you can find the JAR file under the target directory as
shown below −

For Gradle, you can find the JAR file under the build/libs directory
as shown below −

Now, run the JAR file by using the command java –jar
<JARFILE>. Observe that in the above example, the JAR file is
named [Link]
Once you run the jar file, you can see the output in the console
window as shown below −

Now, look at the console, Tomcat started on port 8080 (http).


Now, go to the web browser and hit the
URL [Link] and you can see the output as
shown below −

Spring Boot - Tomcat


Deployment
By using Spring Boot application, we can create a war file to
deploy into the web server. In this chapter, you are going to learn
how to create a WAR file and deploy the Spring Boot application in
Tomcat web server.

Spring Boot Servlet Initializer


The traditional way of deployment is making the Spring Boot
Application @SpringBootApplication class extend
the SpringBootServletInitializer class. Spring Boot Servlet
Initializer class file allows you to configure the application when it
is launched by using Servlet Container.

The code for Spring Boot Application class file for JAR file
deployment is given below −
package [Link];

import [Link];
import
[Link]
ation;

@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
[Link]([Link],
args);
}
}

We need to extend the class SpringBootServletInitializer to


support WAR file deployment. The code of Spring Boot Application
class file is given below −

package [Link];

import [Link];
import
[Link]
ation;
import
[Link]
er;
import
[Link]
ServletInitializer;

@SpringBootApplication
public class DemoApplication extends
SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder
configure(SpringApplicationBuilder application) {
return
[Link]([Link]);
}
public static void main(String[] args) {
[Link]([Link],
args);
}
}

Setting Main Class


In Spring Boot, we need to mention the main class that should
start in the build file. For this purpose, you can use the following
pieces of code −

For Maven, add the start class in [Link] properties as shown


below −

<start-class>[Link]</start-class>

For Gradle, add the main class name in [Link] as shown


below −

mainClassName="[Link]"

Update packaging JAR into WAR


We have to update the packaging JAR into WAR using the
following pieces of code −

For Maven, add the packaging as WAR in [Link] as shown


below −

<packaging>war</packaging>

For Gradle, add the application plugin and war plugin in


the [Link] as shown below −

apply plugin: ‘war’


apply plugin: ‘application’

Now, let us write a simple Rest Endpoint to return the string


“Hello World from Tomcat”. To write a Rest Endpoint, we need to
add the Spring Boot web starter dependency into our build file.

For Maven, add the Spring Boot starter dependency in [Link]


using the code as shown below −
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

For Gradle, add the Spring Boot starter dependency


in [Link] using the code as shown below −

dependencies {
compile('[Link]:spring-boot-starter-web')
}

Now, write a simple Rest Endpoint in Spring Boot Application class


file using the code as shown below −

package [Link];

import [Link];
import
[Link]
ation;
import
[Link]
er;
import
[Link]
ServletInitializer;
import
[Link];
import
[Link];

@SpringBootApplication
@RestController
public class DemoApplication extends
SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder
configure(SpringApplicationBuilder application) {
return
[Link]([Link]);
}
public static void main(String[] args) {
[Link]([Link],
args);
}

@RequestMapping(value = "/")
public String hello() {
return "Hello World from Tomcat";
}
}

Packaging your Application


Now, create a WAR file to deploy into the Tomcat server by using
Maven and Gradle commands for packaging your application as
given below −

For Maven, use the command mvn package for packaging your
application. Then, the WAR file will be created and you can find it
in the target directory as shown in the screenshots given below −
For Gradle, use the command gradle clean build for packaging
your application. Then, your WAR file will be created and you can
find it under build/libs directory. Observe the screenshots given
here for a better understanding −
Deploy into Tomcat
Now, run the Tomcat Server, and deploy the WAR file under
the webapps directory. Observe the screenshots shown here for
a better understanding −
After successful deployment, hit the URL in your web
browser [Link] and
observe that the output will look as shown in the screenshot given
below −

The full code for this purpose is given below.


[Link]

<?xml version = "1.0" encoding = "UTF-8"?>


<project xmlns = "[Link]
xmlns:xsi = "[Link]
instance"

xsi:schemaLocation = "[Link]
[Link]
<modelVersion>4.0.0</modelVersion>

<groupId>[Link]</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>demo</name>
<description>Demo project for Spring
Boot</description>

<parent>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-parent</artifactId>
<version>[Link]</version>
<relativePath/> <!-- lookup parent from
repository -->
</parent>

<properties>

<[Link]>UTF-8</[Link]
ceEncoding>

<[Link]>UTF-8</[Link]
[Link]>
<[Link]>1.8</[Link]>
<start-
class>[Link]</start-
class>
</properties>

<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

[Link]

buildscript {
ext {
springBootVersion = '[Link]'
}
repositories {
mavenCentral()
}
dependencies {
classpath("[Link]:spring-boot-
gradle-plugin:${springBootVersion}")
}
}

apply plugin: 'java'


apply plugin: 'eclipse'
apply plugin: '[Link]'
apply plugin: 'war'
apply plugin: 'application'
group = '[Link]'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
mainClassName =
"[Link]"

repositories {
mavenCentral()
}
dependencies {
compile('[Link]:spring-boot-
starter-web')
testCompile('[Link]:spring-boot-
starter-test')
}

The code for main Spring Boot application class file is given below

package [Link];

import [Link];
import
[Link]
ation;
import
[Link]
er;
import
[Link]
ServletInitializer;
import
[Link];
import
[Link];

@SpringBootApplication
@RestController
public class DemoApplication extends
SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder
configure(SpringApplicationBuilder application) {
return
[Link]([Link]);
}
public static void main(String[] args) {
[Link]([Link],
args);
}

@RequestMapping(value = "/")
public String hello() {
return "Hello World from Tomcat";
}
}

Spring Boot - Build Systems


In Spring Boot, choosing a build system is an important task. We
recommend Maven or Gradle as they provide a good support for
dependency management. Spring does not support well other
build systems.

Dependency Management
Spring Boot team provides a list of dependencies to support the
Spring Boot version for its every release. You do not need to
provide a version for dependencies in the build configuration file.
Spring Boot automatically configures the dependencies version
based on the release. Remember that when you upgrade the
Spring Boot version, dependencies also will upgrade
automatically.

Note − If you want to specify the version for dependency, you


can specify it in your configuration file. However, the Spring Boot
team highly recommends that it is not needed to specify the
version for dependency.

Maven Dependency
For Maven configuration, we should inherit the Spring Boot Starter
parent project to manage the Spring Boot Starters dependencies.
For this, simply we can inherit the starter parent in
our [Link] file as shown below.

<parent>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>[Link]</version>
</parent>

We should specify the version number for Spring Boot Parent


Starter dependency. Then for other starter dependencies, we do
not need to specify the Spring Boot version number. Observe the
code given below −

<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

Gradle Dependency
We can import the Spring Boot Starters dependencies directly
into [Link] file. We do not need Spring Boot start Parent
dependency like Maven for Gradle. Observe the code given below

buildscript {
ext {
springBootVersion = '[Link]'
}
repositories {
mavenCentral()
}
dependencies {
classpath("[Link]:spring-boot-
gradle-plugin:${springBootVersion}")
}
}
Similarly, in Gradle, we need not specify the Spring Boot version
number for dependencies. Spring Boot automatically configures
the dependency based on the version.

dependencies {
compile('[Link]:spring-boot-
starter-web')
}

Spring Boot - Code Structure


Spring Boot does not have any code layout to work with.
However, there are some best practices that will help us. This
chapter talks about them in detail.

Default package
A class that does not have any package declaration is considered
as a default package. Note that generally a default package
declaration is not recommended. Spring Boot will cause issues
such as malfunctioning of Auto Configuration or Component Scan,
when you use default package.

Note − Java's recommended naming convention for package


declaration is reversed domain name. For example
− [Link]

Typical Layout
The typical layout of Spring Boot application is shown in the
image given below −
The [Link] file should declare the main method along
with @SpringBootApplication. Observe the code given below for a
better understanding −

package [Link];

import [Link];
import
[Link]
ation;

@SpringBootApplication
public class Application {
public static void main(String[] args)
{[Link]([Link], args);}
}

Beans and Dependency Injection


In Spring Boot, we can use Spring Framework to define our beans
and their dependency injection.
The @ComponentScan annotation is used to find beans and the
corresponding injected with @Autowired annotation.

If you followed the Spring Boot typical layout, no need to specify


any arguments for @ComponentScan annotation. All component
class files are automatically registered with Spring Beans.
The following example provides an idea about Auto wiring the
Rest Template object and creating a Bean for the same −

@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}

The following code shows the code for auto wired Rest Template
object and Bean creation object in main Spring Boot Application
class file −

package [Link];

import
[Link];
import [Link];
import
[Link]
ation;
import [Link];
import [Link];

@SpringBootApplication
public class DemoApplication {
@Autowired
RestTemplate restTemplate;

public static void main(String[] args) {


[Link]([Link],
args);
}
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}

Spring Boot - Runners


Application Runner and Command Line Runner interfaces lets you
to execute the code after the Spring Boot application is started.
You can use these interfaces to perform any actions immediately
after the application has started. This chapter talks about them in
detail.

Application Runner
Application Runner is an interface used to execute the code after
the Spring Boot application started. The example given below
shows how to implement the Application Runner interface on the
main class file.

package [Link];

import [Link];
import [Link];
import [Link];
import
[Link]
ation;

@SpringBootApplication
public class DemoApplication implements
ApplicationRunner {
public static void main(String[] args) {
[Link]([Link],
args);
}
@Override
public void run(ApplicationArguments arg0) throws
Exception {
[Link]("Hello World from Application
Runner");
}
}

Now, if you observe the console window below Hello World from
Application Runner, the println statement is executed after the
Tomcat started. Is the following screenshot relevant?
Command Line Runner
Command Line Runner is an interface. It is used to execute the
code after the Spring Boot application started. The example given
below shows how to implement the Command Line Runner
interface on the main class file.

package [Link];

import [Link];
import [Link];
import
[Link]
ation;

@SpringBootApplication
public class DemoApplication implements
CommandLineRunner {
public static void main(String[] args) {
[Link]([Link],
args);
}
@Override
public void run(String... arg0) throws Exception {
[Link]("Hello world from Command Line
Runner");
}
}

Look at the console window below “Hello world from Command


Line Runner” println statement is executed after the Tomcat
started.

Spring Boot - Application


Properties
Application Properties support us to work in different
environments. In this chapter, you are going to learn how to
configure and specify the properties to a Spring Boot application.
Command Line Properties
Spring Boot application converts the command line properties into
Spring Boot Environment properties. Command line properties
take precedence over the other property sources. By default,
Spring Boot uses the 8080 port number to start the Tomcat. Let
us learn how change the port number by using command line
properties.

Step 1 − After creating an executable JAR file, run it by using the


command java –jar <JARFILE>.

Step 2 − Use the command given in the screenshot given below


to change the port number for Spring Boot application by using
command line properties.

Note − You can provide more than one application properties by


using the delimiter −.

Properties File
Properties files are used to keep ‘N’ number of properties in a
single file to run the application in a different environment. In
Spring Boot, properties are kept in
the [Link] file under the classpath.

The [Link] file is located in


the src/main/resources directory. The code for
sample [Link] file is given below −

[Link] = 9090
[Link] = demoservice

Note that in the code shown above the Spring Boot application
demoservice starts on the port 9090.

YAML File
Spring Boot supports YAML based properties configurations to run
the application. Instead of [Link], we can
use [Link] file. This YAML file also should be kept
inside the classpath. The sample [Link] file is given
below −

spring:
application:
name: demoservice
server:
port: 9090

Externalized Properties
Instead of keeping the properties file under classpath, we can
keep the properties in different location or path. While running
the JAR file, we can specify the properties file path. You can use
the following command to specify the location of properties file
while running the JAR −

-[Link] = C:\[Link]

Use of @Value Annotation


The @Value annotation is used to read the environment or
application property value in Java code. The syntax to read the
property value is shown below −

@Value("${property_key_name}")

Look at the following example that shows the syntax to read


the [Link] property value in Java variable by
using @Value annotation.

@Value("${[Link]}")

Observe the code given below for a better understanding −

import
[Link];
import [Link];
import
[Link]
ation;
import
[Link];
import
[Link];

@SpringBootApplication
@RestController
public class DemoApplication {
@Value("${[Link]}")
private String name;
public static void main(String[] args) {
[Link]([Link],
args);
}
@RequestMapping(value = "/")
public String name() {
return name;
}
}

Note − If the property is not found while running the application,


Spring Boot throws the Illegal Argument exception as Could not
resolve placeholder '[Link]' in value "$
{[Link]}".

To resolve the placeholder issue, we can set the default value for
the property using thr syntax given below −

@Value("${property_key_name:default_value}")

@Value("${[Link]:demoservice}")

Spring Boot Active Profile


Spring Boot supports different properties based on the Spring
active profile. For example, we can keep two separate files for
development and production to run the Spring Boot application.

Spring active profile in [Link]


Let us understand how to have Spring active profile in
[Link]. By default, application. properties will be
used to run the Spring Boot application. If you want to use profile
based properties, we can keep separate properties file for each
profile as shown below −

[Link]

[Link] = 8080
[Link] = demoservice

[Link]

[Link] = 9090
[Link] = demoservice

[Link]

[Link] = 4431
[Link] = demoservice

While running the JAR file, we need to specify the spring active
profile based on each properties file. By default, Spring Boot
application uses the [Link] file. The command to
set the spring active profile is shown below −

You can see active profile name on the console log as shown
below −

2017-11-26 [Link].322 INFO 14028 --- [


main] [Link] :
The following profiles are active: dev

Now, Tomcat has started on the port 9090 (http) as shown below

2017-11-26 [Link].185 INFO 14028 --- [


main] [Link] :
Tomcat started on port(s): 9090 (http)
You can set the Production active profile as shown below −

You can see active profile name on the console log as shown
below −

2017-11-26 [Link].322 INFO 14028 --- [


main] [Link] :
The following profiles are active: prod

Now, Tomcat started on the port 4431 (http) as shown below −

2017-11-26 [Link].185 INFO 14028 --- [


main] [Link] :
Tomcat started on port(s): 4431 (http)

Spring active profile for [Link]

Let us understand how to keep Spring active profile for


[Link]. We can keep the Spring active profile properties
in the single [Link] file. No need to use the separate
file like [Link].

The following is an example code to keep the Spring active


profiles in [Link] file. Note that the delimiter (---) is used
to separate each profile in [Link] file.

spring:
application:
name: demoservice
server:
port: 8080

---
spring:
profiles: dev
application:
name: demoservice
server:
port: 9090

---
spring:
profiles: prod
application:
name: demoservice
server:
port: 4431

To command to set development active profile is given below −

You can see active profile name on the console log as shown
below −

2017-11-26 [Link].202 INFO 14104 --- [


main] [Link] :
The following profiles are active: dev

Now, Tomcat started on the port 9090 (http) as shown below −

2017-11-26 [Link].650 INFO 14104 --- [


main] [Link] :
Tomcat started on port(s): 9090 (http)

The command to set Production active profile is given below −

You can see active profile name on the console log as shown
below −

2017-11-26 [Link].743 INFO 13400 --- [


main] [Link] :
The following profiles are active: prod
This will start Tomcat on the port 4431 (http) as shown below:

2017-11-26 [Link].473 INFO 13400 --- [


main] [Link] :
Tomcat started on port(s): 4431 (http)

Spring Boot - Logging


Spring Boot uses Apache Commons logging for all internal
logging. Spring Boot’s default configurations provides a support
for the use of Java Util Logging, Log4j2, and Logback. Using these,
we can configure the console logging as well as file logging.

If you are using Spring Boot Starters, Logback will provide a good
support for logging. Besides, Logback also provides a use of good
support for Common Logging, Util Logging, Log4J, and SLF4J.

Log Format
The default Spring Boot Log format is shown in the screenshot
given below.

which gives you the following information −

 Date and Time that gives the date and time of the log
 Log level shows INFO, ERROR or WARN
 Process ID
 The --- which is a separator
 Thread name is enclosed within the square brackets []
 Logger Name that shows the Source class name
 The Log message

Console Log Output


The default log messages will print to the console window. By
default, “INFO”, “ERROR” and “WARN” log messages will print in
the log file.
If you have to enable the debug level log, add the debug flag on
starting your application using the command shown below −

java –jar [Link] --debug

You can also add the debug mode to your [Link]


file as shown here −

debug = true

File Log Output


By default, all logs will print on the console window and not in the
files. If you want to print the logs in a file, you need to set the
property [Link] or [Link] in the
[Link] file.

You can specify the log file path using the property shown below.
Note that the log file name is [Link].

[Link] = /var/tmp/

You can specify the own log file name using the property shown
below −

[Link] = /var/tmp/[Link]

Note − files will rotate automatically after reaching the size 10


MB.

Log Levels
Spring Boot supports all logger levels such as “TRACE”, “DEBUG”,
“INFO”, “WARN”, “ERROR”, “FATAL”, “OFF”. You can define Root
logger in the [Link] file as shown below −

[Link] = WARN

Note − Logback does not support “FATAL” level log. It is mapped


to the “ERROR” level log.

Configure Logback
Logback supports XML based configuration to handle Spring Boot
Log configurations. Logging configuration details are configured
in [Link] file. The [Link] file should be placed under
the classpath.

You can configure the ROOT level log in [Link] file using the
code given below −

<?xml version = "1.0" encoding = "UTF-8"?>


<configuration>
<root level = "INFO">
</root>
</configuration>

You can configure the console appender in [Link] file given


below.

<?xml version = "1.0" encoding = "UTF-8"?>


<configuration>
<appender name = "STDOUT" class =
"[Link]"></appender>
<root level = "INFO">
<appender-ref ref = "STDOUT"/>
</root>
</configuration>

You can configure the file appender in [Link] file using the
code given below. Note that you need to specify the Log file path
insider the file appender.

<?xml version = "1.0" encoding = "UTF-8"?>


<configuration>
<appender name = "FILE" class =
"[Link]">
<File>/var/tmp/[Link]</File>
</appender>
<root level = "INFO">
<appender-ref ref = "FILE"/>
</root>
</configuration>
You can define the Log pattern in [Link] file using the code
given below. You can also define the set of supported log patterns
inside the console or file log appender using the code given below

<pattern>[%d{yyyy-MM-dd'T'HH:mm:[Link]'Z'}] [%C] [%t] [%L] [%-5p] %m


%n</pattern>

The code for complete [Link] file is given below. You have
to place this in the class path.

<?xml version = "1.0" encoding = "UTF-8"?>


<configuration>
<appender name = "STDOUT" class =
"[Link]">
<encoder>
<pattern>[%d{yyyy-MM-dd'T'HH:mm:[Link]'Z'}]
[%C] [%t] [%L] [%-5p] %m%n</pattern>
</encoder>
</appender>

<appender name = "FILE" class =


"[Link]">
<File>/var/tmp/[Link]</File>
<encoder>
<pattern>[%d{yyyy-MM-dd'T'HH:mm:[Link]'Z'}]
[%C] [%t] [%L] [%-5p] %m%n</pattern>
</encoder>
</appender>

<root level = "INFO">


<appender-ref ref = "FILE"/>
<appender-ref ref = "STDOUT"/>
</root>
</configuration>

The code given below shows how to add the slf4j logger in Spring
Boot main class file.

package [Link];

import [Link];
import [Link];
import [Link];
import
[Link]
ation;

@SpringBootApplication
public class DemoApplication {
private static final Logger logger =
[Link]([Link]);

public static void main(String[] args) {


[Link]("this is a info message");
[Link]("this is a warn message");
[Link]("this is a error message");
[Link]([Link],
args);
}
}

The output that you can see in the console window is shown here

The output that you can see in the log file is shown here −

Spring Boot - Building RESTful


Web Services
Spring Boot provides a very good support to building RESTful Web
Services for enterprise applications. This chapter will explain in
detail about building RESTful web services using Spring Boot.

Note − For building a RESTful Web Services, we need to add the


Spring Boot Starter Web dependency into the build configuration
file.
If you are a Maven user, use the following code to add the below
dependency in your [Link] file −

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

If you are a Gradle user, use the following code to add the below
dependency in your [Link] file.

compile('[Link]:spring-boot-starter-web')

The code for complete build configuration file Maven build –


[Link] is given below −

<?xml version = "1.0" encoding = "UTF-8"?>


<project xmlns = "[Link]
xmlns:xsi = "[Link]
instance"
xsi:schemaLocation =
"[Link]
[Link]

<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring
Boot</description>

<parent>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-parent</artifactId>
<version>[Link]</version>
<relativePath/>
</parent>

<properties>
<[Link]>UTF-8</[Link]
ceEncoding>

<[Link]>UTF-8</[Link]
[Link]>
<[Link]>1.8</[Link]>
</properties>

<dependencies>
<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

The code for complete build configuration file Gradle Build –


[Link] is given below −

buildscript {
ext {
springBootVersion = '[Link]'
}
repositories {
mavenCentral()
}
dependencies {
classpath("[Link]:spring-boot-
gradle-plugin:${springBootVersion}")
}
}

apply plugin: 'java'


apply plugin: 'eclipse'
apply plugin: '[Link]'

group = '[Link]'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
mavenCentral()
}
dependencies {
compile('[Link]:spring-boot-
starter-web')
testCompile('[Link]:spring-boot-
starter-test')
}

Before you proceed to build a RESTful web service, it is suggested


that you have knowledge of the following annotations −

Rest Controller
The @RestController annotation is used to define the RESTful web
services. It serves JSON, XML and custom response. Its syntax is
shown below −

@RestController
public class ProductServiceController {
}

Request Mapping
The @RequestMapping annotation is used to define the Request
URI to access the REST Endpoints. We can define Request method
to consume and produce object. The default request method is
GET.

@RequestMapping(value = "/products")
public ResponseEntity<Object> getProducts() { }

Request Body
The @RequestBody annotation is used to define the request body
content type.

public ResponseEntity<Object> createProduct(@RequestBody Product


product) {
}

Path Variable
The @PathVariable annotation is used to define the custom or
dynamic request URI. The Path variable in request URI is defined
as curly braces {} as shown below −

public ResponseEntity<Object> updateProduct(@PathVariable("id") String id)


{
}

Request Parameter
The @RequestParam annotation is used to read the request
parameters from the Request URL. By default, it is a required
parameter. We can also set default value for request parameters
as shown here −

public ResponseEntity<Object> getProduct(


@RequestParam(value = "name", required = false, defaultValue = "honey")
String name) {
}

GET API
The default HTTP request method is GET. This method does not
require any Request Body. You can send request parameters and
path variables to define the custom or dynamic URL.
The sample code to define the HTTP GET request method is
shown below. In this example, we used HashMap to store the
Product. Note that we used a POJO class as the product to be
stored.

Here, the request URI is /products and it will return the list of
products from HashMap repository. The controller class file is
given below that contains GET method REST Endpoint.

package [Link];

import [Link];
import [Link];

import [Link];
import [Link];
import
[Link];
import
[Link];

import [Link];

@RestController
public class ProductServiceController {
private static Map<String, Product> productRepo =
new HashMap<>();
static {
Product honey = new Product();
[Link]("1");
[Link]("Honey");
[Link]([Link](), honey);

Product almond = new Product();


[Link]("2");
[Link]("Almond");
[Link]([Link](), almond);
}
@RequestMapping(value = "/products")
public ResponseEntity<Object> getProduct() {
return new ResponseEntity<>([Link](),
[Link]);
}
}
POST API
The HTTP POST request is used to create a resource. This method
contains the Request Body. We can send request parameters and
path variables to define the custom or dynamic URL.

The following example shows the sample code to define the HTTP
POST request method. In this example, we used HashMap to store
the Product, where the product is a POJO class.

Here, the request URI is /products, and it will return the String
after storing the product into HashMap repository.

package [Link];

import [Link];
import [Link];

import [Link];
import [Link];
import
[Link];
import
[Link];
import
[Link];
import
[Link];

import [Link];

@RestController
public class ProductServiceController {
private static Map<String, Product> productRepo =
new HashMap<>();

@RequestMapping(value = "/products", method =


[Link])
public ResponseEntity<Object>
createProduct(@RequestBody Product product) {
[Link]([Link](), product);
return new ResponseEntity<>("Product is created
successfully", [Link]);
}
}

PUT API
The HTTP PUT request is used to update the existing resource.
This method contains a Request Body. We can send request
parameters and path variables to define the custom or dynamic
URL.

The example given below shows how to define the HTTP PUT
request method. In this example, we used HashMap to update the
existing Product, where the product is a POJO class.

Here the request URI is /products/{id} which will return the


String after a the product into a HashMap repository. Note that we
used the Path variable {id} which defines the products ID that
needs to be updated.

package [Link];

import [Link];
import [Link];

import [Link];
import [Link];
import
[Link];
import
[Link];
import
[Link];
import
[Link];
import
[Link];
import [Link];

@RestController
public class ProductServiceController {
private static Map<String, Product> productRepo =
new HashMap<>();
@RequestMapping(value = "/products/{id}", method =
[Link])
public ResponseEntity<Object>
updateProduct(@PathVariable("id") String id,
@RequestBody Product product) {
[Link](id);
[Link](id);
[Link](id, product);
return new ResponseEntity<>("Product is updated
successsfully", [Link]);
}
}

DELETE API
The HTTP Delete request is used to delete the existing resource.
This method does not contain any Request Body. We can send
request parameters and path variables to define the custom or
dynamic URL.

The example given below shows how to define the HTTP DELETE
request method. In this example, we used HashMap to remove
the existing product, which is a POJO class.

The request URI is /products/{id} and it will return the String


after deleting the product from HashMap repository. We used the
Path variable {id} which defines the products ID that needs to be
deleted.

package [Link];

import [Link];
import [Link];

import [Link];
import [Link];
import
[Link];
import
[Link];
import
[Link];
import
[Link];
import
[Link];

import [Link];

@RestController
public class ProductServiceController {
private static Map<String, Product> productRepo =
new HashMap<>();

@RequestMapping(value = "/products/{id}", method =


[Link])
public ResponseEntity<Object>
delete(@PathVariable("id") String id) {
[Link](id);
return new ResponseEntity<>("Product is deleted
successsfully", [Link]);
}
}

This section gives you the complete set of source code. Observe
the following codes for their respective functionalities −

The Spring Boot main application class –


[Link]

package [Link];

import [Link];
import
[Link]
ation;

@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
[Link]([Link],
args);
}
}

The POJO class – [Link]

package [Link];

public class Product {


private String id;
private String name;

public String getId() {


return id;
}
public void setId(String id) {
[Link] = id;
}
public String getName() {
return name;
}
public void setName(String name) {
[Link] = name;
}
}

The Rest Controller class – [Link]

package [Link];

import [Link];
import [Link];

import [Link];
import [Link];
import
[Link];
import
[Link];
import
[Link];
import
[Link];
import
[Link];

import [Link];

@RestController
public class ProductServiceController {
private static Map<String, Product> productRepo =
new HashMap<>();
static {
Product honey = new Product();
[Link]("1");
[Link]("Honey");
[Link]([Link](), honey);

Product almond = new Product();


[Link]("2");
[Link]("Almond");
[Link]([Link](), almond);
}

@RequestMapping(value = "/products/{id}", method =


[Link])
public ResponseEntity<Object>
delete(@PathVariable("id") String id) {
[Link](id);
return new ResponseEntity<>("Product is deleted
successsfully", [Link]);
}

@RequestMapping(value = "/products/{id}", method =


[Link])
public ResponseEntity<Object>
updateProduct(@PathVariable("id") String id,
@RequestBody Product product) {
[Link](id);
[Link](id);
[Link](id, product);
return new ResponseEntity<>("Product is updated
successsfully", [Link]);
}
@RequestMapping(value = "/products", method =
[Link])
public ResponseEntity<Object>
createProduct(@RequestBody Product product) {
[Link]([Link](), product);
return new ResponseEntity<>("Product is created
successfully", [Link]);
}

@RequestMapping(value = "/products")
public ResponseEntity<Object> getProduct() {
return new ResponseEntity<>([Link](),
[Link]);
}
}

You can create an executable JAR file, and run the spring boot
application by using the below Maven or Gradle commands as
shown −

For Maven, use the command shown below −

mvn clean install

After “BUILD SUCCESS”, you can find the JAR file under the target
directory.

For Gradle, use the command shown below −

gradle clean build

After “BUILD SUCCESSFUL”, you can find the JAR file under the
build/libs directory.

You can run the JAR file by using the command shown below −

java –jar <JARFILE>

This will start the application on the Tomcat port 8080 as shown
below −
Now hit the URL shown below in POSTMAN application and see the
output.

GET API URL is: [Link]

POST API URL is: [Link]

PUT API URL is: [Link]


DELETE API URL is: [Link]

Spring Boot - Exception Handling


Handling exceptions and errors in APIs and sending the proper
response to the client is good for enterprise applications. In this
chapter, we will learn how to handle exceptions in Spring Boot.

Before proceeding with exception handling, let us gain an


understanding on the following annotations.

Controller Advice
The @ControllerAdvice is an annotation, to handle the exceptions
globally.
Exception Handler
The @ExceptionHandler is an annotation used to handle the
specific exceptions and sending the custom responses to the
client.

You can use the following code to create @ControllerAdvice class


to handle the exceptions globally −

package [Link];

import
[Link]
e;

@ControllerAdvice
public class ProductExceptionController {
}

Define a class that extends the RuntimeException class.

package [Link];

public class ProductNotfoundException extends


RuntimeException {
private static final long serialVersionUID = 1L;
}

You can define the @ExceptionHandler method to handle the


exceptions as shown. This method should be used for writing the
Controller Advice class file.

@ExceptionHandler(value =
[Link])

public ResponseEntity<Object>
exception(ProductNotfoundException exception) {
}
Now, use the code given below to throw the exception from the
API.

@RequestMapping(value = "/products/{id}", method =


[Link])
public ResponseEntity<Object> updateProduct() {
throw new ProductNotfoundException();
}

The complete code to handle the exception is given below. In this


example, we used the PUT API to update the product. Here, while
updating the product, if the product is not found, then return the
response error message as “Product not found”. Note that
the ProductNotFoundException exception class should extend
the RuntimeException.

package [Link];
public class ProductNotfoundException extends
RuntimeException {
private static final long serialVersionUID = 1L;
}

The Controller Advice class to handle the exception globally is


given below. We can define any Exception Handler methods in
this class file.

package [Link];

import [Link];
import [Link];
import
[Link]
e;
import
[Link]
r;

@ControllerAdvice
public class ProductExceptionController {
@ExceptionHandler(value =
[Link])
public ResponseEntity<Object>
exception(ProductNotfoundException exception) {
return new ResponseEntity<>("Product not found",
HttpStatus.NOT_FOUND);
}
}

The Product Service API controller file is given below to update


the Product. If the Product is not found, then it throws
the ProductNotFoundException class.

package [Link];

import [Link];
import [Link];

import [Link];
import [Link];
import
[Link];
import
[Link];
import
[Link];
import
[Link];
import
[Link];

import
[Link]
ion;
import [Link];

@RestController
public class ProductServiceController {
private static Map<String, Product> productRepo =
new HashMap<>();
static {
Product honey = new Product();
[Link]("1");
[Link]("Honey");
[Link]([Link](), honey);

Product almond = new Product();


[Link]("2");
[Link]("Almond");
[Link]([Link](), almond);
}

@RequestMapping(value = "/products/{id}", method =


[Link])
public ResponseEntity<Object>
updateProduct(@PathVariable("id") String id,
@RequestBody Product product) {
if(![Link](id))throw new
ProductNotfoundException();
[Link](id);
[Link](id);
[Link](id, product);
return new ResponseEntity<>("Product is updated
successfully", [Link]);
}
}

The code for main Spring Boot application class file is given below

package [Link];

import [Link];
import
[Link]
ation;

@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
[Link]([Link],
args);
}
}
The code for POJO class for Product is given below −

package [Link];
public class Product {
private String id;
private String name;

public String getId() {


return id;
}
public void setId(String id) {
[Link] = id;
}
public String getName() {
return name;
}
public void setName(String name) {
[Link] = name;
}
}

The code for Maven build – [Link] is shown below −

<?xml version = "1.0" encoding = "UTF-8"?>


<project xmlns = "[Link]
xmlns:xsi = "[Link]
instance"
xsi:schemaLocation =
"[Link]
[Link]

<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring
Boot</description>

<parent>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>[Link]</version>
<relativePath/>
</parent>

<properties>

<[Link]>UTF-8</[Link]
ceEncoding>

<[Link]>UTF-8</[Link]
[Link]>
<[Link]>1.8</[Link]>
</properties>

<dependencies>
<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

The code for Gradle Build – [Link] is given below −


buildscript {
ext {
springBootVersion = '[Link]'
}
repositories {
mavenCentral()
}
dependencies {
classpath("[Link]:spring-boot-
gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: '[Link]'

group = '[Link]'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
mavenCentral()
}
dependencies {
compile('[Link]:spring-boot-
starter-web')
testCompile('[Link]:spring-boot-
starter-test')
}

You can create an executable JAR file, and run the Spring Boot
application by using the Maven or Gradle commands −

For Maven, you can use the following command −

mvn clean install

After “BUILD SUCCESS”, you can find the JAR file under the target
directory.

For Gradle, you can use the following command −

gradle clean build


After “BUILD SUCCESSFUL”, you can find the JAR file under the
build/libs directory.

You can run the JAR file by using the following command −

java –jar <JARFILE>

This will start the application on the Tomcat port 8080 as shown
below −

Now hit the below URL in POSTMAN application and you can see
the output as shown below −

Update URL: [Link]

Spring Boot - Interceptor


You can use the Interceptor in Spring Boot to perform operations
under the following situations −

 Before sending the request to the controller


 Before sending the response to the client

For example, you can use an interceptor to add the request


header before sending the request to the controller and add the
response header before sending the response to the client.
To work with interceptor, you need to create @Component class
that supports it and it should implement
the HandlerInterceptor interface.

The following are the three methods you should know about while
working on Interceptors −

 preHandle() method − This is used to perform operations


before sending the request to the controller. This method
should return true to return the response to the client.
 postHandle() method − This is used to perform operations
before sending the response to the client.
 afterCompletion() method − This is used to perform
operations after completing the request and response.

Observe the following code for a better understanding −

@Component
public class ProductServiceInterceptor implements
HandlerInterceptor {
@Override
public boolean preHandle(
HttpServletRequest request, HttpServletResponse
response, Object handler) throws Exception {

return true;
}
@Override
public void postHandle(
HttpServletRequest request, HttpServletResponse
response, Object handler,
ModelAndView modelAndView) throws Exception {}

@Override
public void afterCompletion(HttpServletRequest
request, HttpServletResponse response,
Object handler, Exception exception) throws
Exception {}
}

You will have to register this Interceptor


with InterceptorRegistry by
using WebMvcConfigurerAdapter as shown below −
@Component
public class ProductServiceInterceptorAppConfig extends
WebMvcConfigurerAdapter {
@Autowired
ProductServiceInterceptor productServiceInterceptor;

@Override
public void addInterceptors(InterceptorRegistry
registry) {

[Link](productServiceInterceptor);
}
}

In the example given below, we are going to hit the GET products
API which gives the output as given under −

The code for the Interceptor class [Link]


is given below −

package [Link];

import [Link];
import [Link];

import [Link];
import
[Link];
import [Link];

@Component
public class ProductServiceInterceptor implements
HandlerInterceptor {
@Override
public boolean preHandle
(HttpServletRequest request, HttpServletResponse
response, Object handler)
throws Exception {

[Link]("Pre Handle method is


Calling");
return true;
}
@Override
public void postHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler, ModelAndView modelAndView) throws
Exception {

[Link]("Post Handle method is


Calling");
}
@Override
public void afterCompletion
(HttpServletRequest request, HttpServletResponse
response, Object
handler, Exception exception) throws Exception {

[Link]("Request and Response is


completed");
}
}

The code for Application Configuration class file to register the


Interceptor into Interceptor Registry –
[Link] is given below −

package [Link];

import
[Link];
import [Link];
import
[Link]
ceptorRegistry;
import
[Link]
cConfigurerAdapter;

@Component
public class ProductServiceInterceptorAppConfig extends
WebMvcConfigurerAdapter {
@Autowired
ProductServiceInterceptor productServiceInterceptor;
@Override
public void addInterceptors(InterceptorRegistry
registry) {

[Link](productServiceInterceptor);
}
}

The code for Controller class file [Link] is


given below −

package [Link];

import [Link];
import [Link];

import [Link];
import [Link];
import
[Link];
import
[Link];
import
[Link];
import
[Link];
import
[Link];
import
[Link]
ion;
import [Link];

@RestController
public class ProductServiceController {
private static Map<String, Product> productRepo =
new HashMap<>();
static {
Product honey = new Product();
[Link]("1");
[Link]("Honey");
[Link]([Link](), honey);
Product almond = new Product();
[Link]("2");
[Link]("Almond");
[Link]([Link](), almond);
}
@RequestMapping(value = "/products")
public ResponseEntity<Object> getProduct() {
return new ResponseEntity<>([Link](),
[Link]);
}
}

The code for POJO class for [Link] is given below −

package [Link];

public class Product {


private String id;
private String name;

public String getId() {


return id;
}
public void setId(String id) {
[Link] = id;
}
public String getName() {
return name;
}
public void setName(String name) {
[Link] = name;
}
}

The code for main Spring Boot application class


file [Link] is given below −

package [Link];

import [Link];
import
[Link]
ation;

@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
[Link]([Link],
args);
}
}

The code for Maven build – [Link] is shown here −

<?xml version = "1.0" encoding = "UTF-8"?>


<project xmlns = "[Link]
xmlns:xsi = "
[Link]
xsi:schemaLocation =
"[Link]
[Link]

<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring
Boot</description>

<parent>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-parent</artifactId>
<version>[Link]</version>
<relativePath/>
</parent>

<properties>
<[Link]>UTF-8</[Link]
ceEncoding>

<[Link]>UTF-8</[Link]
[Link]>
<[Link]>1.8</[Link]>
</properties>

<dependencies>
<dependency>
<groupId>[Link]</groupId>

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

<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

The code for Gradle Build [Link] is shown here −

buildscript {
ext {
springBootVersion = '[Link]'
}
repositories {
mavenCentral()
}
dependencies {
classpath("[Link]:spring-boot-
gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: '[Link]'
group = '[Link]'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
mavenCentral()
}
dependencies {
compile('[Link]:spring-boot-
starter-web')
testCompile('[Link]:spring-boot-
starter-test')
}

You can create an executable JAR file, and run the Spring Boot
application by using the below Maven or Gradle commands.

For Maven, use the command as shown below −

mvn clean install

After “BUILD SUCCESS”, you can find the JAR file under the target
directory.

For Gradle, use the command as shown below −

gradle clean build

After “BUILD SUCCESSFUL”, you can find the JAR file under the
build/libs directory.

You can run the JAR file by using the following command −

java –jar <JARFILE>


Now, the application has started on the Tomcat port 8080 as
shown below −

Now hit the below URL in POSTMAN application and you can see
the output as shown under −

GET API: [Link]

In the console window, you can see the [Link]


statements added in the Interceptor as shown in the screenshot
given below −

Spring Boot - Servlet Filter


A filter is an object used to intercept the HTTP requests and
responses of your application. By using filter, we can perform two
operations at two instances −

 Before sending the request to the controller


 Before sending a response to the client.

The following code shows the sample code for a Servlet Filter
implementation class with @Component annotation.

@Component
public class SimpleFilter implements Filter {
@Override
public void destroy() {}

@Override
public void doFilter
(ServletRequest request, ServletResponse
response, FilterChain filterchain)
throws IOException, ServletException {}

@Override
public void init(FilterConfig filterconfig) throws
ServletException {}
}

The following example shows the code for reading the remote
host and remote address from the ServletRequest object before
sending the request to the controller.

In doFilter() method, we have added the [Link]


statements to print the remote host and remote address.

package [Link];

import [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

import [Link];

@Component
public class SimpleFilter implements Filter {
@Override
public void destroy() {}

@Override
public void doFilter(ServletRequest request,
ServletResponse response, FilterChain filterchain)
throws IOException, ServletException {

[Link]("Remote
Host:"+[Link]());
[Link]("Remote
Address:"+[Link]());
[Link](request, response);
}

@Override
public void init(FilterConfig filterconfig) throws
ServletException {}
}

In the Spring Boot main application class file, we have added the
simple REST endpoint that returns the “Hello World” string.

package [Link];

import [Link];
import
[Link]
ation;
import
[Link];
import
[Link];

@SpringBootApplication
@RestController
public class DemoApplication {
public static void main(String[] args) {
[Link]([Link],
args);
}
@RequestMapping(value = "/")
public String hello() {
return "Hello World";
}
}

The code for Maven build – [Link] is given below −

<?xml version = "1.0" encoding = "UTF-8"?>


<project xmlns = "[Link]
xmlns:xsi = "
[Link]
xsi:schemaLocation =
"[Link]
[Link]

<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring
Boot</description>

<parent>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-parent</artifactId>
<version>[Link]</version>
<relativePath/>
</parent>

<properties>

<[Link]>UTF-8</[Link]
ceEncoding>

<[Link]>UTF-8</[Link]
[Link]>
<[Link]>1.8</[Link]>
</properties>
<dependencies>
<dependency>
<groupId>[Link]</groupId>

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

<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

The code for Gradle Build – [Link] is given below −

buildscript {
ext {
springBootVersion = '[Link]'
}
repositories {
mavenCentral()
}
dependencies {
classpath("[Link]:spring-boot-
gradle-plugin:${springBootVersion}")
}
}

apply plugin: 'java'


apply plugin: 'eclipse'
apply plugin: '[Link]'

group = '[Link]'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
mavenCentral()
}
dependencies {
compile('[Link]:spring-boot-
starter-web')
testCompile('[Link]:spring-boot-
starter-test')
}

You can create an executable JAR file, and run the Spring Boot
application by using the Maven or Gradle commands shown below

For Maven, use the command as shown below −

mvn clean install

After BUILD SUCCESS, you can find the JAR file under the target
directory.

For Gradle, use the command as shown below −

gradle clean build

After BUILD SUCCESSFUL, you can find the JAR file under the
build/libs directory.

Now, run the JAR file by using the following command

java –jar <JARFILE>

You can see the application has started on the Tomcat port 8080.

Now hit the URL [Link] and see the output Hello
World. It should look as shown below −
Then, you can see the Remote host and Remote address on the
console log as shown below −

Spring Boot - Tomcat Port


Number
Spring Boot lets you to run the same application more than once
on a different port number. In this chapter, you will learn about
this in detail. Note that the default port number 8080.

Custom Port
In the [Link] file, we can set custom port
number for the property [Link]

[Link] = 9090

In the [Link] file, you can find as follows −

server:
port: 9090

Random Port
In the [Link] file, we can set random port
number for the property [Link]

[Link] = 0
In the [Link] file, you can find as follows −

server:
port: 0

Note − If the [Link] number is 0 while starting the Spring


Boot application, Tomcat uses the random port number.

Spring Boot - Rest Template


Rest Template is used to create applications that consume
RESTful Web Services. You can use the exchange() method to
consume the web services for all HTTP methods. The code given
below shows how to create Bean for Rest Template to auto wiring
the Rest Template object.

package [Link];

import [Link];
import
[Link]
ation;
import [Link];
import [Link];

@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
[Link]([Link],
args);
}
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}

GET
Consuming the GET API by using RestTemplate -
exchange() method
Assume this URL [Link] returns the
following JSON and we are going to consume this API response by
using Rest Template using the following code −

[
{
"id": "1",
"name": "Honey"
},
{
"id": "2",
"name": "Almond"
}
]

You will have to follow the given points to consume the API −

 Autowired the Rest Template Object.


 Use HttpHeaders to set the Request Headers.
 Use HttpEntity to wrap the request object.
 Provide the URL, HttpMethod, and Return type for
Exchange() method.
@RestController
public class ConsumeWebService {
@Autowired
RestTemplate restTemplate;

@RequestMapping(value = "/template/products")
public String getProductList() {
HttpHeaders headers = new HttpHeaders();

[Link]([Link](MediaType.APPLICATION_J
SON));
HttpEntity <String> entity = new
HttpEntity<String>(headers);

return [Link]("
[Link]
[Link], entity, [Link]).getBody();
}
}
POST
Consuming POST API by using RestTemplate - exchange()
method

Assume this URL [Link] returns the


response shown below, we are going to consume this API
response by using the Rest Template.

The code given below is the Request body −

{
"id":"3",
"name":"Ginger"
}

The code given below is the Response body −

Product is created successfully

You will have to follow the points given below to consume the API

 Autowired the Rest Template Object.


 Use the HttpHeaders to set the Request Headers.
 Use the HttpEntity to wrap the request object. Here, we wrap
the Product object to send it to the request body.
 Provide the URL, HttpMethod, and Return type for
exchange() method.
@RestController
public class ConsumeWebService {
@Autowired
RestTemplate restTemplate;

@RequestMapping(value = "/template/products", method


= [Link])
public String createProducts(@RequestBody Product
product) {
HttpHeaders headers = new HttpHeaders();

[Link]([Link](MediaType.APPLICATION_J
SON));
HttpEntity<Product> entity = new
HttpEntity<Product>(product,headers);

return [Link](
"[Link]
[Link], entity, [Link]).getBody();
}
}

PUT
Consuming PUT API by using RestTemplate - exchange()
method

Assume this URL [Link] returns the


below response and we are going to consume this API response
by using Rest Template.

The code given below is Request body −

{
"name":"Indian Ginger"
}

The code given below is the Response body −

Product is updated successfully

You will have to follow the points given below to consume the API

 Autowired the Rest Template Object.


 Use HttpHeaders to set the Request Headers.
 Use HttpEntity to wrap the request object. Here, we wrap the
Product object to send it to the request body.
 Provide the URL, HttpMethod, and Return type for
exchange() method.
@RestController
public class ConsumeWebService {
@Autowired
RestTemplate restTemplate;

@RequestMapping(value = "/template/products/{id}",
method = [Link])
public String updateProduct(@PathVariable("id")
String id, @RequestBody Product product) {
HttpHeaders headers = new HttpHeaders();

[Link]([Link](MediaType.APPLICATION_J
SON));
HttpEntity<Product> entity = new
HttpEntity<Product>(product,headers);

return [Link](
"[Link]
[Link], entity, [Link]).getBody();
}
}

DELETE
Consuming DELETE API by using RestTemplate -
exchange() method

Assume this URL [Link] returns the


response given below and we are going to consume this API
response by using Rest Template.

This line of code shown below is the Response body −

Product is deleted successfully

You will have to follow the points shown below to consume the API

 Autowired the Rest Template Object.


 Use HttpHeaders to set the Request Headers.
 Use HttpEntity to wrap the request object.
 Provide the URL, HttpMethod, and Return type for
exchange() method.
@RestController
public class ConsumeWebService {
@Autowired
RestTemplate restTemplate;

@RequestMapping(value = "/template/products/{id}",
method = [Link])
public String deleteProduct(@PathVariable("id")
String id) {
HttpHeaders headers = new HttpHeaders();

[Link]([Link](MediaType.APPLICATION_J
SON));
HttpEntity<Product> entity = new
HttpEntity<Product>(headers);

return [Link](
"[Link]
[Link], entity, [Link]).getBody();
}
}

The complete Rest Template Controller class file is given below −

package [Link];

import [Link];

import
[Link];
import [Link];
import [Link];
import [Link];
import [Link];

import
[Link];
import
[Link];
import
[Link];
import
[Link];
import
[Link];
import [Link];

import [Link];

@RestController
public class ConsumeWebService {
@Autowired
RestTemplate restTemplate;

@RequestMapping(value = "/template/products")
public String getProductList() {
HttpHeaders headers = new HttpHeaders();

[Link]([Link](MediaType.APPLICATION_J
SON));
HttpEntity<String> entity = new
HttpEntity<String>(headers);

return [Link](
"[Link]
[Link], entity, [Link]).getBody();
}
@RequestMapping(value = "/template/products", method
= [Link])
public String createProducts(@RequestBody Product
product) {
HttpHeaders headers = new HttpHeaders();

[Link]([Link](MediaType.APPLICATION_J
SON));
HttpEntity<Product> entity = new
HttpEntity<Product>(product,headers);

return [Link](
"[Link]
[Link], entity, [Link]).getBody();
}
@RequestMapping(value = "/template/products/{id}",
method = [Link])
public String updateProduct(@PathVariable("id")
String id, @RequestBody Product product) {
HttpHeaders headers = new HttpHeaders();
[Link]([Link](MediaType.APPLICATION_J
SON));
HttpEntity<Product> entity = new
HttpEntity<Product>(product,headers);

return [Link](
"[Link]
[Link], entity, [Link]).getBody();
}
@RequestMapping(value = "/template/products/{id}",
method = [Link])
public String deleteProduct(@PathVariable("id")
String id) {
HttpHeaders headers = new HttpHeaders();

[Link]([Link](MediaType.APPLICATION_J
SON));
HttpEntity<Product> entity = new
HttpEntity<Product>(headers);

return [Link](
"[Link]
[Link], entity, [Link]).getBody();
}
}

The code for Spring Boot Application Class – [Link]


is given below −

package [Link];

import [Link];
import
[Link]
ation;

@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
[Link]([Link],
args);
}
}

The code for Maven build – [Link] is given below −

<?xml version = "1.0" encoding = "UTF-8"?>


<project xmlns = "[Link]
xmlns:xsi = "[Link]
instance"
xsi:schemaLocation =
"[Link]
[Link]

<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring
Boot</description>

<parent>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-parent</artifactId>
<version>[Link]</version>
<relativePath/>
</parent>

<properties>

<[Link]>UTF-8</[Link]
ceEncoding>

<[Link]>UTF-8</[Link]
[Link]>
<[Link]>1.8</[Link]>
</properties>

<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

The code for Gradle Build – [Link] is given below −

buildscript {
ext {
springBootVersion = '[Link]'
}
repositories {
mavenCentral()
}
dependencies {
classpath("[Link]:spring-boot-
gradle-plugin:${springBootVersion}")
}
}

apply plugin: 'java'


apply plugin: 'eclipse'
apply plugin: '[Link]'
group = '[Link]'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
mavenCentral()
}
dependencies {
compile('[Link]:spring-boot-
starter-web')
testCompile('[Link]:spring-boot-
starter-test')
}

You can create an executable JAR file, and run the Spring Boot
application by using the following Maven or Gradle commands −

For Maven, you can use the command given below −

mvn clean install

After “BUILD SUCCESS”, you can find the JAR file under the target
directory.

For Gradle, you can use the command shown below −

gradle clean build

After “BUILD SUCCESSFUL”, you can find the JAR file under
build/libs directory.

Now, run the JAR file by using the following command −

java –jar <JARFILE>

Now, the application has started on the Tomcat port 8080.

Now hit the below URL’s in POSTMAN application and you can see
the output.
GET Products by Rest Template
− [Link]

Create Products POST


− [Link]

Update Product PUT


− [Link]
Delete Product − [Link]

Spring Boot - File Handling


In this chapter, you will learn how to upload and download the file
by using web service.

File Upload
For uploading a file, you can use MultipartFile as a Request
Parameter and this API should consume Multi-Part form data
value. Observe the code given below −
@RequestMapping(value = "/upload", method =
[Link], consumes =
MediaType.MULTIPART_FORM_DATA_VALUE)

public String fileUpload(@RequestParam("file")


MultipartFile file) {
return null;
}

The complete code for the same is given below −

package [Link];

import [Link];
import [Link];
import [Link];

import [Link];
import
[Link];
import
[Link];
import
[Link];
import
[Link];
import [Link];

@RestController
public class FileUploadController {
@RequestMapping(value = "/upload", method =
[Link],
consumes = MediaType.MULTIPART_FORM_DATA_VALUE)

public String fileUpload(@RequestParam("file")


MultipartFile file) throws IOException {
File convertFile = new
File("/var/tmp/"+[Link]());
[Link]();
FileOutputStream fout = new
FileOutputStream(convertFile);
[Link]([Link]());
[Link]();
return "File is upload successfully";
}
}

File Download
For file download, you should use InputStreamResource for
downloading a File. We need to set the HttpHeader Content-
Disposition in Response and need to specify the response Media
Type of the application.

Note − In the following example, file should be available on the


specified path where the application is running.

@RequestMapping(value = "/download", method =


[Link])
public ResponseEntity<Object> downloadFile() throws
IOException {
String filename = "/var/tmp/[Link]";
File file = new File(filename);
InputStreamResource resource = new
InputStreamResource(new FileInputStream(file));
HttpHeaders headers = new HttpHeaders();

[Link]("Content-Disposition",
[Link]("attachment; filename=\"%s\"",
[Link]()));
[Link]("Cache-Control", "no-cache, no-store,
must-revalidate");
[Link]("Pragma", "no-cache");
[Link]("Expires", "0");

ResponseEntity<Object>
responseEntity =
[Link]().headers(headers).contentLength(file
.length()).contentType(

[Link]("application/txt")).body(resou
rce);

return responseEntity;
}

The complete code for the same is given below −

package [Link];

import [Link];
import [Link];
import [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import
[Link];
import
[Link];
import
[Link];

@RestController
public class FileDownloadController {
@RequestMapping(value = "/download", method =
[Link])
public ResponseEntity<Object> downloadFile() throws
IOException {
String filename = "/var/tmp/[Link]";
File file = new File(filename);
InputStreamResource resource = new
InputStreamResource(new FileInputStream(file));
HttpHeaders headers = new HttpHeaders();

[Link]("Content-Disposition",
[Link]("attachment; filename=\"%s\"",
[Link]()));
[Link]("Cache-Control", "no-cache, no-store,
must-revalidate");
[Link]("Pragma", "no-cache");
[Link]("Expires", "0");

ResponseEntity<Object>
responseEntity =
[Link]().headers(headers).contentLength(

[Link]()).contentType([Link]("ap
plication/txt")).body(resource);

return responseEntity;
}
}

The main Spring Boot application is given below −

package [Link];

import [Link];
import
[Link]
ation;

@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
[Link]([Link],
args);
}
}

The code for Maven build – [Link] is given below −

<?xml version = "1.0" encoding = "UTF-8"?>


<project xmlns = "[Link]
xmlns:xsi = "[Link]
instance"
xsi:schemaLocation =
"[Link]
[Link]

<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring
Boot</description>

<parent>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-parent</artifactId>
<version>[Link]</version>
<relativePath/>
</parent>

<properties>

<[Link]>UTF-8</[Link]
ceEncoding>

<[Link]>UTF-8</[Link]
[Link]>
<[Link]>1.8</[Link]>
</properties>

<dependencies>
<dependency>
<groupId>[Link]</groupId>

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

<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

The code for Gradle Build – [Link] is given below −

buildscript {
ext {
springBootVersion = '[Link]'
}
repositories {
mavenCentral()
}
dependencies {
classpath("[Link]:spring-boot-
gradle-plugin:${springBootVersion}")
}
}

apply plugin: 'java'


apply plugin: 'eclipse'
apply plugin: '[Link]'

group = '[Link]'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
mavenCentral()
}
dependencies {
compile('[Link]:spring-boot-
starter-web')
testCompile('[Link]:spring-boot-
starter-test')
}

Now you can create an executable JAR file, and run the Spring
Boot application by using the Maven or Gradle commands given
below −
For Maven, use the command given below −

mvn clean install

After “BUILD SUCCESS”, you can find the JAR file under target
directory.

For Gradle, you ca use the command shown below −

sgradle clean build

After “BUILD SUCCESSFUL”, you can find the JAR file under
build/libs directory.

Now, run the JAR file by using the following command −

java –jar <JARFILE>

This will start the application on the Tomcat port 8080 as shown
below −

Now hit the below URL’s in POSTMAN application and you can see
the output as shown below −

File upload − [Link]

File download − [Link]


Spring Boot - Service
Components
Service Components are the class file which contains @Service
annotation. These class files are used to write business logic in a
different layer, separated from @RestController class file. The
logic for creating a service component class file is shown here −

public interface ProductService {


}

The class that implements the Interface with @Service annotation


is as shown −

@Service
public class ProductServiceImpl implements
ProductService {
}

Observe that in this tutorial, we are using Product Service


API(s) to store, retrieve, update and delete the products. We
wrote the business logic in @RestController class file itself. Now,
we are going to move the business logic code from controller to
service component.

You can create an Interface which contains add, edit, get and
delete methods using the code as shown below −

package [Link];

import [Link];
import [Link];

public interface ProductService {


public abstract void createProduct(Product product);
public abstract void updateProduct(String id,
Product product);
public abstract void deleteProduct(String id);
public abstract Collection<Product> getProducts();
}

The following code will let you to create a class which implements
the ProductService interface with @Service annotation and write
the business logic to store, retrieve, delete and updates the
product.

package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

@Service
public class ProductServiceImpl implements
ProductService {
private static Map<String, Product> productRepo =
new HashMap<>();
static {
Product honey = new Product();
[Link]("1");
[Link]("Honey");
[Link]([Link](), honey);

Product almond = new Product();


[Link]("2");
[Link]("Almond");
[Link]([Link](), almond);
}
@Override
public void createProduct(Product product) {
[Link]([Link](), product);
}
@Override
public void updateProduct(String id, Product
product) {
[Link](id);
[Link](id);
[Link](id, product);
}
@Override
public void deleteProduct(String id) {
[Link](id);

}
@Override
public Collection<Product> getProducts() {
return [Link]();
}
}

The code here show the Rest Controller class file, here we
@Autowired the ProductService interface and called the methods.

package [Link];

import
[Link];
import [Link];
import [Link];
import
[Link];
import
[Link];
import
[Link];
import
[Link];
import
[Link];

import [Link];
import [Link];

@RestController
public class ProductServiceController {
@Autowired
ProductService productService;

@RequestMapping(value = "/products")
public ResponseEntity<Object> getProduct() {
return new
ResponseEntity<>([Link](),
[Link]);
}
@RequestMapping(value = "/products/{id}", method =
[Link])
public ResponseEntity<Object>
updateProduct(@PathVariable("id") String id,
@RequestBody Product product) {

[Link](id, product);
return new ResponseEntity<>("Product is updated
successsfully", [Link]);
}
@RequestMapping(value = "/products/{id}", method =
[Link])
public ResponseEntity<Object>
delete(@PathVariable("id") String id) {
[Link](id);
return new ResponseEntity<>("Product is deleted
successsfully", [Link]);
}
@RequestMapping(value = "/products", method =
[Link])
public ResponseEntity<Object>
createProduct(@RequestBody Product product) {
[Link](product);
return new ResponseEntity<>("Product is created
successfully", [Link]);
}
}

The code for POJO class – [Link] is shown here −

package [Link];

public class Product {


private String id;
private String name;

public String getId() {


return id;
}
public void setId(String id) {
[Link] = id;
}
public String getName() {
return name;
}
public void setName(String name) {
[Link] = name;
}
}

A main Spring Boot application is given below −

package [Link];

import [Link];
import
[Link]
ation;

@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
[Link]([Link],
args);
}
}

The code for Maven build – [Link] is shown below −

<?xml version = "1.0" encoding = "UTF-8"?>


<project xmlns = "[Link]
xmlns:xsi = "[Link]
instance"
xsi:schemaLocation =
"[Link]
[Link]

<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring
Boot</description>

<parent>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-parent</artifactId>
<version>[Link]</version>
<relativePath/>
</parent>

<properties>

<[Link]>UTF-8</[Link]
ceEncoding>

<[Link]>UTF-8</[Link]
[Link]>
<[Link]>1.8</[Link]>
</properties>

<dependencies>
<dependency>
<groupId>[Link]</groupId>

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

<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

The code for Gradle Build – [Link] is shown below −

buildscript {
ext {
springBootVersion = '[Link]'
}
repositories {
mavenCentral()
}
dependencies {
classpath("[Link]:spring-boot-
gradle-plugin:${springBootVersion}")
}
}

apply plugin: 'java'


apply plugin: 'eclipse'
apply plugin: '[Link]'

group = '[Link]'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
mavenCentral()
}
dependencies {
compile('[Link]:spring-boot-
starter-web')
testCompile('[Link]:spring-boot-
starter-test')
}
You can create an executable JAR file, and run the Spring Boot
application by using the Maven or Gradle commands given below

For Maven, use the command as shown below −

mvn clean install

After “BUILD SUCCESS”, you can find the JAR file under the target
directory.

For Gradle, you can use the command as shown below −

gradle clean build

After “BUILD SUCCESSFUL”, you can find the JAR file under
build/libs directory.

Run the JAR file by using the command given below −

java –jar <JARFILE>

Now, the application has started on the Tomcat port 8080 as


shown in the image given below −

Now hit the below URL’s in POSTMAN application and you can see
the output as shown below −

GET API URL is − [Link]


POST API URL is − [Link]

PUT API URL is − [Link]


DELETE API URL is − [Link]

Spring Boot - Thymeleaf


Thymeleaf is a Java-based library used to create a web
application. It provides a good support for serving a
XHTML/HTML5 in web applications. In this chapter, you will learn
in detail about Thymeleaf.

Thymeleaf Templates
Thymeleaf converts your files into well-formed XML files. It
contains 6 types of templates as given below −

 XML
 Valid XML
 XHTML
 Valid XHTML
 HTML5
 Legacy HTML5

All templates, except Legacy HTML5, are referring to well-formed


valid XML files. Legacy HTML5 allows us to render the HTML5 tags
in web page including not closed tags.

Web Application
You can use Thymeleaf templates to create a web application in
Spring Boot. You will have to follow the below steps to create a
web application in Spring Boot by using Thymeleaf.

Use the following code to create a @Controller class file to


redirect the Request URI to HTML file −

package [Link];

import [Link];
import
[Link];

@Controller
public class WebController {
@RequestMapping(value = "/index")
public String index() {
return "index";
}
}

In the above example, the request URI is /index, and the control
is redirected into the [Link] file. Note that the [Link] file
should be placed under the templates directory and all JS and CSS
files should be placed under the static directory in classpath. In
the example shown, we used CSS file to change the color of the
text.

You can use the following code and created a CSS file in separate
folder css and name the file as [Link] −
h4 {
color: red;
}

The code for [Link] file is given below −

<!DOCTYPE html>
<html>
<head>
<meta charset = "ISO-8859-1" />
<link href = "css/[Link]" rel =
"stylesheet"/>
<title>Spring Boot Application</title>
</head>
<body>
<h4>Welcome to Thymeleaf Spring Boot web
application</h4>
</body>
</html>

The project explorer is shown in the screenshot given below −


Now, we need to add the Spring Boot Starter Thymeleaf
dependency in our build configuration file.

Maven users can add the following dependency into the [Link]
file −
<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

Gradle users can add the following dependency in the [Link]


file −

compile group: '[Link]', name: 'spring-boot-starter-


thymeleaf'

The code for main Spring Boot application class file is given below

package [Link];

import [Link];
import
[Link]
ation;

@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
[Link]([Link],
args);
}
}

The code for Maven – [Link] is given below −

<?xml version = "1.0" encoding = "UTF-8"?>


<project xmlns = "[Link]
xmlns:xsi = "[Link]
instance"
xsi:schemaLocation =
"[Link]
[Link]

<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring
Boot</description>

<parent>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-parent</artifactId>
<version>[Link]</version>
<relativePath />
</parent>

<properties>

<[Link]>UTF-8</[Link]
ceEncoding>

<[Link]>UTF-8</[Link]
[Link]>
<[Link]>1.8</[Link]>
</properties>

<dependencies>
<dependency>
<groupId>[Link]</groupId>

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

<dependency>
<groupId>[Link]</groupId>

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

<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

The code for Gradle – [Link] is given below −

buildscript {
ext {
springBootVersion = '[Link]'
}
repositories {
mavenCentral()
}
dependencies {
classpath("[Link]:spring-boot-
gradle-plugin:${springBootVersion}")
}
}

apply plugin: 'java'


apply plugin: 'eclipse'
apply plugin: '[Link]'

group = '[Link]'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
mavenCentral()
}
dependencies {
compile('[Link]:spring-boot-
starter-web')
compile group: '[Link]', name:
'spring-boot-starter-thymeleaf'
testCompile('[Link]:spring-boot-
starter-test')
}

You can create an executable JAR file, and run the spring boot
application by using the following Maven or Gradle commands −

For Maven, use the command as shown below −

mvn clean install

After “BUILD SUCCESS”, you can find the JAR file under the target
directory.

For Gradle, use the command as shown below −

gradle clean build

After “BUILD SUCCESSFUL”, you can find the JAR file under the
build/libs directory.

Run the JAR file by using the command given here −

java –jar <JARFILE>

Now, the application has started on the Tomcat port 8080 as


shown below −

Now hit the URL in your web browser and you can see the output
as shown −

[Link]
Consuming RESTful Web
Services
This chapter will discuss in detail about consuming a RESTful Web
Services by using jQuery AJAX.

Create a simple Spring Boot web application and write a controller


class files which is used to redirects into the HTML file to
consumes the RESTful web services.

We need to add the Spring Boot starter Thymeleaf and Web


dependency in our build configuration file.

For Maven users, add the below dependencies in your [Link]


file.

<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

For Gradle users, add the below dependencies into your


[Link] file −

compile group: ‘[Link]’, name:


‘spring-boot-starter-thymeleaf’
compile(‘[Link]:spring-boot-starter-
web’)
The code for @Controller class file is given below −

@Controller
public class ViewController {
}

You can define the Request URI methods to redirects into the
HTML file as shown below −

@RequestMapping(“/view-products”)
public String viewProducts() {
return “view-products”;
}
@RequestMapping(“/add-products”)
public String addProducts() {
return “add-products”;
}

This API [Link] should return the


below JSON in response as shown below −

[
{
"id": "1",
"name": "Honey"
},
{
"id": "2",
"name": "Almond"
}
]

Now, create a [Link] file under the templates


directory in the classpath.

In the HTML file, we added the jQuery library and written the code
to consume the RESTful web service on page load.
<script src =
"[Link]
[Link]"></script>

<script>
$(document).ready(function(){
$.getJSON("[Link]
function(result){
$.each(result, function(key,value) {
$("#productsJson").append([Link]+"
"+[Link]+" ");
});
});
});
</script>

The POST method and this


URL [Link] should contains the below
Request Body and Response body.

The code for Request body is given below −

{
"id":"3",
"name":"Ginger"
}

The code for Response body is given below −

Product is created successfully

Now, create the [Link] file under the templates


directory in the classpath.

In the HTML file, we added the jQuery library and written the code
that submits the form to RESTful web service on clicking the
button.

<script src =
"[Link]
[Link]"></script>
<script>
$(document).ready(function() {
$("button").click(function() {
var productmodel = {
id : "3",
name : "Ginger"
};
var requestJSON =
[Link](productmodel);
$.ajax({
type : "POST",
url : "[Link]
headers : {
"Content-Type" : "application/json"
},
data : requestJSON,
success : function(data) {
alert(data);
},
error : function(data) {
}
});
});
});
</script>

The complete code is given below.

Maven – [Link] file

<?xml version = "1.0" encoding = "UTF-8"?>


<project xmlns = "[Link]
xmlns:xsi = "[Link]
instance"
xsi:schemaLocation =
"[Link]
[Link]

<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring
Boot</description>

<parent>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-parent</artifactId>
<version>[Link]</version>
<relativePath />
</parent>

<properties>

<[Link]>UTF-8</[Link]
ceEncoding>

<[Link]>UTF-8</[Link]
[Link]>
<[Link]>1.8</[Link]>
</properties>

<dependencies>
<dependency>
<groupId>[Link]</groupId>

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

<dependency>
<groupId>[Link]</groupId>

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

<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

The code for Gradle – [Link] is given below −

buildscript {
ext {
springBootVersion = ‘[Link]’
}
repositories {
mavenCentral()
}
dependencies {
classpath("[Link]:spring-boot-
gradle-plugin:${springBootVersion}")
}
}

apply plugin: ‘java’


apply plugin: ‘eclipse’
apply plugin: ‘[Link]’

group = ‘[Link]’
version = ‘0.0.1-SNAPSHOT’
sourceCompatibility = 1.8

repositories {
mavenCentral()
}

dependencies {
compile(‘[Link]:spring-boot-
starter-web’)
compile group: ‘[Link]’, name:
‘spring-boot-starter-thymeleaf’
testCompile(‘[Link]:spring-boot-
starter-test’)
}

The controller class file given below – [Link] is given


below −

package [Link];

import [Link];
import
[Link];

@Controller
public class ViewController {
@RequestMapping(“/view-products”)
public String viewProducts() {
return “view-products”;
}
@RequestMapping(“/add-products”)
public String addProducts() {
return “add-products”;
}
}

The [Link] file is given below −

<!DOCTYPE html>
<html>
<head>
<meta charset = "ISO-8859-1"/>
<title>View Products</title>
<script src =
"[Link]
[Link]"></script>

<script>
$(document).ready(function(){
$.getJSON("[Link]
function(result){
$.each(result, function(key,value) {
$("#productsJson").append([Link]+"
"+[Link]+" ");
});
});
});
</script>
</head>

<body>
<div id = "productsJson"> </div>
</body>
</html>

The [Link] file is given below −

<!DOCTYPE html>
<html>
<head>
<meta charset = "ISO-8859-1" />
<title>Add Products</title>
<script src =
"[Link]
[Link]"></script>

<script>
$(document).ready(function() {
$("button").click(function() {
var productmodel = {
id : "3",
name : "Ginger"
};
var requestJSON =
[Link](productmodel);
$.ajax({
type : "POST",
url :
"[Link]
headers : {
"Content-Type" :
"application/json"
},
data : requestJSON,
success : function(data) {
alert(data);
},
error : function(data) {
}
});
});
});
</script>
</head>

<body>
<button>Click here to submit the form</button>
</body>
</html>

The main Spring Boot Application class file is given below −

package [Link];

import [Link];
import
[Link]
ation;

@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
[Link]([Link],
args);
}
}

Now, you can create an executable JAR file, and run the Spring
Boot application by using the following Maven or Gradle
commands.

For Maven, use the command as given below −


mvn clean install

After “BUILD SUCCESS”, you can find the JAR file under the target
directory.

For Gradle, use the command as given below −

gradle clean build

After “BUILD SUCCESSFUL”, you can find the JAR file under the
build/libs directory.

Run the JAR file by using the following command −

java –jar <JARFILE>

Now, the application has started on the Tomcat port 8080.

Now hit the URL in your web browser and you can see the output
as shown −

[Link]

[Link]
Now, click the button Click here to submit the form and you
can see the result as shown −

Now, hit the view products URL and see the created product.

[Link]

Angular JS
To consume the APIs by using Angular JS, you can use the
examples given below −

Use the following code to create the Angular JS Controller to


consume the GET API - [Link] −

[Link]('demo', [])
.controller('Hello', function($scope, $http) {
$[Link]('[Link]
then(function(response) {
$[Link] = [Link];
});
});
Use the following code to create the Angular JS Controller to
consume the POST API - [Link] −

[Link]('demo', [])
.controller('Hello', function($scope, $http) {
$[Link]('[Link]
then(function(response) {
[Link]("Product created successfully");
});
});

Note − The Post method data represents the Request body in


JSON format to create a product.

Spring Boot - CORS Support


Cross-Origin Resource Sharing (CORS) is a security concept that
allows restricting the resources implemented in web browsers. It
prevents the JavaScript code producing or consuming the
requests against different origin.

For example, your web application is running on 8080 port and by


using JavaScript you are trying to consuming RESTful web
services from 9090 port. Under such situations, you will face the
Cross-Origin Resource Sharing security issue on your web
browsers.

Two requirements are needed to handle this issue −

 RESTful web services should support the Cross-Origin


Resource Sharing.
 RESTful web service application should allow accessing the
API(s) from the 8080 port.

In this chapter, we are going to learn in detail about How to


Enable Cross-Origin Requests for a RESTful Web Service
application.

Enable CORS in Controller Method


We need to set the origins for RESTful web service by
using @CrossOrigin annotation for the controller method. This
@CrossOrigin annotation supports specific REST API, and not for
the entire application.

@RequestMapping(value = "/products")
@CrossOrigin(origins = "[Link]

public ResponseEntity<Object> getProduct() {


return null;
}

Global CORS Configuration


We need to define the shown @Bean configuration to set the
CORS configuration support globally to your Spring Boot
application.

@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsMappings(CorsRegistry
registry) {

[Link]("/products").allowedOrigins("http:/
/localhost:9000");
}
};
}

To code to set the CORS configuration globally in main Spring


Boot application is given below.

package [Link];

import [Link];
import
[Link]
ation;
import [Link];
import
[Link]
egistry;
import
[Link]
cConfigurer;
import
[Link]
cConfigurerAdapter;

@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
[Link]([Link],
args);
}
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsMappings(CorsRegistry
registry) {

[Link]("/products").allowedOrigins("http:/
/localhost:8080");
}
};
}
}

Now, you can create a Spring Boot web application that runs on
8080 port and your RESTful web service application that can run
on the 9090 port. For further details about implementation about
RESTful Web Service, you can refer to the chapter
titled Consuming RESTful Web Services of this tutorial.

Spring Boot -
Internationalization
Internationalization is a process that makes your application
adaptable to different languages and regions without engineering
changes on the source code. In ither words, Internationalization is
a readiness of Localization.

In this chapter, we are going to learn in detail about How to


implement the Internationalization in Spring Boot.

Dependencies
We need the Spring Boot Starter Web and Spring Boot Starter
Thymeleaf dependency to develop a web application in Spring
Boot.

Maven

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

Gradle

compile('[Link]:spring-boot-starter-web')
compile group: '[Link]', name: 'spring-boot-starter-
thymeleaf'

LocaleResolver
We need to determine default Locale of your application. We need
to add the LocaleResolver bean in our Spring Boot application.

@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver sessionLocaleResolver = new
SessionLocaleResolver();
[Link]([Link]);
return sessionLocaleResolver;
}

LocaleChangeInterceptor
LocaleChangeInterceptor is a used to change the new Locale
based on the value of the language parameter added to a
request.

@Bean
public LocaleChangeInterceptor
localeChangeInterceptor() {
LocaleChangeInterceptor localeChangeInterceptor =
new LocaleChangeInterceptor();
[Link]("language");
return localeChangeInterceptor;
}

To take this effect, we need to add the LocaleChangeInterceptor


into the application’s registry interceptor. The configuration class
should extend the WebMvcConfigurerAdapter class and override
the addInterceptors() method.

@Override
public void addInterceptors(InterceptorRegistry
registry) {
[Link](localeChangeInterceptor());
}

Messages Sources
Spring Boot application by default takes the message sources
from src/main/resources folder under the classpath. The default
locale message file name should be [Link] and
files for each locale should name as messages_XX.properties.
The “XX” represents the locale code.

All the message properties should be used as key pair values. If


any properties are not found on the locale, the application uses
the default property from [Link] file.
The default [Link] will be as shown −

[Link]=Hi Welcome to Everyone

The French language messages_fr.properties will be as shown −

[Link]=Salut Bienvenue à tous

Note − Messages source file should be saved as “UTF-8” file


format.

HTML file
In the HTML file, use the syntax #{key} to display the messages
from the properties file.

<h1 th:text = "#{[Link]}"></h1>

The complete code is given below

Maven – [Link]

<?xml version = "1.0" encoding = "UTF-8"?>


<project xmlns = "[Link]
xmlns:xsi = "[Link]
instance"
xsi:schemaLocation =
"[Link]
[Link]

<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring
Boot</description>

<parent>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-parent</artifactId>
<version>[Link]</version>
<relativePath />
</parent>

<properties>

<[Link]>UTF-8</[Link]
ceEncoding>

<[Link]>UTF-8</[Link]
[Link]>
<[Link]>1.8</[Link]>
</properties>

<dependencies>
<dependency>
<groupId>[Link]</groupId>

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

<dependency>
<groupId>[Link]</groupId>

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

<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

Gradle – [Link]

buildscript {
ext {
springBootVersion = '[Link]'
}
repositories {
mavenCentral()
}
dependencies {
classpath("[Link]:spring-boot-
gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: '[Link]'

group = '[Link]'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
mavenCentral()
}
dependencies {
compile('[Link]:spring-boot-
starter-web')
compile group: '[Link]', name:
'spring-boot-starter-thymeleaf'
testCompile('[Link]:spring-boot-
starter-test')
}

The main Spring Boot application class file is given below −

package [Link];
import [Link];
import
[Link]
ation;

@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
[Link]([Link],
args);
}
}

The controller class file is given below −

package [Link];

import [Link];
import
[Link];

@Controller
public class ViewController {
@RequestMapping("/locale")
public String locale() {
return "locale";
}
}

Configuration class to support the Internationalization

package [Link];

import [Link];

import [Link];
import
[Link];
import [Link];
import
[Link]
ceptorRegistry;
import
[Link]
cConfigurerAdapter;
import
[Link]
eptor;
import
[Link]
ver;

@Configuration
public class Internationalization extends
WebMvcConfigurerAdapter {
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver sessionLocaleResolver = new
SessionLocaleResolver();

[Link]([Link]);
return sessionLocaleResolver;
}
@Bean
public LocaleChangeInterceptor
localeChangeInterceptor() {
LocaleChangeInterceptor localeChangeInterceptor =
new LocaleChangeInterceptor();
[Link]("language");
return localeChangeInterceptor;
}
@Override
public void addInterceptors(InterceptorRegistry
registry) {

[Link](localeChangeInterceptor());
}
}

The Message sources – [Link] is as shown −

[Link] = Hi Welcome to Everyone


The Message sources – message_fr.properties is as shown −

[Link] = Salut Bienvenue à tous

The HTML file [Link] should be placed under the templates


directory on the classpath as shown −

<!DOCTYPE html>
<html>
<head>
<meta charset = "ISO-8859-1"/>
<title>Internationalization</title>
</head>
<body>
<h1 th:text = "#{[Link]}"></h1>
</body>
</html>

You can create an executable JAR file, and run the Spring boot
application by using the following Maven or Gradle commands −

For Maven, use the following command −

mvn clean install

After “BUILD SUCCESS”, you can find the JAR file under the target
directory.

For Gradle, use the following command −

gradle clean build

After “BUILD SUCCESSFUL”, you can find the JAR file under the
build/libs directory.

Now, run the JAR file by using the command as shown −

java –jar <JARFILE>

You will find that the application has started on the Tomcat port
8080.
Now hit the URL [Link] in your web
browser and you can see the following output −

The URL [Link] will give


you the output as shown −

Spring Boot - Scheduling


Scheduling is a process of executing the tasks for the specific
time period. Spring Boot provides a good support to write a
scheduler on the Spring applications.

Java Cron Expression


Java Cron expressions are used to configure the instances of
CronTrigger, a subclass of [Link]. For more
information about Java cron expression you can refer to this link −

[Link]
cron_expressions.htm

The @EnableScheduling annotation is used to enable the


scheduler for your application. This annotation should be added
into the main Spring Boot application class file.

@SpringBootApplication
@EnableScheduling

public class DemoApplication {


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

The @Scheduled annotation is used to trigger the scheduler for a


specific time period.

@Scheduled(cron = "0 * 9 * * ?")


public void cronJobSch() throws Exception {
}

The following is a sample code that shows how to execute the


task every minute starting at 9:00 AM and ending at 9:59 AM,
every day

package [Link];

import [Link];
import [Link];
import
[Link];
import [Link];

@Component
public class Scheduler {
@Scheduled(cron = "0 * 9 * * ?")
public void cronJobSch() {
SimpleDateFormat sdf = new
SimpleDateFormat("yyyy-MM-dd HH:mm:[Link]");
Date now = new Date();
String strDate = [Link](now);
[Link]("Java cron job expression:: "
+ strDate);
}
}
The following screenshot shows how the application has started at
[Link] and for every one minute from that time the cron job
scheduler task has executed.

Fixed Rate
Fixed Rate scheduler is used to execute the tasks at the specific
time. It does not wait for the completion of previous task. The
values should be in milliseconds. The sample code is shown here

@Scheduled(fixedRate = 1000)
public void fixedRateSch() {
}

A sample code for executing a task on every second from the


application startup is shown here −

package [Link];

import [Link];
import [Link];
import
[Link];
import [Link];

@Component
public class Scheduler {
@Scheduled(fixedRate = 1000)
public void fixedRateSch() {
SimpleDateFormat sdf = new
SimpleDateFormat("yyyy-MM-dd HH:mm:[Link]");

Date now = new Date();


String strDate = [Link](now);
[Link]("Fixed Rate scheduler:: " +
strDate);
}
}

Observe the following screenshot that shows the application that


has started at [Link] and after that every second fixed rate
scheduler task has executed.

Fixed Delay
Fixed Delay scheduler is used to execute the tasks at a specific
time. It should wait for the previous task completion. The values
should be in milliseconds. A sample code is shown here −

@Scheduled(fixedDelay = 1000, initialDelay = 1000)


public void fixedDelaySch() {
}

Here, the initialDelay is the time after which the task will be
executed the first time after the initial delay value.

An example to execute the task for every second after 3 seconds


from the application startup has been completed is shown below

package [Link];

import [Link];
import [Link];
import
[Link];
import [Link];

@Component
public class Scheduler {
@Scheduled(fixedDelay = 1000, initialDelay = 3000)
public void fixedDelaySch() {
SimpleDateFormat sdf = new
SimpleDateFormat("yyyy-MM-dd HH:mm:[Link]");
Date now = new Date();
String strDate = [Link](now);
[Link]("Fixed Delay scheduler:: " +
strDate);
}
}

Observe the following screenshot which shows the application


that has started at [Link] and after every 3 seconds, the fixed
delay scheduler task has executed on every second.

Spring Boot - Enabling HTTPS


By default, Spring Boot application uses HTTP 8080 port when the
application starts up.

You need to follow the steps given below to configure the HTTPS
and the port 443 in Spring Boot application −

 Obtain the SSL certificate – Create a self-signed certificate or


get one from a Certificate Authority
 Enable HTTPS and 443 port

Self-Signed Certificate
To create a self-signed certificate, Java Run Time environment
comes bundled with certificate management utility key tool. This
utility tool is used to create a Self-Signed certificate. It is shown in
the code given here −

keytool -genkey -alias tomcat -storetype PKCS12 -keyalg


RSA -keysize 2048 -keystore keystore.p12 -validity 3650
Enter keystore password:
Re-enter new password:
What is your first and last name?
[Unknown]:
What is the name of your organizational unit?
[Unknown]:
What is the name of your organization?
[Unknown]:
What is the name of your City or Locality?
[Unknown]:
What is the name of your State or Province?
[Unknown]:
What is the two-letter country code for this unit?
[Unknown]:
Is CN = Unknown, OU=Unknown, O = Unknown, L =
Unknown, ST = Unknown, C = Unknown correct?
[no]: yes

This code will generate a PKCS12 keystore file named as


keystore.p12 and the certificate alias name is tomcat.

Configure HTTPS
We need to provide the server port as 443, key-store file path,
key-store-password, key-store-type and key alias name into the
[Link] file. Observe the code given here −

[Link]: 443
[Link]-store: keystore.p12
[Link]-store-password: springboot
[Link]: PKCS12
[Link]: tomcat

You can use the following code if you are using YAML properties
use below [Link] −

server:
port: 443
ssl:
key-store: keystore.p12
key-store-password: springboot
keyStoreType: PKCS12
keyAlias: tomcat

You can create an executable JAR file, and run the spring boot
application by using the following Maven or Gradle commands.

For Maven, you can use the following command −

mvn clean install

After “BUILD SUCCESS”, you can find the JAR file under the target
directory.

For Gradle, you can use the command

gradle clean build

After “BUILD SUCCESSFUL”, you can find the JAR file under the
build/libs directory.

Now, run the JAR file by using the following command −

java –jar <JARFILE>

Now, the application has started on the Tomcat port 443 with
https as shown −

Spring Boot - Eureka Server


Eureka Server is an application that holds the information about
all client-service applications. Every Micro service will register into
the Eureka server and Eureka server knows all the client
applications running on each port and IP address. Eureka Server is
also known as Discovery Server.

In this chapter, we will learn in detail about How to build a Eureka


server.

Building a Eureka Server


Eureka Server comes with the bundle of Spring Cloud. For this, we
need to develop the Eureka server and run it on the default port
8761.

Visit the Spring Initializer homepage [Link] and


download the Spring Boot project with Eureka server dependency.
It is shown in the screenshot below −

After downloading the project in main Spring Boot Application


class file, we need to add @EnableEurekaServer annotation. The
@EnableEurekaServer annotation is used to make your Spring
Boot application acts as a Eureka Server.

The code for main Spring Boot application class file is as shown
below −

package [Link];

import [Link];
import
[Link]
ation;
import
[Link]
urekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaserverApplication {
public static void main(String[] args) {

[Link]([Link],
args);
}
}

Make sure Spring cloud Eureka server dependency is added in


your build configuration file.

The code for Maven user dependency is shown below −

<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-cloud-starter-eureka-server</artifac
tId>
</dependency>

The code for Gradle user dependency is given below −

compile('[Link]:spring-cloud-starter-eureka-server')

The complete build configuration file is given below −

Maven [Link]

<?xml version = "1.0" encoding = "UTF-8"?>


<project xmlns = "[Link]
xmlns:xsi = "[Link]
instance"
xsi:schemaLocation =
"[Link]
[Link]

<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>eurekaserver</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eurekaserver</name>
<description>Demo project for Spring
Boot</description>

<parent>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-parent</artifactId>
<version>[Link]</version>
<relativePath/> <!-- lookup parent from
repository -->
</parent>

<properties>

<[Link]>UTF-8</[Link]
ceEncoding>

<[Link]>UTF-8</[Link]
[Link]>
<[Link]>1.8</[Link]>
<[Link]>[Link]</spring-
[Link]>
</properties>

<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-cloud-starter-eureka-
server</artifactId>
</dependency>

<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>

<groupId>[Link]</groupId>

<artifactId>spring-cloud-dependencies</artifactId>
<version>${[Link]}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

Gradle – [Link]

buildscript {
ext {
springBootVersion = '[Link]'
}
repositories {
mavenCentral()
}
dependencies {
classpath("[Link]:spring-boot-
gradle-plugin:${springBootVersion}")
}
}

apply plugin: 'java'


apply plugin: 'eclipse'
apply plugin: '[Link]'
group = '[Link]'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
mavenCentral()
}
ext {
springCloudVersion = '[Link]'
}
dependencies {
compile('[Link]:spring-cloud-
starter-eureka-server')
testCompile('[Link]:spring-boot-
starter-test')
}
dependencyManagement {
imports {
mavenBom "[Link]:spring-cloud-
dependencies:${springCloudVersion}"
}
}

By default, the Eureka Server registers itself into the discovery.


You should add the below given configuration into your
[Link] file or [Link] file.

[Link] file is given below −

[Link] = false
[Link] = false
[Link] = 8761

The [Link] file is given below −

eureka:
client:
registerWithEureka: false
fetchRegistry: false
server:
port: 8761

Now, you can create an executable JAR file, and run the Spring
Boot application by using the Maven or Gradle commands shown
below −

For Maven, use the command as shown below −

mvn clean install

After “BUILD SUCCESS”, you can find the JAR file under the target
directory.

For Gradle, you can use the command shown below −

gradle clean build

After “BUILD SUCCESSFUL”, you can find the JAR file under the
build/libs directory.

Now, run the JAR file by using the following command −

java –jar <JARFILE>

You can find that the application has started on the Tomcat port
8761 as shown below −

Now, hit the URL [Link] in your web browser


and you can find the Eureka Server running on the port 8761 as
shown below −
Service Registration with Eureka
In this chapter, you are going to learn in detail about How to
register the Spring Boot Micro service application into the Eureka
Server. Before registering the application, please make sure
Eureka Server is running on the port 8761 or first build the Eureka
Server and run it. For further information on building the Eureka
server, you can refer to the previous chapter.

First, you need to add the following dependencies in our build


configuration file to register the microservice with the Eureka
server.
Maven users can add the following dependencies into
the [Link] file −

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

Gradle users can add the following dependencies into


the [Link] file −

compile('[Link]:spring-cloud-starter-eureka')

Now, we need to add the @EnableEurekaClient annotation in the


main Spring Boot application class file. The @EnableEurekaClient
annotation makes your Spring Boot application act as a Eureka
client.

The main Spring Boot application is as given below −

package [Link];

import [Link];
import
[Link]
ation;
import
[Link]
ient;

@SpringBootApplication
@EnableEurekaClient
public class EurekaclientApplication {
public static void main(String[] args) {

[Link]([Link],
args);
}
}

To register the Spring Boot application into Eureka Server we


need to add the following configuration in our
[Link] file or [Link] file and specify the
Eureka Server URL in our configuration.

The code for [Link] file is given below −

eureka:
client:
serviceUrl:
defaultZone: [Link]
instance:
preferIpAddress: true
spring:
application:
name: eurekaclient

The code for [Link] file is given below −

[Link] =
[Link]
[Link] = true
[Link] = eurekaclient

Now, add the Rest Endpoint to return String in the main Spring
Boot application and the Spring Boot Starter web dependency in
build configuration file. Observe the code given below −

package [Link];

import [Link];
import
[Link]
ation;
import
[Link]
ient;
import
[Link];
import
[Link];

@SpringBootApplication
@EnableEurekaClient
@RestController
public class EurekaclientApplication {
public static void main(String[] args) {

[Link]([Link],
args);
}
@RequestMapping(value = "/")
public String home() {
return "Eureka Client application";
}
}

The entire configuration file is given below.

For Maven user - [Link]

<?xml version = "1.0" encoding = "UTF-8"?>


<project xmlns = "[Link]
xmlns:xsi = "[Link]
instance"
xsi:schemaLocation =
"[Link]
[Link]

<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>eurekaclient</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>eurekaclient</name>
<description>Demo project for Spring
Boot</description>

<parent>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-parent</artifactId>
<version>[Link]</version>
<relativePath/> <!-- lookup parent from
repository -->
</parent>

<properties>

<[Link]>UTF-8</[Link]
ceEncoding>

<[Link]>UTF-8</[Link]
[Link]>
<[Link]>1.8</[Link]>
<[Link]>[Link]</spring-
[Link]>
</properties>

<dependencies>
<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>

<groupId>[Link]</groupId>

<artifactId>spring-cloud-dependencies</artifactId>
<version>${[Link]}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</projecta>

For Gradle user – [Link]

buildscript {
ext {
springBootVersion = '[Link]'
}
repositories {
mavenCentral()
}
dependencies {
classpath("[Link]:spring-boot-
gradle-plugin:${springBootVersion}")
}
}

apply plugin: 'java'


apply plugin: 'eclipse'
apply plugin: '[Link]'

group = '[Link]'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
mavenCentral()
}
ext {
springCloudVersion = '[Link]'
}
dependencies {
compile('[Link]:spring-cloud-
starter-eureka')
testCompile('[Link]:spring-boot-
starter-test')
compile('[Link]:spring-boot-
starter-web')
}
dependencyManagement {
imports {
mavenBom "[Link]:spring-cloud-
dependencies:${springCloudVersion}"
}
}

You can create an executable JAR file, and run the Spring Boot
application by using the following Maven or Gradle commands −

For Maven, you can use the following command −

mvn clean install

After “BUILD SUCCESS”, you can find the JAR file under the target
directory.

For Gradle, you can use the following command −

gradle clean build

After “BUILD SUCCESSFUL”, you can find the JAR file under the
build/libs directory.

Now, run the JAR file by using the command as shown −

java –jar <JARFILE>

Now, the application has started on the Tomcat port 8080 and
Eureka Client application is registered with the Eureka Server as
shown below −
Hit the URL [Link] in your web browser and you
can see the Eureka Client application is registered with Eureka
Server.

Now hit the URL [Link] in your web browser and


see the Rest Endpoint output.

Spring Boot - Zuul Proxy Server


and Routing
Zuul Server is a gateway application that handles all the requests
and does the dynamic routing of microservice applications. The
Zuul Server is also known as Edge Server.

For Example, /api/user is mapped to the user service and


/api/products is mapped to the product service and Zuul Server
dynamically routes the requests to the respective backend
application.
In this chapter, we are going to see in detail how to create Zuul
Server application in Spring Boot.

Creating Zuul Server Application


The Zuul Server is bundled with Spring Cloud dependency. You
can download the Spring Boot project from Spring Initializer
page [Link] and choose the Zuul Server
dependency.

Add the @EnableZuulProxy annotation on your main Spring Boot


application. The @EnableZuulProxy annotation is used to make
your Spring Boot application act as a Zuul Proxy server.

package [Link];

import [Link];
import
[Link]
ation;
import
[Link];

@SpringBootApplication
@EnableZuulProxy
public class ZuulserverApplication {
public static void main(String[] args) {
[Link]([Link],
args);
}
}

You will have to add the Spring Cloud Starter Zuul dependency in
our build configuration file.

Maven users will have to add the following dependency in


your [Link] file −

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>

For Gradle users, add the below dependency in your [Link]


file

compile('[Link]:spring-cloud-starter-zuul')

For Zuul routing, add the below properties in your


[Link] file or [Link] file.

[Link] = zuulserver
[Link] = /api/demo/**
[Link] = [Link]
[Link] = 8111

This means that http calls to /api/demo/ get forwarded to the


products service. For example, /api/demo/products is forwarded
to /products.

yaml file users can use the [Link] file shown below −

server:
port: 8111
spring:
application:
name: zuulserver
zuul:

routes:
products:
path: /api/demo/**
url: [Link]

Note − The [Link] application should already


be running before routing via Zuul Proxy.

The complete build configuration file is given below.

Maven users can use the [Link] file given below −

<?xml version = "1.0" encoding = "UTF-8"?>


<project xmlns = "[Link]
xmlns:xsi = "[Link]
instance"
xsi:schemaLocation =
"[Link]
[Link]

<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>zuulserver</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>zuulserver</name>
<description>Demo project for Spring
Boot</description>

<parent>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-parent</artifactId>
<version>[Link]</version>
<relativePath/> <!-- lookup parent from
repository -->
</parent>
<properties>

<[Link]>UTF-8</[Link]
ceEncoding>

<[Link]>UTF-8</[Link]
[Link]>
<[Link]>1.8</[Link]>
<[Link]>[Link]</spring-
[Link]>
</properties>

<dependencies>
<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>

<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>

<groupId>[Link]</groupId>

<artifactId>spring-cloud-dependencies</artifactId>
<version>${[Link]}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

Gradle users can use the [Link] file given below −

buildscript {
ext {
springBootVersion = '[Link]'
}
repositories {
mavenCentral()
}
dependencies {
classpath("[Link]:spring-boot-
gradle-plugin:${springBootVersion}")
}
}

apply plugin: 'java'


apply plugin: 'eclipse'
apply plugin: '[Link]'

group = '[Link]'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
mavenCentral()
}
ext {
springCloudVersion = '[Link]'
}
dependencies {
compile('[Link]:spring-cloud-
starter-zuul')
testCompile('[Link]:spring-boot-
starter-test')
}
dependencyManagement {
imports {
mavenBom "[Link]:spring-cloud-
dependencies:${springCloudVersion}"
}
}

You can create an executable JAR file, and run the Spring Boot
application by using the Maven or Gradle commands given below

For Maven, you can use the command given below −

mvn clean install

After “BUILD SUCCESS”, you can find the JAR file under the target
directory.

For Gradle, you can use the command given below −

gradle clean build

After “BUILD SUCCESSFUL”, you can find the JAR file under the
build/libs directory.

Now, run the JAR file by using the command shown below −

java –jar <JARFILE>

You can find the application has started on the Tomcat port 8111
as shown here.

Now, hit the URL [Link] in


your web browser and you can see the output of /products REST
Endpoint as shown below −
Spring Boot - Cloud
Configuration Server
Spring Cloud Configuration Server is a centralized application that
manages all the application related configuration properties. In
this chapter, you will learn in detail about how to create Spring
Cloud Configuration server.

Creating Spring Cloud Configuration Server


First, download the Spring Boot project from the Spring Initializer
page and choose the Spring Cloud Config Server dependency.
Observe the screenshot given below −
Now, add the Spring Cloud Config server dependency in your
build configuration file as explained below −

Maven users can add the below dependency into the [Link] file.

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>

Gradle users can add the below dependency in your [Link]


file.

compile('[Link]:spring-cloud-config-server')

Now, add the @EnableConfigServer annotation in your main


Spring Boot application class file. The @EnableConfigServer
annotation makes your Spring Boot application act as a
Configuration Server.

The main Spring Boot application class file is given below −

package [Link];

import [Link];
import
[Link]
ation;
import
[Link]
ver;

@SpringBootApplication
@EnableConfigServer
public class ConfigserverApplication {
public static void main(String[] args) {

[Link]([Link],
args);
}
}

Now, add the below configuration to your properties file and


replace the [Link] file into [Link]
file. Observe the code given below −

[Link] = 8888
[Link]=file:
///C:/configprop/
SPRING_PROFILES_ACTIVE=native

Configuration Server runs on the Tomcat port 8888 and


application configuration properties are loaded from native search
locations.

Now, in [Link] place your client application -


[Link] file. For example, your client application
name is config-client, then rename your [Link]
file as [Link] and place the properties file on
the path [Link]

The code for config-client properties file is given below −

[Link] = Welcome to Spring cloud config server

The complete build configuration file is given below −

Maven users can use [Link] given below −


<?xml version = "1.0" encoding = "UTF-8"?>
<project xmlns = "[Link]
xmlns:xsi = "[Link]
instance"
xsi:schemaLocation =
"[Link]
[Link]

<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>configserver</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>configserver</name>
<description>Demo project for Spring
Boot</description>
<parent>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-parent</artifactId>
<version>[Link]</version>
<relativePath/> <!-- lookup parent from
repository -->
</parent>

<properties>

<[Link]>UTF-8</[Link]
ceEncoding>

<[Link]>UTF-8</[Link]
[Link]>
<[Link]>1.8</[Link]>
<[Link]>[Link]</spring-
[Link]>
</properties>

<dependencies>
<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>

<groupId>[Link]</groupId>

<artifactId>spring-cloud-dependencies</artifactId>
<version>${[Link]}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

Gradle users can use the [Link] file given below −

<scope>import</scope>
</dependency>
</dependencies>
buildscript {
ext {
springBootVersion = '[Link]'
}
repositories {
mavenCentral()
}
dependencies {
classpath("[Link]:spring-boot-
gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: '[Link]'

group = '[Link]'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
mavenCentral()
}
ext {
springCloudVersion = '[Link]'
}
dependencies {
compile('[Link]:spring-cloud-
config-server')
testCompile('[Link]:spring-boot-
starter-test')
}
dependencyManagement {
imports {
mavenBom "[Link]:spring-cloud-
dependencies:${springCloudVersion}"
}
}

Now, create an executable JAR file, and run the Spring Boot
application by using the following Maven or Gradle commands −

For Maven, use the command given below −

mvn clean install

After “BUILD SUCCESS”, you can find the JAR file under the target
directory.
For Gradle, use the command given below −

gradle clean build

After “BUILD SUCCESSFUL”, you can find the JAR file under the
build/libs directory.

Run the JAR file by using the following command −

java –jar <JARFILE>

Now, the application has started on the Tomcat port 8888 as


shown here −

Now hit the


URL [Link] on
your web browser and you can see your config-client application
configuration properties as shown here.
Spring Boot - Cloud
Configuration Client
Some applications may need configuration properties that may
need a change and developers may need to take them down or
restart the application to perform this. However, this might be
lead to downtime in production and the need of restarting the
application. Spring Cloud Configuration Server lets developers to
load the new configuration properties without restarting the
application and without any downtime.

Working with Spring Cloud Configuration Server


First, download the Spring Boot project
from [Link] and choose the Spring Cloud Config
Client dependency. Now, add the Spring Cloud Starter Config
dependency in your build configuration file.

Maven users can add the following dependency into the [Link]
file.

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>

Gradle users can add the following dependency into


the [Link] file.

compile('[Link]:spring-cloud-starter-config')

Now, you need to add the @RefreshScope annotation to your


main Spring Boot application. The @RefreshScope annotation is
used to load the configuration properties value from the Config
server.

package [Link];

import [Link];
import
[Link]
ation;
import
[Link]
reshScope;

@SpringBootApplication
@RefreshScope
public class ConfigclientApplication {
public static void main(String[] args) {

[Link]([Link],
args);
}
}

Now, add the config server URL in your [Link] file


and provide your application name.

Note − [Link] config server should be run before


starting the config client application.

[Link] = config-client
[Link] = [Link]

The code for writing a simple REST Endpoint to read the welcome
message from the configuration server is given below −

package [Link];

import
[Link];
import [Link];
import
[Link]
ation;
import
[Link]
reshScope;
import
[Link];
import
[Link];

@SpringBootApplication
@RefreshScope
@RestController
public class ConfigclientApplication {
@Value("${[Link]}")
String welcomeText;

public static void main(String[] args) {

[Link]([Link],
args);
}
@RequestMapping(value = "/")
public String welcomeText() {
return welcomeText;
}
}

You can create an executable JAR file, and run the Spring Boot
application by using the following Maven or Gradle commands −

For Maven, you can use the command shown below −

mvn clean install

After “BUILD SUCCESS”, you can find the JAR file under the target
directory.

For Gradle, you can use the command shown below −

gradle clean build

After “BUILD SUCCESSFUL”, you can find the JAR file under the
build/libs directory.

Now, run the JAR file by using the command shown here:

java –jar <JARFILE>

Now, the application has started on the Tomcat port 8080 as


shown here −
You can see the log in console window; config-client application is
fetching the configuration from the [Link]

2017-12-08 [Link].682 INFO 1104 --- [


main] [Link] :
Fetching config from server at: [Link]

Now hit the URL, [Link] welcome message is


loaded from the Configuration server.

Now, go and change the property value on the Configuration


server and hit the actuator Endpoint POST
URL [Link] and see the new
configuration property value in the URL [Link]

Spring Boot - Actuator


Spring Boot Actuator provides secured endpoints for monitoring
and managing your Spring Boot application. By default, all
actuator endpoints are secured. In this chapter, you will learn in
detail about how to enable Spring Boot actuator to your
application.

Enabling Spring Boot Actuator


To enable Spring Boot actuator endpoints to your Spring Boot
application, we need to add the Spring Boot Starter actuator
dependency in our build configuration file.

Maven users can add the below dependency in your [Link] file.

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

Gradle users can add the below dependency in your [Link]


file.

compile group: '[Link]', name: 'spring-boot-starter-


actuator'

In the [Link] file, we need to disable the security


for actuator endpoints.

[Link] = false

YAML file users can add the following property in your


[Link] file.

management:
security:
enabled: false

If you want to use the separate port number for accessing the
Spring boot actutator endpoints add the management port
number in [Link] file.

[Link] = 9000

YAML file users can add the following property in your


[Link] file.

management:
port: 9000

Now, you can create an executable JAR file, and run the Spring
Boot application by using the following Maven or Gradle
commands.

For Maven, you can use the following command −

mvn clean install


After “BUILD SUCCESS”, you can find the JAR file under the target
directory.

For Gradle, you can use the following command −

gradle clean build

After “BUILD SUCCESSFUL”, you can find the JAR file under the
build/libs directory.

Now, you can run the JAR file by using the following command −

java –jar <JARFILE>

Now, the application has started on the Tomcat port 8080. Note
that if you specified the management port number, then same
application is running on two different port numbers.

Some important Spring Boot Actuator endpoints are given below.


You can enter them in your web browser and monitor your
application behavior.

ENDPOIN
USAGE
TS

To view the application metrics such as memory used, memory free, threads,
/metrics
classes, system uptime etc.

/env To view the list of Environment variables used in the application.

/beans To view the Spring beans and its types, scopes and dependency.

/health To view the application health

/info To view the information about the Spring Boot application.

/trace To view the list of Traces of your Rest endpoints.

Spring Boot - Admin Server


Monitoring your application by using Spring Boot Actuator
Endpoint is slightly difficult. Because, if you have ‘n’ number of
applications, every application has separate actuator endpoints,
thus making monitoring difficult. Spring Boot Admin Server is an
application used to manage and monitor your Microservice
application.

To handle such situations, CodeCentric Team provides a Spring


Boot Admin UI to manage and monitor all your Spring Boot
application Actuator endpoints at one place.

For building a Spring Boot Admin Server we need to add the


below dependencies in your build configuration file.

Maven users can add the below dependencies in your [Link]


file −

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-admin-server</artifactId>
<version>1.5.5</version>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-admin-server-ui</artifactId>
<version>1.5.5</version>
</dependency>

Gradle users can add the below dependencies in your [Link]


file −

compile group: '[Link]', name: 'spring-boot-


admin-server', version: '1.5.5'
compile group: '[Link]', name: 'spring-boot-
admin-server-ui', version: '1.5.5'

Add the @EnableAdminServer annotation in your main Spring


Boot application class file. The @EnableAdminServer annotation is
used to make your as Admin Server to monitor all other
microservices.

package [Link];

import [Link];
import
[Link]
ation;
import
[Link];

@SpringBootApplication
@EnableAdminServer
public class AdminserverApplication {
public static void main(String[] args) {

[Link]([Link],
args);
}
}

Now, define the [Link] and application name in


[Link] file a shown −

[Link] = 9090
[Link] = adminserver

For YAML users, use the following properties to define the port
number and application name in [Link] file.

server:
port: 9090
spring:
application:
name: adminserver

The build configuration file is given below.

For Maven users – [Link]

<?xml version = "1.0" encoding = "UTF-8"?>


<project xmlns = "[Link]
xmlns:xsi = "[Link]
instance"
xsi:schemaLocation =
"[Link]
[Link]

<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>adminserver</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>adminserver</name>
<description>Demo project for Spring
Boot</description>

<parent>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-parent</artifactId>
<version>[Link]</version>
<relativePath /> <!-- lookup parent from
repository -->
</parent>

<properties>

<[Link]>UTF-8</[Link]
ceEncoding>

<[Link]>UTF-8</[Link]
[Link]>
<[Link]>1.8</[Link]>
</properties>

<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-admin-server</artifactId>
<version>1.5.5</version>
</dependency>

<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-admin-server-ui</artifactId>
<version>1.5.5</version>
</dependency>

<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

For Gradle users – [Link] file

buildscript {
ext {
springBootVersion = '[Link]'
}
repositories {
mavenCentral()
}
dependencies {
classpath("[Link]:spring-boot-
gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: '[Link]'

group = '[Link]'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('[Link]:spring-boot-
starter')
compile group: '[Link]', name: 'spring-boot-
admin-server', version: '1.5.5'
compile group: '[Link]', name: 'spring-boot-
admin-server-ui', version: '1.5.5'
testCompile('[Link]:spring-boot-
starter-test')
}

You can create an executable JAR file, and run the Spring Boot
application by using the following Maven or Gradle commands −

For Maven, use the command shown here −

mvn clean install

After “BUILD SUCCESS”, you can find the JAR file under target
directory.

For Gradle, use the command shown here −

gradle clean build

After “BUILD SUCCESSFUL”, you can find the JAR file under
build/libs directory.

Now, run the JAR file by using the command given below −

java –jar <JARFILE>


Now, the application has started on the Tomcat port 9090 as
shown here −

Now hit the below URL from your web browser and see the Admin
Server UI.

[Link]

Spring Boot - Admin Client


For monitoring and managing your microservice application via
Spring Boot Admin Server, you should add the Spring Boot Admin
starter client dependency and point out the Admin Server URI into
the application properties file.

Note − For monitoring an application, you should enable the


Spring Boot Actuator Endpoints for your Microservice application.

First, add the following Spring Boot Admin starter client


dependency and Spring Boot starter actuator dependency in your
build configuration file.

Maven users can add the following dependencies in your [Link]


file −

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>1.5.5</version>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Gradle users can add the following dependencies in your
[Link] file.

compile group: '[Link]', name: 'spring-boot-


admin-starter-client', version: '1.5.5'
compile('[Link]:spring-boot-starter-
actuator')

Now, add the Spring Boot Admin Server URL into your application
properties file.

For properties file users, add the following properties in the


[Link] file.

[Link] = [Link]

For YAML users, add the following property in [Link] file.

spring:
boot:
admin:
url: [Link]

Now, create an executable JAR file, and run the Spring Boot
application by using the following Maven or Gradle commands.

For Maven, you can use the command as shown −

mvn clean install

After “BUILD SUCCESS”, you can find the JAR file under the target
directory.

For Gradle, you can use the command as shown −

gradle clean build

After “BUILD SUCCESSFUL”, you can find the JAR file under the
build/libs directory.

Now, run the JAR file by using the command shown −

java –jar <JARFILE>


Now, the application has started on the Tomcat port 9090 as
shown −

Now hit the following URL from your web browser and see your
spring Boot application is registered with Spring Boot Admin
Server.

[Link]

Now, click the Details button and the see the actuator endpoints
in Admin Server UI.

Spring Boot - Enabling Swagger2


Swagger2 is an open source project used to generate the REST
API documents for RESTful web services. It provides a user
interface to access our RESTful web services via the web browser.

To enable the Swagger2 in Spring Boot application, you need to


add the following dependencies in our build configurations file.

<dependency>
<groupId>[Link]</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>

For Gradle users, add the following dependencies in your


[Link] file.

compile group: '[Link]', name: 'springfox-swagger2', version: '2.7.0'


compile group: '[Link]', name: 'springfox-swagger-ui', version: '2.7.0'

Now, add the @EnableSwagger2 annotation in your main Spring


Boot application. The @EnableSwagger2 annotation is used to
enable the Swagger2 for your Spring Boot application.

The code for main Spring Boot application is shown below −

package [Link];

import [Link];
import
[Link]
ation;
import
[Link]
ger2;

@SpringBootApplication
@EnableSwagger2
public class SwaggerDemoApplication {
public static void main(String[] args) {

[Link]([Link],
args);
}
}

Next, create Docket Bean to configure Swagger2 for your Spring


Boot application. We need to define the base package to
configure REST API(s) for Swagger2.

@Bean
public Docket productApi() {
return new Docket(DocumentationType.SWAGGER_2).select()
.apis([Link]("[Link]
erdemo")).build();
}

Now, add this bean in main Spring Boot application class file itself
and your main Spring Boot application class will look as shown
below −

package [Link];

import [Link];
import
[Link]
ation;
import [Link];

import
[Link]
s;
import [Link];
import
[Link];
import
[Link]
ger2;
@SpringBootApplication
@EnableSwagger2
public class SwaggerDemoApplication {
public static void main(String[] args) {

[Link]([Link],
args);
}
@Bean
public Docket productApi() {
return new
Docket(DocumentationType.SWAGGER_2).select()
.apis([Link]("com
.[Link]")).build();
}
}

Now, add the below Spring Boot Starter Web dependency in your
build configuration file to write a REST Endpoints as shown below

Maven users can add the following dependency in your [Link]


file −

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

Gradle users can add the following dependency in [Link] file


compile('[Link]:spring-boot-starter-web')

Now, the code to build two simple RESTful web services GET and
POST in Rest Controller file is shown here −

package [Link];

import [Link];
import [Link];
import
[Link];
import
[Link];
import
[Link];

@RestController
public class SwaggerAPIController {
@RequestMapping(value = "/products", method =
[Link])
public List<String> getProducts() {
List<String> productsList = new ArrayList<>();
[Link]("Honey");
[Link]("Almond");
return productsList;
}
@RequestMapping(value = "/products", method =
[Link])
public String createProduct() {
return "Product is saved successfully";
}
}

The complete build configuration file is given below −

Maven – [Link]

<?xml version = "1.0" encoding = "UTF-8"?>


<project xmlns = "[Link]
xmlns:xsi = "[Link]
instance"
xsi:schemaLocation =
"[Link]
[Link]
<modelVersion>4.0.0</modelVersion>

<groupId>[Link]</groupId>
<artifactId>swagger-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>swagger-demo</name>
<description>Demo project for Spring
Boot</description>

<parent>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-parent</artifactId>
<version>[Link]</version>
<relativePath /> <!-- lookup parent from
repository -->
</parent>

<properties>

<[Link]>UTF-8</[Link]
ceEncoding>

<[Link]>UTF-8</[Link]
[Link]>
<[Link]>1.8</[Link]>
</properties>

<dependencies>
<dependency>
<groupId>[Link]</groupId>

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

<dependency>
<groupId>[Link]</groupId>

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

<dependency>
<groupId>[Link]</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>

<dependency>
<groupId>[Link]</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

Gradle – [Link]

buildscript {
ext {
springBootVersion = '[Link]'
}
repositories {
mavenCentral()
}
dependencies {
classpath("[Link]:spring-boot-
gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: '[Link]'

group = '[Link]'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
mavenCentral()
} dependencies {
compile('[Link]:spring-boot-
starter-web')
testCompile('[Link]:spring-boot-
starter-test')
compile group: '[Link]', name: 'springfox-
swagger2', version: '2.7.0'
compile group: '[Link]', name: 'springfox-
swagger-ui', version: '2.7.0'
}

You can create an executable JAR file, and run the Spring Boot
application by using the following Maven or Gradle commands.

For Maven, you can use the command shown here −

mvn clean install

After “BUILD SUCCESS”, you can find the JAR file under the target
directory.

For Gradle, you can use the command as shown here −

gradle clean build

After “BUILD SUCCESSFUL”, you can find the JAR file under the
build/libs directory.

Now, run the JAR file by using the command shown here −

java –jar <JARFILE>

Now, the application will start on the Tomcat port 8080 as shown

Now, hit the URL in your web browser and see the Swagger API
functionalities.

[Link]
Spring Boot - Creating Docker
Image
Docker is a container management service that eases building
and deployment. If you are a beginner to Docker, you can learn
about is in detail at this link
− [Link]

In this chapter, we are going to see How to create a Docker image


by using Maven and Gradle dependencies for your Spring Boot
application.

Create Dockerfile
First, create a file with the name Dockerfile under the
directories src/main/docker with the contents shown below.
Note that this file is important to create a Docker image.

FROM java:8
VOLUME /tmp
ADD [Link] [Link]
RUN bash -c 'touch /[Link]'
ENTRYPOINT
["java","-[Link]=file:/dev/./urandom","-
jar","/[Link]"]

Maven
For Maven, add the Docker Maven plugin into your build
configuration file [Link]
<properties>

<[Link]>spring-boot-tutorialspoint</docker
.[Link]>
</properties>

<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>

<configuration>
<imageName>${[Link]}/$
{[Link]}</imageName>

<dockerDirectory>src/main/docker</dockerDirectory>
<resources>
<resource>
<directory>$
{[Link]}</directory>
<include>$
{[Link]}.jar</include>
</resource>
</resources>
</configuration>
</plugin>

<plugin>
<groupId>[Link]</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>

</build>

The complete [Link] file is given below −

<?xml version = "1.0" encoding = "UTF-8"?>


<project xmlns = "[Link]
xmlns:xsi = "[Link]
instance"
xsi:schemaLocation =
"[Link]
[Link]

<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>dockerapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>dockerapp</name>
<description>Demo project for Spring
Boot</description>

<parent>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-parent</artifactId>
<version>[Link]</version>
<relativePath /> <!-- lookup parent from
repository -->
</parent>

<properties>

<[Link]>UTF-8</[Link]
ceEncoding>

<[Link]>UTF-8</[Link]
[Link]>
<[Link]>1.8</[Link]>
<[Link]>spring-boot-
tutorialspoint</[Link]>
</properties>

<dependencies>
<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>

<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>

<configuration>
<imageName>${[Link]}/$
{[Link]}</imageName>

<dockerDirectory>src/main/docker</dockerDirectory>
<resources>
<resource>
<directory>$
{[Link]}</directory>
<include>$
{[Link]}.jar</include>
</resource>
</resources>
</configuration>
</plugin>

<plugin>
<groupId>[Link]</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

Now, you can run your application by using the Maven


command mvn package docker:build
Note − Enable the Expose daemon
on tcp://localhost:2375 without TLS.

After build success, you can see the output on the console as
shown below −

Now, see the Docker images by the command using docker


images and see the image info on the console.

Gradle
To build a Docker image by using Gradle build configuration, we
need to add the docker plugin and need to write a
task buildDocker to create a Docker image.
The code for Gradle Docker configuration is given below.

buildscript {
.....
dependencies {
.....
classpath('[Link]:gradle-
docker:1.2')
}
}

group = 'spring-boot-tutorialspoint'

.....
apply plugin: 'docker'

task buildDocker(type: Docker, dependsOn: build) {


applicationName = [Link]
dockerfile = file('src/main/docker/Dockerfile')
doFirst {
copy {
from jar
into stageDir
}
}
}

The complete [Link] file is given below.

buildscript {
ext {
springBootVersion = '[Link]'
}
repositories {
mavenCentral()
}
dependencies {
classpath("[Link]:spring-boot-
gradle-plugin:${springBootVersion}")
classpath('[Link]:gradle-
docker:1.2')
}
}

group = 'spring-boot-tutorialspoint'

apply plugin: 'java'


apply plugin: 'eclipse'
apply plugin: '[Link]'
apply plugin: 'docker'

group = '[Link]'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
mavenCentral()
}
dependencies {
compile('[Link]:spring-boot-
starter-web')
testCompile('[Link]:spring-boot-
starter-test')
}
task buildDocker(type: Docker, dependsOn: build) {
applicationName = [Link]
dockerfile = file('src/main/docker/Dockerfile')
doFirst {
copy {
from jar
into stageDir
}
}
}

Now, create a Docker image by using the command shown below


gradle build buildDocker


After executing the command, you can see the BUILD
SUCCESSFUL log on the console window.

Now, see the Docker images by the command using docker


images and see the image’s info on the console.

Spring Boot - Tracing Micro


Service Logs
Most developers face difficulty of tracing logs if any issue
occurred. This can be solved by Spring Cloud Sleuth and ZipKin
server for Spring Boot application.

Spring Cloud Sleuth


Spring cloud Sleuth logs are printed in the following format −

[application-name,traceid,spanid,zipkin-export]

Where,

 Application-name = Name of the application


 Traceid = each request and response traceid is same when
calling same service or one service to another service.
 Spanid = Span Id is printed along with Trace Id. Span Id is
different every request and response calling one service to
another service.
 Zipkin-export = By default it is false. If it is true, logs will be
exported to the Zipkin server.

Now, add the Spring Cloud Starter Sleuth dependency in your


build configuration file as follows −

Maven users can add the following dependency in your [Link]


file −

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>

Gradle users can add the following dependency in your


[Link] file −

compile('[Link]:spring-cloud-starter-sleuth')

Now, add the Logs into your Spring Boot application Rest
Controller class file as shown here −

package [Link];

import [Link];
import [Link];

import [Link];
import
[Link]
ation;
import
[Link];
import
[Link];

@SpringBootApplication
@RestController
public class SleuthappApplication {
private static final Logger LOG =
[Link]([Link]());
public static void main(String[] args) {
[Link]([Link],
args);
}
@RequestMapping("/")
public String index() {
[Link]([Link], "Index API is calling");
return "Welcome Sleuth!";
}
}

Now, add the application name in [Link] file as


shown −

[Link] = tracinglogs

The complete code for build configuration file is given below −

Maven – [Link]

<?xml version = "1.0" encoding = "UTF-8"?>


<project xmlns = "[Link]
xmlns:xsi = "[Link]
instance"
xsi:schemaLocation =
"[Link]
[Link]

<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>sleuthapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>sleuthapp</name>
<description>Demo project for Spring
Boot</description>
<parent>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-parent</artifactId>
<version>[Link]</version>
<relativePath/> <!-- lookup parent from
repository -->
</parent>

<properties>

<[Link]>UTF-8</[Link]
ceEncoding>

<[Link]>UTF-8</[Link]
[Link]>
<[Link]>1.8</[Link]>
<[Link]>[Link]</spring-
[Link]>
</properties>

<dependencies>
<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-cloud-dependencies</artifactId>
<version>${[Link]}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

Gradle – [Link]

buildscript {
ext {
springBootVersion = '[Link]'
}
repositories {
mavenCentral()
}
dependencies {
classpath("[Link]:spring-boot-
gradle-plugin:${springBootVersion}")
}
}

apply plugin: 'java'


apply plugin: 'eclipse'
apply plugin: '[Link]'

group = '[Link]'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
mavenCentral()
}
ext {
springCloudVersion = '[Link]'
}
dependencies {
compile('[Link]:spring-cloud-
starter-sleuth')
compile('[Link]:spring-boot-
starter-web')
testCompile('[Link]:spring-boot-
starter-test')
}
dependencyManagement {
imports {
mavenBom "[Link]:spring-cloud-
dependencies:${springCloudVersion}"
}
}

You can create an executable JAR file, and run the Spring Boot
application by using the following Maven or Gradle commands.

For Maven, you can use the following command −

mvn clean install

After “BUILD SUCCESS”, you can find the JAR file under the target
directory.

For Gradle, you can use the following command −

gradle clean build

After “BUILD SUCCESSFUL”, you can find the JAR file under the
build/libs directory.

Now, run the JAR file by using the command shown here −

java –jar <JARFILE>


Now, the application has started on the Tomcat port 8080.

Now, hit the URL in your web browser and see the output in
console log.

[Link]

You can see the following logs in the console window. Observe
that log is printed in the following format [application-name,
traceid, spanid, zipkin-export]

Zipkin Server
Zipkin is an application that monitors and manages the Spring
Cloud Sleuth logs of your Spring Boot application. To build a
Zipkin server, we need to add the Zipkin UI and Zipkin Server
dependencies in our build configuration file.

Maven users can add the following dependency in your [Link]


file −

<dependency>
<groupId>[Link]</groupId>
<artifactId>zipkin-server</artifactId>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>zipkin-autoconfigure-ui</artifactId>
</dependency>
Gradle users can add the below dependency in your [Link]
file −

compile('[Link]:zipkin-autoconfigure-ui')
compile('[Link]:zipkin-server')

Now, configure the [Link] = 9411 in application properties


file.

For properties file users, add the below property in


[Link] file.

[Link] = 9411

For YAML users, add the below property in [Link] file.

server:
port: 9411

Add the @EnableZipkinServer annotation in your main Spring


Boot application class fie. The @EnableZipkinServer annotation is
used to enable your application act as a Zipkin server.

package [Link];

import [Link];
import
[Link]
ation;
import [Link];

@SpringBootApplication
@EnableZipkinServer
public class ZipkinappApplication {
public static void main(String[] args) {
[Link]([Link],
args);
}
}

The code for complete build configuration file is given below.

Maven – [Link]
<?xml version = "1.0" encoding = "UTF-8"?>
<project xmlns = "[Link]
xmlns:xsi = "[Link]
instance"
xsi:schemaLocation =
"[Link]
[Link]

<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>zipkinapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>zipkinapp</name>
<description>Demo project for Spring
Boot</description>

<parent>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-parent</artifactId>
<version>[Link]</version>
<relativePath /> <!-- lookup parent from
repository -->
</parent>

<properties>

<[Link]>UTF-8</[Link]
ceEncoding>

<[Link]>UTF-8</[Link]
[Link]>
<[Link]>1.8</[Link]>
<[Link]>[Link]</spring-
[Link]>
</properties>

<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>zipkin-server</artifactId>
</dependency>
<dependency>
<groupId>[Link]</groupId>

<artifactId>zipkin-autoconfigure-ui</artifactId>
</dependency>
<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>

<groupId>[Link]</groupId>

<artifactId>spring-cloud-dependencies</artifactId>
<version>${[Link]}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

Gradle – [Link]

buildscript {
ext {
springBootVersion = '[Link]'
}
repositories {
mavenCentral()
}
dependencies {
classpath("[Link]:spring-boot-
gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: '[Link]'

group = '[Link]'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
mavenCentral()
}
ext {
springCloudVersion = '[Link]'
}
dependencies {
compile('[Link]:zipkin-autoconfigure-ui')
compile('[Link]:zipkin-server')
testCompile('[Link]:spring-boot-
starter-test')
}
dependencyManagement {
imports {
mavenBom "[Link]:spring-cloud-
dependencies:${springCloudVersion}"
}
}

You can create an executable JAR file, and run the Spring Boot
application by using the below Maven or Gradle commands −

For Maven, use the command given below −

mvn clean install


After “BUILD SUCCESS”, you can find the JAR file under the target
directory.

For Gradle, use the command given below −

gradle clean build

After “BUILD SUCCESSFUL”, you can find the JAR file under the
build/libs directory.

Run the JAR file by using the command shown −

java –jar <JARFILE>

Now, the application has started on the Tomcat port 9411 as


shown below −

Now, hit the below URL and see the Zipkin server UI.

[Link]

Then, add the following dependency in your client service


application and point out the Zipkin Server URL to trace the
microservice logs via Zipkin UI.

Now, add the Spring Cloud Starter Zipkin dependency in your


build configuration file as shown −

Maven users can add the following dependency in [Link] file −

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-cloud-sleuth-zipkin</artifactId>
</dependency>

Gradle users can add the below dependency in [Link] file −

compile('[Link]:spring-cloud-sleuth-zipkin')

Now, add the Always Sampler Bean in your Spring Boot


application to export the logs into Zipkin server.

@Bean
public AlwaysSampler defaultSampler() {
return new AlwaysSampler();
}

If you add the AlwaysSampler Bean, then automatically Spring


Sleuth Zipkin Export option will change from false to true.

Next, configure your Zipkin Server base URL in client service


[Link] file.

[Link] = [Link]

Then, provide the trace id and find the traces in Zipkin UI.

[Link]

Spring Boot - Flyway Database


Flyway is a version control application to evolve your Database
schema easily and reliably across all your instances. To learn
more about Flyway, you can use the link − [Link]

Many software projects use relational databases. This requires the


handling of database migrations, also often called schema
migrations.

In this chapter, you are going to learn in detail about how to


configure Flyway database in your Spring Boot application.

Configuring Flyway Database


First, download the Spring Boot project from Spring Initializer
page [Link] and choose the following dependencies

 Spring Boot Starter Web


 Flyway
 MySQL
 JDBC

Maven users can add the following dependencies in [Link] file.

<dependency>
<groupId>[Link]</groupId>
<artifactId>flyway-core</artifactId>
</dependency>

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

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

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

Gradle users can add the following dependencies in [Link]


file.

compile('[Link]:flyway-core')
compile('[Link]:spring-boot-starter-
jdbc')
compile('[Link]:spring-boot-starter-
web')
compile('mysql:mysql-connector-java')

In application properties, we need to configure the database


properties for creating a DataSource and also flyway properties
we need to configure in application properties.

For properties file users, add the below properties in the


[Link] file.

[Link] = flywayapp

[Link] =
[Link]
[Link] =
jdbc:mysql://localhost:3306/USERSERVICE?
autoreconnect=true
[Link] = root
[Link] = root
[Link] = true
[Link] = true
[Link] = 60000
[Link] = 30000
[Link] = SELECT 1
[Link]-active = 15
[Link]-idle = 10
[Link]-wait = 8000

[Link] = jdbc:mysql://localhost:3306/mysql
[Link] = USERSERVICE
[Link] = root
[Link] = root

YAML users can add the following properties in [Link]


file.

spring:
application:
name: flywayapp
datasource:
driverClassName: [Link]
url: "jdbc:mysql://localhost:3306/USERSERVICE?
autoreconnect=true"
password: "root"
username: "root"
testOnBorrow: true
testWhileIdle: true
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 30000
validationQuery: SELECT 1
max-active: 15
max-idle: 10
max-wait: 8000
flyway:
url: jdbc:mysql://localhost:3306/mysql
schemas: USERSERVICE
user: "root"
password: "root"

Now, create a SQL file under


the src/main/resources/db/migration directory. Name the SQL
file as “V1__Initial.sql”

CREATE TABLE USERS (ID INT AUTO_INCREMENT PRIMARY KEY,


USERID VARCHAR(45));
INSERT INTO USERS (ID, USERID) VALUES (1,
'[Link]');

The main Spring Boot application class file code is given below −

package [Link];

import [Link];
import
[Link]
ation;

@SpringBootApplication
public class FlywayappApplication {
public static void main(String[] args) {
[Link]([Link],
args);
}
}

The complete build configuration file is given below.

Maven – [Link]

<?xml version = "1.0" encoding = "UTF-8"?>


<project xmlns = "[Link]
xmlns:xsi = "[Link]
instance"
xsi:schemaLocation =
"[Link]
[Link]

<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>flywayapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>flywayapp</name>
<description>Demo project for Spring
Boot</description>

<parent>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-parent</artifactId>
<version>[Link]</version>
<relativePath/> <!-- lookup parent from
repository -->
</parent>

<properties>

<[Link]>UTF-8</[Link]
ceEncoding>

<[Link]>UTF-8</[Link]
[Link]>
<[Link]>1.8</[Link]>
</properties>

<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>flyway-core</artifactId>
</dependency>

<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

<dependency>
<groupId>[Link]</groupId>

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

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

Gradle – [Link]

buildscript {
ext {
springBootVersion = '[Link]'
}
repositories {
mavenCentral()
}
dependencies {
classpath("[Link]:spring-boot-
gradle-plugin:${springBootVersion}")
}
}

apply plugin: 'java'


apply plugin: 'eclipse'
apply plugin: '[Link]'

group = '[Link]'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
mavenCentral()
}
dependencies {
compile('[Link]:flyway-core')
compile('[Link]:spring-boot-
starter-jdbc')
compile('[Link]:spring-boot-
starter-web')
compile('mysql:mysql-connector-java')
testCompile('[Link]:spring-boot-
starter-test')
}

You can create an executable JAR file, and run the Spring Boot
application by using the Maven or Gradle commands given below

For Maven, you can use the command shown here −

mvn clean install

After “BUILD SUCCESS”, you can find the JAR file under the target
directory.
For Gradle, you can use the command shown here −

gradle clean build

After “BUILD SUCCESSFUL”, you can find the JAR file under the
build/libs directory.

Now, run the JAR file by using the following command −

java –jar <JARFILE>

Now, Tomcat started on the port 8080 and in the console window
you can see the flyway database logs as shown here.

You can now go to the database and do the select queries.

Spring Boot - Sending Email


By using Spring Boot RESTful web service, you can send an email
with Gmail Transport Layer Security. In this chapter, let us
understand in detail how to use this feature.

First, we need to add the Spring Boot Starter Mail dependency in


your build configuration file.

Maven users can add the following dependency into the [Link]
file.
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

Gradle users can add the following dependency in your


[Link] file.

compile('[Link]:spring-boot-starter-mail')

The code of main Spring Boot application class file is given below

package [Link];

import [Link];
import
[Link]
ation;

@SpringBootApplication
public class EmailappApplication {
public static void main(String[] args) {
[Link]([Link],
args);
}
}

You can write a simple Rest API to send to email in Rest Controller
class file as shown.

package [Link];

import
[Link];
import
[Link];

@RestController
public class EmailController {
@RequestMapping(value = "/sendemail")
public String sendEmail() {
return "Email sent successfully";
}
}

You can write a method to send the email with Attachment.


Define the [Link] properties and used
PasswordAuthentication.

private void sendmail() throws AddressException,


MessagingException, IOException {
Properties props = new Properties();
[Link]("[Link]", "true");
[Link]("[Link]", "true");
[Link]("[Link]", "[Link]");
[Link]("[Link]", "587");

Session session = [Link](props, new


[Link]() {
protected PasswordAuthentication
getPasswordAuthentication() {
return new
PasswordAuthentication("tutorialspoint@[Link]",
"<your password>");
}
});
Message msg = new MimeMessage(session);
[Link](new
InternetAddress("tutorialspoint@[Link]", false));

[Link]([Link],
[Link]("tutorialspoint@[Link]"));
[Link]("Tutorials point email");
[Link]("Tutorials point email",
"text/html");
[Link](new Date());

MimeBodyPart messageBodyPart = new MimeBodyPart();


[Link]("Tutorials point email",
"text/html");
Multipart multipart = new MimeMultipart();
[Link](messageBodyPart);
MimeBodyPart attachPart = new MimeBodyPart();

[Link]("/var/tmp/[Link]");
[Link](attachPart);
[Link](multipart);
[Link](msg);
}

Now, call the above sendmail() method from the Rest API as
shown −

@RequestMapping(value = "/sendemail")
public String sendEmail() throws AddressException,
MessagingException, IOException {
sendmail();
return "Email sent successfully";
}

Note − Please switch ON allow less secure apps in your Gmail


account settings before sending an email.

The complete build configuration file is given below.

Maven – [Link]

<?xml version = "1.0" encoding = "UTF-8"?>


<project xmlns = "[Link]
xmlns:xsi = "[Link]
instance"
xsi:schemaLocation =
"[Link]
[Link]

<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>emailapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>emailapp</name>
<description>Demo project for Spring
Boot</description>

<parent>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-parent</artifactId>
<version>[Link]</version>
<relativePath/> <!-- lookup parent from
repository -->
</parent>

<properties>

<[Link]>UTF-8</[Link]
ceEncoding>

<[Link]>UTF-8</[Link]
[Link]>
<[Link]>1.8</[Link]>
</properties>

<dependencies>
<dependency>
<groupId>[Link]</groupId>

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

<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

Gradle – [Link]

buildscript {
ext {
springBootVersion = '[Link]'
}
repositories {
mavenCentral()
}
dependencies {
classpath("[Link]:spring-boot-
gradle-plugin:${springBootVersion}")
}
}

apply plugin: 'java'


apply plugin: 'eclipse'
apply plugin: '[Link]'

group = '[Link]'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
mavenCentral()
}
dependencies {
compile('[Link]:spring-boot-
starter-web')
compile('[Link]:spring-boot-
starter-mail')
testCompile('[Link]:spring-boot-
starter-test')
}

Now, you can create an executable JAR file, and run the Spring
Boot application by using the Maven or Gradle commands shown
below −

For Maven, you can use the command as shown −

mvn clean install

After “BUILD SUCCESS”, you can find the JAR file under the target
directory.

For Gradle, you can use the command as shown −

gradle clean build

After “BUILD SUCCESSFUL”, you can find the JAR file under the
build/libs directory.

Now, run the JAR file by using the command given below −

java –jar <JARFILE>

You can see that the application has started on the Tomcat port
8080.

Now hit the following URL from your web browser and you will
receive an email.

[Link]
Spring Boot - Hystrix
Hystrix is a library from Netflix. Hystrix isolates the points of
access between the services, stops cascading failures across
them and provides the fallback options.

For example, when you are calling a 3rd party application, it takes
more time to send the response. So at that time, the control goes
to the fallback method and returns the custom response to your
application.

In this chapter you are going to see How to implement the Hystrix
in a Spring Boot application.

First, we need to add the Spring Cloud Starter Hystrix dependency


in our build configuration file.

Maven users can add the following dependency in the [Link]


file −

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>

Gradle users can add the following dependency in the [Link]


file −

compile('[Link]:spring-cloud-starter-hystrix')

Now, add the @EnableHystrix annotation into your main Spring


Boot application class file. The @EnableHystrix annotation is used
to enable the Hystrix functionalities into your Spring Boot
application.

The main Spring Boot application class file code is given below −

package [Link];

import [Link];
import
[Link]
ation;
import
[Link]
;

@SpringBootApplication
@EnableHystrix
public class HystrixappApplication {
public static void main(String[] args) {

[Link]([Link],
args);
}
}

Now write a simple Rest Controller such that it returns the String
after 3 seconds from the requested time.

@RequestMapping(value = "/")
public String hello() throws InterruptedException {
[Link](3000);
return "Welcome Hystrix";
}

Now, add the @Hystrix command and @HystrixProperty for the


Rest API and define the timeout in milliseconds value.

@HystrixCommand(fallbackMethod = "fallback_hello",
commandProperties = {
@HystrixProperty(name =
"[Link]",
value = "1000")
})

Next, define the fallback method fallback_hello() if the request


takes a long time to respond.

private String fallback_hello() {


return "Request fails. It takes long time to
response";
}

The complete Rest Controller class file that contains REST API and
Hystrix properties is shown here −

@RequestMapping(value = "/")
@HystrixCommand(fallbackMethod = "fallback_hello",
commandProperties = {
@HystrixProperty(name =
"[Link]",
value = "1000")
})
public String hello() throws InterruptedException {
[Link](3000);
return "Welcome Hystrix";
}
private String fallback_hello() {
return "Request fails. It takes long time to
response";
}

In this example, REST API written in main Spring Boot application


class file itself.

package [Link];

import [Link];
import
[Link]
Property;
import
[Link]
ation;
import
[Link]
;
import
[Link];
import
[Link];

import
[Link]
Command;

@SpringBootApplication
@EnableHystrix
@RestController
public class HystrixappApplication {
public static void main(String[] args) {

[Link]([Link],
args);
}
@RequestMapping(value = "/")
@HystrixCommand(fallbackMethod = "fallback_hello",
commandProperties = {
@HystrixProperty(name =
"[Link]",
value = "1000")
})
public String hello() throws InterruptedException {
[Link](3000);
return "Welcome Hystrix";
}
private String fallback_hello() {
return "Request fails. It takes long time to
response";
}
}

The complete build configuration file is given below.

Maven – [Link] file

<?xml version = "1.0" encoding = "UTF-8"?>


<project xmlns = "[Link]
xmlns:xsi = "[Link]
instance"
xsi:schemaLocation =
"[Link]
[Link]

<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>hystrixapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>hystrixapp</name>
<description>Demo project for Spring
Boot</description>

<parent>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-parent</artifactId>
<version>[Link]</version>
<relativePath/> <!-- lookup parent from
repository -->
</parent>

<properties>
<[Link]>UTF-8</[Link]
ceEncoding>

<[Link]>UTF-8</[Link]
[Link]>
<[Link]>1.8</[Link]>
<[Link]>[Link]</spring-
[Link]>
</properties>

<dependencies>
<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>

<groupId>[Link]</groupId>

<artifactId>spring-cloud-dependencies</artifactId>
<version>${[Link]}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

Gradle – [Link]

buildscript {
ext {
springBootVersion = '[Link]'
}
repositories {
mavenCentral()
}
dependencies {
classpath("[Link]:spring-boot-
gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: '[Link]'

group = '[Link]'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
mavenCentral()
}
ext {
springCloudVersion = '[Link]'
}
dependencies {
compile('[Link]:spring-cloud-
starter-hystrix')
compile('[Link]:spring-boot-
starter-web')
testCompile('[Link]:spring-boot-
starter-test')
}
dependencyManagement {
imports {
mavenBom "[Link]:spring-cloud-
dependencies:${springCloudVersion}"
}
}

You can create an executable JAR file, and run the Spring Boot
application by using the following Maven or Gradle commands −

For Maven, use the command as shown −

mvn clean install

After “BUILD SUCCESS”, you can find the JAR file under the target
directory.

For Gradle, use the command as shown −

gradle clean build

After “BUILD SUCCESSFUL”, you can find the JAR file under the
build/libs directory.

Now, run the JAR file by using the command given below −

java –jar <JARFILE>

This will start the application on the Tomcat port 8080 as shown
below −

Now, hit the URL [Link] from your web browser,


and see the Hystrix response. The API takes 3 seconds to
respond, but Hystrix timeout is 1 second.
Spring Boot - Web Socket
In this chapter, let us understand how to build an interactive web
application by using Spring Boot with Web sockets.

To build an interactive web application in Spring Boot with Web


socket, you need to add the following dependencies.

Maven users should add the following dependencies in the


[Link] file.

<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>webjars-locator</artifactId>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>sockjs-client</artifactId>
<version>1.0.2</version>
</dependency>

<dependency>
<groupId>[Link]</groupId>
<artifactId>stomp-websocket</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.7</version> </dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>jquery</artifactId>
<version>3.1.0</version>
</dependency>

Gradle users can add the following dependencies in your


[Link] file −

compile("[Link]:spring-boot-starter-
websocket")
compile("[Link]:webjars-locator")
compile("[Link]:sockjs-client:1.0.2")
compile("[Link]:stomp-websocket:2.3.3")
compile("[Link]:bootstrap:3.3.7")
compile("[Link]:jquery:3.1.0")

Let us create a Message handling controller to work with STOMP


messaging. STOMP messages can be routed to @Controller class
file. For example, GreetingController is mapped to handle the
messages to destination “/hello”.

package [Link];

import
[Link]
eMapping;
import
[Link]
;
import [Link];

@Controller
public class GreetingController {
@MessageMapping("/hello")
@SendTo("/topic/greetings")
public Greeting greeting(HelloMessage message)
throws Exception {
[Link](1000); // simulated delay
return new Greeting("Hello, " + [Link]()
+ "!");
}
}
Now, configure Spring for STOMP messaging. Write a
WebSocketConfig class file that extends the
AbstractWebSocketMessageBrokerConfigurer class as shown
below.

package [Link];

import
[Link];
import
[Link]
Registry;
import
[Link]
ctWebSocketMessageBrokerConfigurer;
import
[Link]
WebSocketMessageBroker;
import
[Link]
ndpointRegistry;

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends
AbstractWebSocketMessageBrokerConfigurer {
@Override
public void
configureMessageBroker(MessageBrokerRegistry config) {
[Link]("/topic");
[Link]("/app");
}
@Override
public void
registerStompEndpoints(StompEndpointRegistry registry)
{
[Link]("/tutorialspoint-
websocket").withSockJS();
}
}

The @EnableWebSocketMessageBroker annotation is used to


configure the Web socket message broker to create STOMP
endpoints.
You can create a browser client file under the
src/main/resources/static/[Link] as shown −

<!DOCTYPE html>
<html>
<head>
<title>Hello WebSocket</title>
<link href =
"/webjars/bootstrap/css/[Link]" rel =
"stylesheet">
<link href = "/[Link]" rel = "stylesheet">
<script src =
"/webjars/jquery/[Link]"></script>
<script src =
"/webjars/sockjs-client/[Link]"></script>
<script src =
"/webjars/stomp-websocket/[Link]"></script>
<script src = "/[Link]"></script>
</head>

<body>
<noscript>
<h2 style = "color: #ff0000">
Seems your browser doesn't support
Javascript! Websocket relies on Javascript being
enabled. Please enable Javascript and
reload this page!
</h2>
</noscript>
<div id = "main-content" class = "container">
<div class = "row">
<div class = "col-md-6">
<form class = "form-inline">
<div class = "form-group">
<label for = "connect">WebSocket
connection:</label>
<button id = "connect" class =
"btn btn-default" type = "submit">Connect</button>
<button id = "disconnect" class =
"btn btn-default" type = "submit" disabled =
"disabled">Disconnect
</button>
</div>
</form>
</div>

<div class = "col-md-6">


<form class = "form-inline">
<div class = "form-group">
<label for = "name">What is your
name?</label>
<input type = "text" id = "name"
class = "form-control" placeholder = "Your name
here...">
</div>
<button id = "send" class = "btn btn-
default" type = "submit">Send</button>
</form>
</div>
</div>

<div class = "row">


<div class = "col-md-12">
<table id = "conversation" class =
"table table-striped">
<thead>
<tr>
<th>Greetings</th>
</tr>
</thead>
<tbody id = "greetings"></tbody>
</table>
</div>
</div>
</div>
</body>
</html>

Let us create an [Link] file to consume and produce the messages


by using STOMP.

var stompClient = null;

function setConnected(connected) {
$("#connect").prop("disabled", connected);
$("#disconnect").prop("disabled", !connected);
if (connected) {
$("#conversation").show();
} else {
$("#conversation").hide();
}
$("#greetings").html("");
}

function connect() {
var socket = new SockJS('/tutorialspoint-
websocket');
stompClient = [Link](socket);
[Link]({}, function (frame) {
setConnected(true);
[Link]('Connected: ' + frame);
[Link]('/topic/greetings',
function (greeting) {

showGreeting([Link]([Link]).content);
});
});
}
function disconnect() {
if (stompClient !== null) {
[Link]();
}
setConnected(false);
[Link]("Disconnected");
}
function sendName() {
[Link]("/app/hello", {},
[Link]({'name': $("#name").val()}));
}
function showGreeting(message) {
$("#greetings").append("<tr><td>" + message +
"</td></tr>");
}
$(function () {
$( "form" ).on('submit', function (e)
{[Link]();});
$( "#connect" ).click(function() { connect(); });
$( "#disconnect" ).click(function()
{ disconnect(); });
$( "#send" ).click(function() { sendName(); });
});

The code for main Spring Boot application is shown below.

package [Link];

import [Link];
import
[Link]
ation;

@SpringBootApplication
public class WebsocketappApplication {
public static void main(String[] args) {

[Link]([Link],
args);
}
}

The complete build configuration file is given below.

Maven – [Link]

<?xml version = "1.0" encoding = "UTF-8"?>


<project xmlns = "[Link]
xmlns:xsi = "[Link]
instance"
xsi:schemaLocation =
"[Link]
[Link]

<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>websocketapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>websocketapp</name>
<description>Demo project for Spring
Boot</description>
<parent>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-parent</artifactId>
<version>[Link]</version>
</parent>

<dependencies>
<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>webjars-locator</artifactId>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>sockjs-client</artifactId>
<version>1.0.2</version>
</dependency>

<dependency>
<groupId>[Link]</groupId>
<artifactId>stomp-websocket</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.7</version>
</dependency>

<dependency>
<groupId>[Link]</groupId>
<artifactId>jquery</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<properties>
<[Link]>1.8</[Link]>
</properties>

<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

Gradle – [Link]

buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("[Link]:spring-boot-
gradle-plugin:[Link]")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: '[Link]'

jar {
baseName = 'websocketapp'
version = '0.1.0'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
mavenCentral()
}
dependencies {
compile("[Link]:spring-boot-
starter-websocket")
compile("[Link]:webjars-locator")
compile("[Link]:sockjs-client:1.0.2")
compile("[Link]:stomp-websocket:2.3.3")
compile("[Link]:bootstrap:3.3.7")
compile("[Link]:jquery:3.1.0")

testCompile("[Link]:spring-boot-
starter-test")
}

Spring Boot - Batch Service


You can create an executable JAR file, and run the Spring Boot
application by using the Maven or Gradle commands as shown
below −

For Maven, you can use the command given below −

mvn clean install

After “BUILD SUCCESS”, you can find the JAR file under the target
directory.

For Gradle, you can use the command as shown −

gradle clean build

After “BUILD SUCCESSFUL”, you can find the JAR file under the
build/libs directory.

Run the JAR file by using the command given here −

java –jar <JARFILE>

Now, the application has started on the Tomcat port 8080 as


shown.
Now, hit the URL [Link] in your web browser
and connect the web socket and send the greeting and receive
the message.

Batch Service is a process to execute more than one command in


a single task. In this chapter, you are going to learn how to create
batch service in a Spring Boot application.

Let us consider an example where we are going to save the CSV


file content into HSQLDB.

To create a Batch Service program, we need to add the Spring


Boot Starter Batch dependency and HSQLDB dependency in our
build configuration file.

Maven users can add the following dependencies in [Link] file.

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>hsqldb</artifactId>
</dependency>

Gradle users can add the following dependencies in [Link]


file.
compile("[Link]:spring-boot-starter-batch")
compile("[Link]:hsqldb")

Now, add the simple CSV data file under classpath resources –
src/main/resources and name the file as [Link] as shown −

William,John
Mike, Sebastian
Lawarance, Lime

Next, write a SQL script for HSQLDB – under the classpath


resource directory – request_fail_hystrix_timeout

DROP TABLE USERS IF EXISTS;


CREATE TABLE USERS (
user_id BIGINT IDENTITY NOT NULL PRIMARY KEY,
first_name VARCHAR(20),
last_name VARCHAR(20)
);

Create a POJO class for USERS model as shown −

package [Link];
public class User {
private String lastName;
private String firstName;

public User() {
}
public User(String firstName, String lastName) {
[Link] = firstName;
[Link] = lastName;
}
public void setFirstName(String firstName) {
[Link] = firstName;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
[Link] = lastName;
}

@Override
public String toString() {
return "firstName: " + firstName + ", lastName: " + lastName;
}
}

Now, create an intermediate processor to do the operations after


the reading the data from the CSV file and before writing the data
into SQL.

package [Link];

import [Link];
import [Link];
import [Link];

public class UserItemProcessor implements


ItemProcessor<User, User> {
private static final Logger log =
[Link]([Link]);

@Override
public User process(final User user) throws
Exception {
final String firstName =
[Link]().toUpperCase();
final String lastName =
[Link]().toUpperCase();
final User transformedPerson = new
User(firstName, lastName);

[Link]("Converting (" + user + ") into (" +


transformedPerson + ")");
return transformedPerson;
}
}
Let us create a Batch configuration file, to read the data from CSV
and write into the SQL file as shown below. We need to add the
@EnableBatchProcessing annotation in the configuration class
file. The @EnableBatchProcessing annotation is used to enable
the batch operations for your Spring Boot application.

package [Link];

import [Link];
import [Link];
import [Link];

import
[Link]
.EnableBatchProcessing;
import
[Link]
.JobBuilderFactory;
import
[Link]
.StepBuilderFactory;
import
[Link]
ementer;
import
[Link]
mSqlParameterSourceProvider;
import
[Link]
iter;
import
[Link];
import
[Link]
FieldSetMapper;
import
[Link]
Mapper;
import
[Link]
LineTokenizer;
import
[Link];
import [Link];
import
[Link];
import [Link];

@Configuration
@EnableBatchProcessing
public class BatchConfiguration {
@Autowired
public JobBuilderFactory jobBuilderFactory;

@Autowired
public StepBuilderFactory stepBuilderFactory;

@Autowired
public DataSource dataSource;

@Bean
public FlatFileItemReader<User> reader() {
FlatFileItemReader<User> reader = new
FlatFileItemReader<User>();
[Link](new
ClassPathResource("[Link]"));
[Link](new
DefaultLineMapper<User>() {
{
setLineTokenizer(new
DelimitedLineTokenizer() {
{
setNames(new String[] { "firstName",
"lastName" });
}
});
setFieldSetMapper(new
BeanWrapperFieldSetMapper<User>() {
{
setTargetType([Link]);
}
});
}
});
return reader;
}
@Bean
public UserItemProcessor processor() {
return new UserItemProcessor();
}
@Bean
public JdbcBatchItemWriter<User> writer() {
JdbcBatchItemWriter<User> writer = new
JdbcBatchItemWriter<User>();
[Link](new
BeanPropertyItemSqlParameterSourceProvider<User>());
[Link]("INSERT INTO USERS (first_name,
last_name) VALUES (:firstName, :lastName)");
[Link](dataSource);
return writer;
}
@Bean
public Job
importUserJob(JobCompletionNotificationListener
listener) {
return
[Link]("importUserJob").incrementer(
new
RunIdIncrementer()).listener(listener).flow(step1()).en
d().build();
}
@Bean
public Step step1() {
return [Link]("step1").<User,
User>chunk(10).reader(reader()).processor(processor()).
writer(writer()).build();
}
}

The reader() method is used to read the data from the CSV file
and writer() method is used to write a data into the SQL.

Next, we will have to write a Job Completion Notification Listener


class – used to notify after the Job completion.

package [Link];

import [Link];
import [Link];
import [Link];

import [Link];
import [Link];

import [Link];
import [Link];
import
[Link]
tenerSupport;
import
[Link];
import [Link];
import [Link];
import [Link];

@Component
public class JobCompletionNotificationListener extends
JobExecutionListenerSupport {
private static final Logger log =
[Link](JobCompletionNotificationListen
[Link]);
private final JdbcTemplate jdbcTemplate;

@Autowired
public
JobCompletionNotificationListener(JdbcTemplate
jdbcTemplate) {
[Link] = jdbcTemplate;
}
@Override
public void afterJob(JobExecution jobExecution) {
if ([Link]() ==
[Link]) {
[Link]("!!! JOB FINISHED !! It's time to
verify the results!!");

List<User> results = [Link](


"SELECT first_name, last_name FROM USERS",
new RowMapper<User>() {

@Override
public User mapRow(ResultSet rs, int row)
throws SQLException {
return new User([Link](1),
[Link](2));
}
});

for (User person : results) {


[Link]("Found <" + person + "> in the
database.");
}
}
}
}

Now, create an executable JAR file, and run the Spring Boot
application by using the following Maven or Gradle commands.

For Maven, use the command as shown −

mvn clean install

After “BUILD SUCCESS”, you can find the JAR file under the target
directory.

For Gradle, you can use the command as shown −

gradle clean build

After “BUILD SUCCESSFUL”, you can find the JAR file under the
build/libs directory.

Run the JAR file by using the command given here −

java –jar <JARFILE>

You can see the output in console window as shown −

Spring Boot - Apache Kafka


Apache Kafka is an open source project used to publish and
subscribe the messages based on the fault-tolerant messaging
system. It is fast, scalable and distributed by design. If you are a
beginner to Kafka, or want to gain a better understanding on it,
please refer to this link − [Link]/apache_kafka/

In this chapter, we are going to see how to implement the Apache


Kafka in Spring Boot application.

First, we need to add the Spring Kafka dependency in our build


configuration file.

Maven users can add the following dependency in the [Link]


file.

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-kafka</artifactId>
<version>[Link]</version>
</dependency>

Gradle users can add the following dependency in the [Link]


file.

compile group: '[Link]', name: 'spring-kafka', version:


'[Link]'

Producing Messages
To produce messages into Apache Kafka, we need to define the
Configuration class for Producer configuration as shown −

package [Link];

import [Link];
import [Link];

import
[Link];
import
[Link];
import [Link];
import
[Link];
import
[Link]
ory;
import [Link];
import [Link];

@Configuration
public class KafkaProducerConfig {
@Bean
public ProducerFactory<String, String>
producerFactory() {
Map<String, Object> configProps = new
HashMap<>();

[Link](ProducerConfig.BOOTSTRAP_SERVERS_CONFIG
, "localhost:9092");

[Link](ProducerConfig.KEY_SERIALIZER_CLASS_CON
FIG, [Link]);

[Link](ProducerConfig.VALUE_SERIALIZER_CLASS_C
ONFIG, [Link]);
return new
DefaultKafkaProducerFactory<>(configProps);
}
@Bean
public KafkaTemplate<String, String> kafkaTemplate()
{
return new KafkaTemplate<>(producerFactory());
}
}

To publish a message, auto wire the Kafka Template object and


produce the message as shown.

@Autowired
private KafkaTemplate<String, String> kafkaTemplate;

public void sendMessage(String msg) {


[Link](topicName, msg);
}
Consuming a Message
To consume messages, we need to write a Consumer
configuration class file as shown below.

package [Link];

import [Link];
import [Link];

import
[Link];
import
[Link]
r;
import [Link];
import
[Link];
import
[Link];
import
[Link]
rContainerFactory;
import [Link];
import
[Link]
ory;

@EnableKafka
@Configuration
public class KafkaConsumerConfig {
@Bean
public ConsumerFactory<String, String>
consumerFactory() {
Map<String, Object> props = new HashMap<>();

[Link](ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,
"localhost:2181");
[Link](ConsumerConfig.GROUP_ID_CONFIG, "group-
id");

[Link](ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,
[Link]);
[Link](ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFI
G, [Link]);
return new DefaultKafkaConsumerFactory<>(props);
}
@Bean
public
ConcurrentKafkaListenerContainerFactory<String, String>
kafkaListenerContainerFactory() {
ConcurrentKafkaListenerContainerFactory<String,
String>
factory = new
ConcurrentKafkaListenerContainerFactory<>();
[Link](consumerFactory());
return factory;
}
}

Next, write a Listener to listen to the messages.

@KafkaListener(topics = "tutorialspoint", groupId =


"group-id")
public void listen(String message) {
[Link]("Received Messasge in group -
group-id: " + message);
}

Let us call the sendMessage() method from ApplicationRunner


class run method from the main Spring Boot application class file
and consume the message from the same class file.

Your main Spring Boot application class file code is given below −

package [Link];

import
[Link];
import [Link];
import [Link];
import [Link];
import
[Link]
ation;
import
[Link];
import [Link];

@SpringBootApplication
public class KafkaDemoApplication implements
ApplicationRunner {
@Autowired
private KafkaTemplate<String, String> kafkaTemplate;

public void sendMessage(String msg) {


[Link]("tutorialspoint", msg);
}
public static void main(String[] args) {
[Link]([Link],
args);
}
@KafkaListener(topics = "tutorialspoint", groupId =
"group-id")
public void listen(String message) {
[Link]("Received Messasge in group -
group-id: " + message);
}
@Override
public void run(ApplicationArguments args) throws
Exception {
sendMessage("Hi Welcome to Spring For Apache
Kafka");
}
}

The code for complete build configuration file is given below.

Maven – [Link]

<?xml version = "1.0" encoding = "UTF-8"?>


<project xmlns = "[Link]
xmlns:xsi = "[Link]
instance"
xsi:schemaLocation =
"[Link]
[Link]

<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>kafka-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>kafka-demo</name>
<description>Demo project for Spring
Boot</description>

<parent>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-parent</artifactId>
<version>[Link]</version>
<relativePath /> <!-- lookup parent from
repository -->
</parent>

<properties>

<[Link]>UTF-8</[Link]
ceEncoding>

<[Link]>UTF-8</[Link]
[Link]>
<[Link]>1.8</[Link]>
</properties>

<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-kafka</artifactId>
<version>[Link]</version>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

Gradle – [Link]

buildscript {
ext {
springBootVersion = '[Link]'
}
repositories {
mavenCentral()
}
dependencies {
classpath("[Link]:spring-boot-
gradle-plugin:${springBootVersion}")
}
}

apply plugin: 'java'


apply plugin: 'eclipse'
apply plugin: '[Link]'

group = '[Link]'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
mavenCentral()
}
dependencies {
compile('[Link]:spring-boot-
starter')
compile group: '[Link]', name:
'spring-kafka', version: '[Link]'
testCompile('[Link]:spring-boot-
starter-test')
}

Now, create an executable JAR file, and run the Spring Boot
application by using the below Maven or Gradle commands as
shown −

For Maven, use the command as shown −

mvn clean install

After “BUILD SUCCESS”, you can find the JAR file under the target
directory.

For Gradle, use the command as shown −

gradle clean build

After “BUILD SUCCESSFUL”, you can find the JAR file under the
build/libs directory.

Run the JAR file by using the command given here −

java –jar <JARFILE>

You can see the output in console window.

Spring Boot - Twilio


Twilio is a 3rd party application used to send SMS and make voice
calls from our application. It allows us to send the SMS and make
voice calls programmatically.

In this chapter, you are going to learn how to implement the SMS
sending and making voice calls by using Spring Boot with Twilio.
Note − We used the Trail account in Twilio to send the SMS and
making voice calls. You can learn more about Twilio
at [Link].

First, we need to add the Twilio dependency in our build


configuration file.

Maven users can add the following dependency in the [Link]


file.

<dependency>
<groupId>[Link]</groupId>
<artifactId>twilio</artifactId>
<version>7.16.1</version>
</dependency>

Gradle users can add the following dependency in the [Link]


file.

compile group: "[Link]", name:"twilio", version: "7.16.1"

Now, initialize the Twilio account with ACCOUNT_SID and AUTH_ID


in static block as shown −

static {
[Link](ACCOUNT_SID, AUTH_ID);
}

Sending SMS
To send the SMS, we need to provide a from-number and to-
number to the [Link]() method. Message body content
also we need to provide for the method [Link]()as
shown −

[Link](new PhoneNumber("to-number"), new PhoneNumber("from-


number"),
"Message from Spring Boot Application").create();

The main Spring Boot application class file looks below.


package [Link];

import [Link];
import [Link];
import [Link];
import
[Link]
ation;

import [Link];
import [Link];
import [Link];

@SpringBootApplication
public class SmsdemoApplication implements
ApplicationRunner {
private final static String ACCOUNT_SID = "<your-
account-sid>";
private final static String AUTH_ID = "<your-auth-
id>";

static {
[Link](ACCOUNT_SID, AUTH_ID);
}
public static void main(String[] args) {
[Link]([Link],
args);
}
@Override
public void run(ApplicationArguments arg0) throws
Exception {
[Link](new PhoneNumber("to-number"), new
PhoneNumber("from-number"),
"Message from Spring Boot
Application").create();
}
}

The complete code to build configuration file is given below −

Maven – [Link]
<?xml version = "1.0" encoding = "UTF-8"?>
<project xmlns = "[Link]
xmlns:xsi = "[Link]
instance"
xsi:schemaLocation =
"[Link]
[Link]

<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>smsdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>smsdemo</name>
<description>Demo project for Spring
Boot</description>

<parent>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-parent</artifactId>
<version>[Link]</version>
<relativePath /> <!-- lookup parent from
repository -->
</parent>

<properties>

<[Link]>UTF-8</[Link]
ceEncoding>

<[Link]>UTF-8</[Link]
[Link]>
<[Link]>1.8</[Link]>
</properties>

<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>twilio</artifactId>
<version>7.16.1</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

Gradle – [Link]

buildscript {
ext {
springBootVersion = '[Link]'
}
repositories {
mavenCentral()
}
dependencies {
classpath("[Link]:spring-boot-
gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: '[Link]'

group = '[Link]'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
mavenCentral()
}
dependencies {
compile('[Link]:spring-boot-
starter')
testCompile('[Link]:spring-boot-
starter-test')
compile group: "[Link]", name:"twilio",
version: "7.11.+"
}

You can create an executable JAR file, and run the spring boot
application by using the following Maven or Gradle commands −

For Maven, use the command as shown −

mvn clean install

After “BUILD SUCCESS”, you can find the JAR file under the target
directory.

For Gradle, use the command as shown −

gradle clean build

After “BUILD SUCCESSFUL”, you can find the JAR file under the
build/libs directory.

Run the JAR file by using the command as given below −

java –jar <JARFILE>

Now, you will receive the SMS to your “to-number”.

Message received to “to-number”.

Sent from your Twilio trail account


- Message from Spring Boot Application
Note − In this example, we used the Trail account. So, you should
verify the numbers before sending the SMS.

Voice Calls
To make voice calls by using Twilio, we need to call the
[Link]() method. For this method, we need to provide a to-
number, from-number, and voice-note as shown here.

[Link](new PhoneNumber("<to-number>"), new PhoneNumber("<from-


number>"),
new URI("[Link]

The code for main Spring Boot application class file is given
below.

package [Link];

import [Link];

import [Link];
import [Link];
import [Link];
import
[Link]
ation;

import [Link];
import [Link];
import [Link];

@SpringBootApplication
public class SmsdemoApplication implements
ApplicationRunner {
private final static String ACCOUNT_SID = "<ACCOUNT-
SID>";
private final static String AUTH_ID = "AUTH-ID";

static {
[Link](ACCOUNT_SID, AUTH_ID);
}
public static void main(String[] args) {
[Link]([Link],
args);
}
@Override
public void run(ApplicationArguments arg0) throws
Exception {
[Link](new PhoneNumber("<to-number>"), new
PhoneNumber("<from-number>"),
new
URI("[Link]
}
}

The code for complete build configuration file is given below −

Maven – [Link]

<?xml version = "1.0" encoding = "UTF-8"?>


<project xmlns = "[Link]
xmlns:xsi = "[Link]
instance"
xsi:schemaLocation =
"[Link]
[Link]

<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>smsdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>smsdemo</name>
<description>Demo project for Spring
Boot</description>

<parent>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-parent</artifactId>
<version>[Link]</version>
<relativePath /> <!-- lookup parent from
repository -->
</parent>
<properties>

<[Link]>UTF-8</[Link]
ceEncoding>

<[Link]>UTF-8</[Link]
[Link]>
<[Link]>1.8</[Link]>
</properties>

<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>twilio</artifactId>
<version>7.16.1</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

Gradle – [Link]
buildscript {
ext {
springBootVersion = '[Link]'
}
repositories {
mavenCentral()
}
dependencies {
classpath("[Link]:spring-boot-
gradle-plugin:${springBootVersion}")
}
}

apply plugin: 'java'


apply plugin: 'eclipse'
apply plugin: '[Link]'

group = '[Link]'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
mavenCentral()
}
dependencies {
compile('[Link]:spring-boot-
starter')
testCompile('[Link]:spring-boot-
starter-test')
compile group: "[Link]", name:"twilio",
version: "7.11.+"
}

You can create an executable JAR file, and run the Spring Boot
application by using the following Maven or Gradle commands.

For Maven, use the command as shown −

mvn clean install

After “BUILD SUCCESS”, you can find the JAR file under the target
directory.

For Gradle, use the command as shown −


gradle clean build

After “BUILD SUCCESSFUL”, you can find the JAR file under the
build/libs directory.

Now, run the JAR file by using the command given here −

java –jar <JARFILE>

Now, you will receive call to your “to-number” from Twilio.

Press any key after attending the call, you will hear the voice note
from [Link]

Note − In this example, we used the Trail account. So, you should
verify the numbers before making calls.

Spring Boot - Unit Test Cases


Unit Testing is a one of the testing done by the developers to
make sure individual unit or component functionalities are
working fine.

In this tutorial, we are going to see how to write a unit test case
by using Mockito and Web Controller.

Mockito
For injecting Mockito Mocks into Spring Beans, we need to add the
Mockito-core dependency in our build configuration file.

Maven users can add the following dependency in your [Link]


file.

<dependency>
<groupId>[Link]</groupId>
<artifactId>mockito-core</artifactId>
<version>2.13.0</version>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

Gradle users can add the following dependency in the [Link]


file.

compile group: '[Link]', name: 'mockito-core',


version: '2.13.0'
testCompile('[Link]:spring-boot-
starter-test')

The code to write a Service class which contains a method that


returns the String value is given here.

package [Link];

import [Link];

@Service
public class ProductService {
public String getProductName() {
return "Honey";
}
}

Now, inject the ProductService class into another Service class file
as shown.

package [Link];

import
[Link];
import [Link];

@Service
public class OrderService {
@Autowired
ProductService productService;

public OrderService(ProductService productService) {


[Link] = productService;
}
public String getProductName() {
return [Link]();
}
}

The main Spring Boot application class file is given below −

package [Link];

import [Link];
import
[Link]
ation;

@SpringBootApplication
public class MockitoDemoApplication {
public static void main(String[] args) {

[Link]([Link],
args);
}
}

Then, configure the Application context for the tests. The


@Profile(“test”) annotation is used to configure the class when
the Test cases are running.

package [Link];

import [Link];
import [Link];
import
[Link];
import [Link];
import [Link];

@Profile("test")
@Configuration
public class ProductServiceTestConfiguration {
@Bean
@Primary
public ProductService productService() {
return [Link]([Link]);
}
}

Now, you can write a Unit Test case for Order Service under
the src/test/resources package.

package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import
[Link];
import
[Link];
import [Link];
import
[Link].junit4.SpringJUnit4Cla
ssRunner;

@SpringBootTest
@ActiveProfiles("test")
@RunWith([Link])
public class MockitoDemoApplicationTests {
@Autowired
private OrderService orderService;

@Autowired
private ProductService productService;

@Test
public void
whenUserIdIsProvided_thenRetrievedNameIsCorrect() {

[Link]([Link]()).thenRetur
n("Mock Product Name");
String testName = [Link]();
[Link]("Mock Product Name",
testName);
}
}

The complete code for build configuration file is given below.

Maven – [Link]

<?xml version = "1.0" encoding = "UTF-8"?>


<project xmlns = "[Link]
xmlns:xsi = "[Link]
instance"
xsi:schemaLocation =
"[Link]
[Link]

<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>mockito-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>mockito-demo</name>
<description>Demo project for Spring
Boot</description>

<parent>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-parent</artifactId>
<version>[Link]</version>
<relativePath /> <!-- lookup parent from
repository -->
</parent>

<properties>

<[Link]>UTF-8</[Link]
ceEncoding>

<[Link]>UTF-8</[Link]
[Link]>
<[Link]>1.8</[Link]>
</properties>

<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>mockito-core</artifactId>
<version>2.13.0</version>
</dependency>
<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

Gradle – [Link]

buildscript {
ext {
springBootVersion = '[Link]'
}
repositories {
mavenCentral()
}
dependencies {
classpath("[Link]:spring-boot-
gradle-plugin:${springBootVersion}")
}
}

apply plugin: 'java'


apply plugin: 'eclipse'
apply plugin: '[Link]'

group = '[Link]'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
mavenCentral()
}
dependencies {
compile('[Link]:spring-boot-
starter')
compile group: '[Link]', name: 'mockito-core',
version: '2.13.0'
testCompile('[Link]:spring-boot-
starter-test')
}

You can create an executable JAR file, and run the Spring Boot
application by using the following Maven or Gradle1 commands.

For Maven, you can use the command as shown −

mvn clean install

You can see the test results in console window.

For Gradle, you can use the command as shown −

gradle clean build


You can see the rest results in console window.

Spring Boot - Rest Controller


Unit Test
Spring Boot provides an easy way to write a Unit Test for Rest
Controller file. With the help of SpringJUnit4ClassRunner and
MockMvc, we can create a web application context to write Unit
Test for Rest Controller file.

Unit Tests should be written under the src/test/java directory


and classpath resources for writing a test should be placed under
the src/test/resources directory.

For Writing a Unit Test, we need to add the Spring Boot Starter
Test dependency in your build configuration file as shown below.

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

Gradle users can add the following dependency in your


[Link] file.

testCompile('[Link]:spring-boot-starter-test')

Before writing a Test case, we should first build RESTful web


services. For further information on building RESTful web services,
please refer to the chapter on the same given in this tutorial.

Writing a Unit Test for REST Controller


In this section, let us see how to write a Unit Test for the REST
Controller.

First, we need to create Abstract class file used to create web


application context by using MockMvc and define the mapToJson()
and mapFromJson() methods to convert the Java object into JSON
string and convert the JSON string into Java object.

package [Link];

import [Link];
import [Link];
import
[Link];
import
[Link];
import
[Link].junit4.SpringJUnit4Cla
ssRunner;
import
[Link]
n;
import [Link];
import
[Link]
ers;
import
[Link];

import [Link];
import
[Link];
import
[Link];
import [Link];

@RunWith([Link])
@SpringBootTest(classes = [Link])
@WebAppConfiguration
public abstract class AbstractTest {
protected MockMvc mvc;
@Autowired
WebApplicationContext webApplicationContext;
protected void setUp() {
mvc =
[Link](webApplicationContex
t).build();
}
protected String mapToJson(Object obj) throws
JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
return [Link](obj);
}
protected <T> T mapFromJson(String json, Class<T>
clazz)
throws JsonParseException, JsonMappingException,
IOException {

ObjectMapper objectMapper = new ObjectMapper();


return [Link](json, clazz);
}
}

Next, write a class file that extends the AbstractTest class and
write a Unit Test for each method such GET, POST, PUT and
DELETE.

The code for GET API Test case is given below. This API is to view
the list of products.

@Test
public void getProductsList() throws Exception {
String uri = "/products";
MvcResult mvcResult =
[Link]([Link](uri)
.accept(MediaType.APPLICATION_JSON_VALUE)).andRet
urn();

int status = [Link]().getStatus();


assertEquals(200, status);
String content =
[Link]().getContentAsString();
Product[] productlist = [Link](content,
Product[].class);
assertTrue([Link] > 0);
}

The code for POST API test case is given below. This API is to
create a product.

@Test
public void createProduct() throws Exception {
String uri = "/products";
Product product = new Product();
[Link]("3");
[Link]("Ginger");

String inputJson = [Link](product);


MvcResult mvcResult =
[Link]([Link](uri)
.contentType(MediaType.APPLICATION_JSON_VALUE).co
ntent(inputJson)).andReturn();

int status = [Link]().getStatus();


assertEquals(201, status);
String content =
[Link]().getContentAsString();
assertEquals(content, "Product is created
successfully");
}

The code for PUT API Test case is given below. This API is to
update the existing product.

@Test
public void updateProduct() throws Exception {
String uri = "/products/2";
Product product = new Product();
[Link]("Lemon");

String inputJson = [Link](product);


MvcResult mvcResult =
[Link]([Link](uri)
.contentType(MediaType.APPLICATION_JSON_VALUE).co
ntent(inputJson)).andReturn();
int status = [Link]().getStatus();
assertEquals(200, status);
String content =
[Link]().getContentAsString();
assertEquals(content, "Product is updated
successsfully");
}

The code for Delete API Test case is given below. This API will
delete the existing product.

@Test
public void deleteProduct() throws Exception {
String uri = "/products/2";
MvcResult mvcResult =
[Link]([Link](uri)).andRetu
rn();
int status = [Link]().getStatus();
assertEquals(200, status);
String content =
[Link]().getContentAsString();
assertEquals(content, "Product is deleted
successsfully");
}

The full Controller Test class file is given below −

package [Link];

import static [Link];


import static [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import
[Link]
uestBuilders;

import [Link];
public class ProductServiceControllerTest extends
AbstractTest {
@Override
@Before
public void setUp() {
[Link]();
}
@Test
public void getProductsList() throws Exception {
String uri = "/products";
MvcResult mvcResult =
[Link]([Link](uri)
.accept(MediaType.APPLICATION_JSON_VALUE)).and
Return();

int status = [Link]().getStatus();


assertEquals(200, status);
String content =
[Link]().getContentAsString();
Product[] productlist =
[Link](content, Product[].class);
assertTrue([Link] > 0);
}
@Test
public void createProduct() throws Exception {
String uri = "/products";
Product product = new Product();
[Link]("3");
[Link]("Ginger");
String inputJson = [Link](product);
MvcResult mvcResult =
[Link]([Link](uri)
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(inputJson)).andReturn();

int status = [Link]().getStatus();


assertEquals(201, status);
String content =
[Link]().getContentAsString();
assertEquals(content, "Product is created
successfully");
}
@Test
public void updateProduct() throws Exception {
String uri = "/products/2";
Product product = new Product();
[Link]("Lemon");
String inputJson = [Link](product);
MvcResult mvcResult =
[Link]([Link](uri)
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(inputJson)).andReturn();

int status = [Link]().getStatus();


assertEquals(200, status);
String content =
[Link]().getContentAsString();
assertEquals(content, "Product is updated
successsfully");
}
@Test
public void deleteProduct() throws Exception {
String uri = "/products/2";
MvcResult mvcResult =
[Link]([Link](uri)).andRetu
rn();
int status = [Link]().getStatus();
assertEquals(200, status);
String content =
[Link]().getContentAsString();
assertEquals(content, "Product is deleted
successsfully");
}
}

You can create an executable JAR file, and run the Spring Boot
application by using the Maven or Gradle commands given below

For Maven, you can use the command given below −

mvn clean install

Now, you can see the test results in console window.


For Gradle, you can use the command as shown below −

gradle clean build

You can see the rest results in console window as shown below.

Spring Boot - Database Handling


Spring Boot provides a very good support to create a DataSource
for Database. We need not write any extra code to create a
DataSource in Spring Boot. Just adding the dependencies and
doing the configuration details is enough to create a DataSource
and connect the Database.

In this chapter, we are going to use Spring Boot JDBC driver


connection to connect the database.

First, we need to add the Spring Boot Starter JDBC dependency in


our build configuration file.

Maven users can add the following dependencies in the [Link]


file.

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

Gradle users can add the following dependencies in the


[Link] file.
compile('[Link]:spring-boot-starter-jdbc')

Connect to H2 database
To connect the H2 database, we need to add the H2 database
dependency in our build configuration file.

For Maven users, add the below dependency in your [Link] file.

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>

For Gradle users, add the below dependency in your [Link]


file.

compile('com.h2database:h2')

We need to create the [Link] file and [Link] file under the
classpath src/main/resources directory to connect the H2
database.

The [Link] file is given below.

CREATE TABLE PRODUCT (ID INT PRIMARY KEY, PRODUCT_NAME


VARCHAR(25));

The [Link] file is given below.

INSERT INTO PRODUCT (ID,PRODUCT_NAME) VALUES (1,'Honey');


INSERT INTO PRODUCT (ID,PRODUCT_NAME) VALUES (2,'Almond');

Connect MySQL
To connect the MySQL database, we need to add the MySQL
dependency into our build configuration file.

For Maven users, add the following dependency in your [Link]


file.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

For Gradle users, add the following dependency in your


[Link] file.

compile('mysql:mysql-connector-java')

Now, create database and tables in MySQL as shown −

For properties file users, add the following properties in the


[Link] file.

[Link] =
[Link]
[Link] =
jdbc:mysql://localhost:3306/PRODUCTSERVICE?
autoreconnect = true
[Link] = root
[Link] = root
[Link] = true
[Link] = true
[Link] = 60000
[Link] = 30000
[Link] = SELECT 1
[Link]-active = 15
[Link]-idle = 10
[Link]-wait = 8000

For YAML users, add the following properties in the


[Link] file.

spring:
datasource:
driverClassName: [Link]
url: "jdbc:mysql://localhost:3306/PRODUCTSERVICE?
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

Connect Redis
Redis is an open source database used to store the in-memory
data structure. To connect the Redis database in Spring Boot
application, we need to add the Redis dependency in our build
configuration file.

Maven users should add the following dependency in your


[Link] file.

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>

Gradle users should add the following dependency in your


[Link] file.

compile('[Link]:spring-boot-starter-data-redis')

For Redis connection, we need to use RedisTemplate. For


RedisTemplate we need to provide the JedisConnectionFactory
details.

@Bean
JedisConnectionFactory jedisConnectionFactory() {
JedisConnectionFactory jedisConFactory = new
JedisConnectionFactory();
[Link]("localhost");
[Link](6000);
[Link](true);
return jedisConFactory;
}
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> template = new
RedisTemplate<>();

[Link](jedisConnectionFactory())
;
[Link](new
StringRedisSerializer());
[Link](new
StringRedisSerializer());
[Link](new
StringRedisSerializer());
[Link](new
StringRedisSerializer());
return template;
}

Now auto wire the RedisTemplate class and access the data from
Redis database.
@Autowired

RedisTemplate<String, Object> redis;


Map<Object,Object> datalist =
[Link]().entries(“Redis_code_index_key”);

JDBCTemplate
To access the Relational Database by using JdbcTemplate in
Spring Boot application, we need to add the Spring Boot Starter
JDBC dependency in our build configuration file.

Then, if you @Autowired the JdbcTemplate class, Spring Boot


automatically connects the Database and sets the Datasource for
the JdbcTemplate object.

@Autowired
JdbcTemplate jdbcTemplate;
Collection<Map<String, Object>> rows =
[Link]("SELECT QUERY");

The @Repository annotation should be added into the class file.


The @Repository annotation is used to create database repository
for your Spring Boot application.

@Repository
public class ProductServiceDAO {
}

Multiple DataSource
We can keep ‘n’ number Datasources in a single Spring Boot
application. The example given here shows how to create more
than 1 data source in Spring Boot application. Now, add the two
data source configuration details in the application properties file.

For properties file users, add the following properties into your
[Link] file.
[Link] =
[Link]
[Link] =
jdbc:mysql://localhost:3306/PRODUCTSERVICE?
autoreconnect = true
[Link] = root
[Link] = root
[Link] = true
[Link] = true
[Link] =
60000
[Link] =
30000
[Link] = SELECT 1
[Link]-active = 15
[Link]-idle = 10
[Link]-wait = 8000

[Link] =
[Link]
[Link] =
jdbc:mysql://localhost:3306/USERSERVICE?autoreconnect =
true
[Link] = root
[Link] = root
[Link] = true
[Link] = true
[Link] =
60000
[Link] = 30000
[Link] = SELECT 1
[Link]-active = 15
[Link]-idle = 10
[Link]-wait = 8000

Yaml users should add the following properties in your


[Link] file.

spring:
dbProductService:
driverClassName: [Link]
url: "jdbc:mysql://localhost:3306/PRODUCTSERVICE?
autoreconnect=true"
password: "root"
username: "root"
testOnBorrow: true
testWhileIdle: true
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 30000
validationQuery: SELECT 1
max-active: 15
max-idle: 10
max-wait: 8000
dbUserService:
driverClassName: [Link]
url: "jdbc:mysql://localhost:3306/USERSERVICE?
autoreconnect=true"
password: "root"
username: "root"
testOnBorrow: true
testWhileIdle: true
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 30000
validationQuery: SELECT 1
max-active: 15
max-idle: 10
max-wait: 8000

Now, create a Configuration class to create a DataSource and


JdbcTemplate for multiple data sources.

import [Link];

import
[Link];
import
[Link];
import
[Link]
uilder;
import
[Link]
onProperties;
import [Link];
import
[Link];
import [Link];
import [Link];

@Configuration
public class DatabaseConfig {
@Bean(name = "dbProductService")
@ConfigurationProperties(prefix =
"[Link]")
@Primary
public DataSource createProductServiceDataSource() {
return [Link]().build();
}
@Bean(name = "dbUserService")
@ConfigurationProperties(prefix =
"[Link]")
public DataSource createUserServiceDataSource() {
return [Link]().build();
}
@Bean(name = "jdbcProductService")
@Autowired
public JdbcTemplate
createJdbcTemplate_ProductService(@Qualifier("dbProduct
Service") DataSource productServiceDS) {
return new JdbcTemplate(productServiceDS);
}
@Bean(name = "jdbcUserService")
@Autowired
public JdbcTemplate
createJdbcTemplate_UserService(@Qualifier("dbUserServic
e") DataSource userServiceDS) {
return new JdbcTemplate(userServiceDS);
}
}

Then, auto wire the JDBCTemplate object by using @Qualifier


annotation.

@Qualifier("jdbcProductService")
@Autowired
JdbcTemplate jdbcTemplate;

@Qualifier("jdbcUserService")
@Autowired
JdbcTemplate jdbcTemplate;

Spring Boot - Securing Web


Applications
If a Spring Boot Security dependency is added on the classpath,
Spring Boot application automatically requires the Basic
Authentication for all HTTP Endpoints. The Endpoint “/” and
“/home” does not require any authentication. All other Endpoints
require authentication.

For adding a Spring Boot Security to your Spring Boot application,


we need to add the Spring Boot Starter Security dependency in
our build configuration file.

Maven users can add the following dependency in the [Link]


file.

<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-security</artifactId>
</dependency>

Gradle users can add the following dependency in the [Link]


file.

compile("[Link]:spring-boot-starter-security")

Securing a Web application


First, create an unsecure web application by using Thymeleaf
templates.

Then, create a [Link] file


under src/main/resources/templates directory.
<!DOCTYPE html>
<html xmlns = "[Link]
xmlns:th = "[Link]
xmlns:sec = "[Link]
extras-springsecurity3">

<head>
<title>Spring Security Example</title>
</head>
<body>
<h1>Welcome!</h1>
<p>Click <a th:href = "@{/hello}">here</a> to see
a greeting.</p>
</body>

</html>

The simple view /hello defined in the HTML file by using


Thymeleaf templates.

Now, create a [Link]


under src/main/resources/templates directory.

<!DOCTYPE html>
<html xmlns = "[Link]
xmlns:th = "[Link]
xmlns:sec = "[Link]
extras-springsecurity3">

<head>
<title>Hello World!</title>
</head>
<body>
<h1>Hello world!</h1>
</body>

</html>

Now, we need to setup the Spring MVC – View controller for home
and hello views.

For this, create a MVC configuration file that extends


WebMvcConfigurerAdapter.
package [Link];

import
[Link];
import
[Link]
ontrollerRegistry;
import
[Link]
cConfigurerAdapter;

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter
{
@Override
public void
addViewControllers(ViewControllerRegistry registry) {

[Link]("/home").setViewName("home")
;

[Link]("/").setViewName("home");

[Link]("/hello").setViewName("hello
");

[Link]("/login").setViewName("login
");
}
}

Now, add the Spring Boot Starter security dependency to your


build configuration file.

Maven users can add the following dependency in your [Link]


file.

<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-security</artifactId>
</dependency>
Gradle users can add the following dependency in the [Link]
file.

compile("[Link]:spring-boot-starter-security")

Now, create a Web Security Configuration file, that is used to


secure your application to access the HTTP Endpoints by using
basic authentication.

package [Link];

import
[Link];
import
[Link];
import
[Link]
[Link];
import
[Link]
[Link];
import
[Link]
[Link];
import
[Link]
[Link];

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends
WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws
Exception {
http
.authorizeRequests()
.antMatchers("/", "/home").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
@Autowired
public void
configureGlobal(AuthenticationManagerBuilder auth)
throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("password").roles("
USER");
}
}

Now, create a [Link] file under


the src/main/resources directory to allow the user to access the
HTTP Endpoint via login screen.

<!DOCTYPE html>
<html xmlns = "[Link] xmlns:th =
"[Link]
xmlns:sec = "[Link]
extras-springsecurity3">

<head>
<title>Spring Security Example </title>
</head>
<body>
<div th:if = "${[Link]}">
Invalid username and password.
</div>
<div th:if = "${[Link]}">
You have been logged out.
</div>

<form th:action = "@{/login}" method = "post">


<div>
<label> User Name : <input type = "text"
name = "username"/> </label>
</div>
<div>
<label> Password: <input type = "password"
name = "password"/> </label>
</div>
<div>
<input type = "submit" value = "Sign In"/>
</div>
</form>

</body>
</html>

Finally, update the [Link] file – to allow the user to Sign-out


from the application and display the current username as shown
below −

<!DOCTYPE html>
<html xmlns = "[Link] xmlns:th =
"[Link]
xmlns:sec = "[Link]
extras-springsecurity3">

<head>
<title>Hello World!</title>
</head>
<body>
<h1 th:inline = "text">Hello [[$
{#[Link]}]]!</h1>
<form th:action = "@{/logout}" method = "post">
<input type = "submit" value = "Sign Out"/>
</form>
</body>

</html>

The code for main Spring Boot application is given below −

package [Link];

import [Link];
import
[Link]
ation;

@SpringBootApplication
public class WebsecurityDemoApplication {
public static void main(String[] args) {

[Link]([Link],
args);
}
}

The complete code for build configuration file is given below.

Maven – [Link]

<?xml version = "1.0" encoding = "UTF-8"?>


<project xmlns = "[Link]
xmlns:xsi = "[Link]
instance"
xsi:schemaLocation =
"[Link]
[Link]

<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>websecurity-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>websecurity-demo</name>
<description>Demo project for Spring
Boot</description>

<parent>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-parent</artifactId>
<version>[Link]</version>
<relativePath/> <!-- lookup parent from
repository -->
</parent>
<properties>

<[Link]>UTF-8</[Link]
ceEncoding>

<[Link]>UTF-8</[Link]
[Link]>
<[Link]>1.8</[Link]>
</properties>

<dependencies>
<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-security</artifactId>
</dependency>

<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

<dependency>
<groupId>[Link]</groupId>

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

<dependency>
<groupId>[Link]</groupId>

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

<dependency>

<groupId>[Link]</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

Gradle – [Link]

buildscript {
ext {
springBootVersion = '[Link]'
}
repositories {
mavenCentral()
}
dependencies {
classpath("[Link]:spring-boot-
gradle-plugin:${springBootVersion}")
}
}

apply plugin: 'java'


apply plugin: 'eclipse'
apply plugin: '[Link]'

group = '[Link]'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
mavenCentral()
}
dependencies {
compile('[Link]:spring-boot-
starter-security')
compile('[Link]:spring-boot-
starter-thymeleaf')
compile('[Link]:spring-boot-
starter-web')

testCompile('[Link]:spring-boot-
starter-test')
testCompile('[Link]:spring-
security-test')
}

Now, create an executable JAR file, and run the Spring Boot
application by using the following Maven or Gradle commands.

Maven users can use the command as given below −

mvn clean install

After “BUILD SUCCESS”, you can find the JAR file under target
directory.

Gradle users can use the command as shown −

gradle clean build

After “BUILD SUCCESSFUL”, you can find the JAR file under the
build/libs directory.

Now, run the JAR file by using the command shown below −

java –jar <JARFILE>

Hit the URL [Link] in your web browser. You can


see the output as shown.
Spring Boot - OAuth2 with JWT
In this chapter, you will learn in detail about Spring Boot Security
mechanisms and OAuth2 with JWT.

Authorization Server
Authorization Server is a supreme architectural component for
Web API Security. The Authorization Server acts a centralization
authorization point that allows your apps and HTTP endpoints to
identify the features of your application.

Resource Server
Resource Server is an application that provides the access token
to the clients to access the Resource Server HTTP Endpoints. It is
collection of libraries which contains the HTTP Endpoints, static
resources, and Dynamic web pages.

OAuth2
OAuth2 is an authorization framework that enables the
application Web Security to access the resources from the client.
To build an OAuth2 application, we need to focus on the Grant
Type (Authorization code), Client ID and Client secret.

JWT Token
JWT Token is a JSON Web Token, used to represent the claims
secured between two parties. You can learn more about the JWT
token at [Link]/.

Now, we are going to build an OAuth2 application that enables


the use of Authorization Server, Resource Server with the help of
a JWT Token.

You can use the following steps to implement the Spring Boot
Security with JWT token by accessing the database.

First, we need to add the following dependencies in our build


configuration file.

Maven users can add the following dependencies in your [Link]


file.

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-security</artifactId>
</dependency>

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>

<groupId>[Link]</groupId>
<artifactId>spring-security-oauth2</artifactId>
</dependency>

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-security-jwt</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>

Gradle users can add the following dependencies in the


[Link] file.

compile('[Link]:spring-boot-starter-
security')
compile('[Link]:spring-boot-starter-
web')
testCompile('[Link]:spring-boot-
starter-test')
testCompile('[Link]:spring-
security-test')

compile("[Link]:spring-
security-oauth2")
compile('[Link]:spring-security-
jwt')
compile("[Link]:spring-boot-starter-
jdbc")
compile("com.h2database:h2:1.4.191")

where,

 Spring Boot Starter Security − Implements the Spring


Security
 Spring Security OAuth2 − Implements the OAUTH2
structure to enable the Authorization Server and Resource
Server.
 Spring Security JWT − Generates the JWT Token for Web
security
 Spring Boot Starter JDBC − Accesses the database to
ensure the user is available or not.
 Spring Boot Starter Web − Writes HTTP endpoints.
 H2 Database − Stores the user information for
authentication and authorization.

The complete build configuration file is given below.

<?xml version = "1.0" encoding = "UTF-8"?>


<project xmlns = "[Link]
xmlns:xsi = "[Link]
instance"
xsi:schemaLocation =
"[Link]
[Link]

<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>websecurityapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>websecurityapp</name>
<description>Demo project for Spring
Boot</description>

<parent>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-parent</artifactId>
<version>[Link]</version>
<relativePath /> <!-- lookup parent from
repository -->
</parent>

<properties>

<[Link]>UTF-8</[Link]
ceEncoding>
<[Link]>UTF-8</[Link]
[Link]>
<[Link]>1.8</[Link]>
</properties>

<dependencies>
<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

<dependency>
<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-security</artifactId>
</dependency>

<dependency>
<groupId>[Link]</groupId>

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

<dependency>

<groupId>[Link]</groupId>

<artifactId>spring-security-oauth2</artifactId>
</dependency>

<dependency>

<groupId>[Link]</groupId>
<artifactId>spring-security-jwt</artifactId>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>

<groupId>[Link]</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

Gradle – [Link]

buildscript {
ext {
springBootVersion = '[Link]'
}
repositories {
mavenCentral()
}
dependencies {
classpath("[Link]:spring-boot-
gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: '[Link]'
group = '[Link]'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
mavenCentral()
}

dependencies {
compile('[Link]:spring-boot-
starter-security')
compile('[Link]:spring-boot-
starter-web')
testCompile('[Link]:spring-boot-
starter-test')
testCompile('[Link]:spring-
security-test')
compile("[Link]:spring-
security-oauth2")
compile('[Link]:spring-
security-jwt')
compile("[Link]:spring-boot-
starter-jdbc")
compile("com.h2database:h2:1.4.191")
}

Now, in the main Spring Boot application, add the


@EnableAuthorizationServer and @EnableResourceServer
annotation to act as an Auth server and Resource Server in the
same application.

Also, you can use the following code to write a simple HTTP
endpoint to access the API with Spring Security by using JWT
Token.

package [Link];

import [Link];
import
[Link]
ation;
import
[Link].w
[Link];
import
[Link].w
[Link];
import
[Link];
import
[Link];

@SpringBootApplication
@EnableAuthorizationServer
@EnableResourceServer
@RestController
public class WebsecurityappApplication {
public static void main(String[] args) {

[Link]([Link],
args);
}
@RequestMapping(value = "/products")
public String getProductName() {
return "Honey";
}
}

Use the following code to define the POJO class to store the User
information for authentication.

package [Link];

import [Link];
import [Link];
import
[Link];

public class UserEntity {


private String username;
private String password;
private Collection<GrantedAuthority>
grantedAuthoritiesList = new ArrayList<>();
public String getPassword() {
return password;
}
public void setPassword(String password) {
[Link] = password;
}
public Collection<GrantedAuthority>
getGrantedAuthoritiesList() {
return grantedAuthoritiesList;
}
public void
setGrantedAuthoritiesList(Collection<GrantedAuthority>
grantedAuthoritiesList) {
[Link] =
grantedAuthoritiesList;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
[Link] = username;
}
}

Now, use the following code and define the CustomUser class that
extends the [Link]
class for Spring Boot authentication.

package [Link];

import
[Link];

public class CustomUser extends User {


private static final long serialVersionUID = 1L;
public CustomUser(UserEntity user) {
super([Link](), [Link](),
[Link]());
}
}
You can create the @Repository class to read the User
information from the database and send it to the Custom user
service and also add the granted authority
“ROLE_SYSTEMADMIN”.

package [Link];

import [Link];
import [Link];
import [Link];
import [Link];

import
[Link];
import [Link];
import
[Link];
import
[Link]
edAuthority;
import [Link];

@Repository
public class OAuthDao {
@Autowired
private JdbcTemplate jdbcTemplate;

public UserEntity getUserDetails(String username) {


Collection<GrantedAuthority>
grantedAuthoritiesList = new ArrayList<>();
String userSQLQuery = "SELECT * FROM USERS WHERE
USERNAME=?";
List<UserEntity> list =
[Link](userSQLQuery, new String[]
{ username },
(ResultSet rs, int rowNum) -> {

UserEntity user = new UserEntity();


[Link](username);
[Link]([Link]("PASSWORD"));
return user;
});
if ([Link]() > 0) {
GrantedAuthority grantedAuthority = new
SimpleGrantedAuthority("ROLE_SYSTEMADMIN");
[Link](grantedAuthority);

[Link](0).setGrantedAuthoritiesList(grantedAuthoritie
sList);
return [Link](0);
}
return null;
}
}

You can create a Custom User detail service class that extends
the
[Link]
to call the DAO repository class as shown.

package [Link];

import
[Link];
import
[Link]
lsService;
import
[Link]
otFoundException;
import [Link];

@Service
public class CustomDetailsService implements
UserDetailsService {
@Autowired
OAuthDao oauthDao;

@Override
public CustomUser loadUserByUsername(final String
username) throws UsernameNotFoundException {
UserEntity userEntity = null;
try {
userEntity =
[Link](username);
CustomUser customUser = new
CustomUser(userEntity);
return customUser;
} catch (Exception e) {
[Link]();
throw new UsernameNotFoundException("User " +
username + " was not found in the database");
}
}
}

Next, create a @configuration class to enable the Web Security,


defining the Password encoder (BCryptPasswordEncoder), and
defining the AuthenticationManager bean. The Security
configuration class should extend WebSecurityConfigurerAdapter
class.

package [Link];

import
[Link];
import [Link];
import
[Link];

import
[Link]
ionManager;
import
[Link]
[Link];
import
[Link].c
[Link];
import
[Link]
[Link];
import
[Link]
[Link];
import
[Link]
[Link];
import
[Link]
[Link];
import
[Link]
nPolicy;
import
[Link]
rdEncoder;
import
[Link]
coder;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration extends
WebSecurityConfigurerAdapter {
@Autowired
private CustomDetailsService customDetailsService;

@Bean
public PasswordEncoder encoder() {
return new BCryptPasswordEncoder();
}
@Override
@Autowired
protected void
configure(AuthenticationManagerBuilder auth) throws
Exception {

[Link](customDetailsService).passwordE
ncoder(encoder());
}
@Override
protected void configure(HttpSecurity http) throws
Exception {

[Link]().anyRequest().authenticated().a
nd().sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.N
EVER);
}
@Override
public void configure(WebSecurity web) throws
Exception {
[Link]();
}
@Override
@Bean
public AuthenticationManager
authenticationManagerBean() throws Exception {
return [Link]();
}
}

Now, define the OAuth2 Configuration class to add the Client ID,
Client Secret, Define the JwtAccessTokenConverter, Private key
and Public key for token signer key and verifier key, and configure
the ClientDetailsServiceConfigurer for the Token validity with
scopes.

package [Link];

import
[Link];
import
[Link];
import [Link];
import
[Link];

import
[Link]
ionManager;
import
[Link].c
[Link];
import
[Link].w
[Link];
import
[Link].w
[Link];
import
[Link].w
[Link];
import
[Link]
[Link];
import
[Link]
[Link];

@Configuration
public class OAuth2Config extends
AuthorizationServerConfigurerAdapter {
private String clientid = "tutorialspoint";
private String clientSecret = "my-secret-key";
private String privateKey = "private key";
private String publicKey = "public key";

@Autowired
@Qualifier("authenticationManagerBean")
private AuthenticationManager authenticationManager;

@Bean
public JwtAccessTokenConverter tokenEnhancer() {
JwtAccessTokenConverter converter = new
JwtAccessTokenConverter();
[Link](privateKey);
[Link](publicKey);
return converter;
}
@Bean
public JwtTokenStore tokenStore() {
return new JwtTokenStore(tokenEnhancer());
}
@Override
public void
configure(AuthorizationServerEndpointsConfigurer
endpoints) throws Exception {

[Link](authenticationManager).
tokenStore(tokenStore())
.accessTokenConverter(tokenEnhancer());
}
@Override
public void
configure(AuthorizationServerSecurityConfigurer
security) throws Exception {

[Link]("permitAll()").checkTokenAccess
("isAuthenticated()");
}
@Override
public void configure(ClientDetailsServiceConfigurer
clients) throws Exception {

[Link]().withClient(clientid).secret(clientSe
cret).scopes("read", "write")
.authorizedGrantTypes("password",
"refresh_token").accessTokenValiditySeconds(20000)
.refreshTokenValiditySeconds(20000);

}
}

Now, create a Private key and public key by using openssl.

You can use the following commands for generating private key.

openssl genrsa -out [Link] 2048


openssl rsa -in [Link]

You can use For public key generation use the below commands.

openssl rsa -in [Link] -pubout

For the version of Spring Boot latter than 1.5 release, add the
below property in your [Link] file to define OAuth2
Resource filter order.

[Link]-order=3

YAML file users can add the below property in YAML file.

security:
oauth2:
resource:
filter-order: 3

Now, create [Link] and [Link] file under the classpath


resources src/main/resources/directory to connect the
application to H2 database.

The [Link] file is as shown −

CREATE TABLE USERS (ID INT PRIMARY KEY, USERNAME VARCHAR(45),


PASSWORD VARCHAR(60));

The [Link] file is as shown −

INSERT INTO USERS (ID, USERNAME,PASSWORD) VALUES (


1,
'tutorialspoint@[Link]','$2a$08$[Link].9rYO8t0q5wk
2ROJ.1cdR53bmaVG');

INSERT INTO USERS (ID, USERNAME,PASSWORD) VALUES (


2,
'myemail@[Link]','$2a$08$[Link].9rYO8t0q5wk2ROJ
.1cdR53bmaVG');

Note − Password should be stored in the format of Bcrypt


Encoder in the database table.

You can create an executable JAR file, and run the Spring Boot
application by using the following Maven or Gradle commands.

For Maven, you can use the command given below −

mvn clean install

After “BUILD SUCCESS”, you can find the JAR file under the target
directory.

For Gradle, you can use the command as shown −

gradle clean build


After “BUILD SUCCESSFUL”, you can find the JAR file under the
build/libs directory.

Now, run the JAR file by using the command shown here −

java –jar <JARFILE>

The application is started on the Tomcat port 8080.

Now hit the POST method URL via POSTMAN to get the OAUTH2
token.

[Link]

Now, add the Request Headers as follows −

 Authorization − Basic Auth with your Client Id and Client


secret.
 Content Type − application/x-www-form-urlencoded

Now, add the Request Parameters as follows −

 grant_type = password
 username = your username
 password = your password

Now, hit the API and get the access_token as shown −


Now, Hit the Resource Server API with Bearer access token in
Request Header as shown.

Then you can see the output as shown below −


Spring Boot - Google Cloud
Platform
Google Cloud Platform provides a cloud computing services that
run the Spring Boot application in the cloud environment. In this
chapter, we are going to see how to deploy the Spring Boot
application in GCP app engine platform.

First, download the Gradle build Spring Boot application from


Spring Initializer page [Link]. Observe the following
screenshot.

Now, in [Link] file, add the Google Cloud appengine plugin


and appengine classpath dependency.

The code for [Link] file is given below −

buildscript {
ext {
springBootVersion = '[Link]'
}
repositories {
mavenCentral()
}
dependencies {
classpath("[Link]:spring-boot-
gradle-plugin:${springBootVersion}")
classpath '[Link]:appengine-
gradle-plugin:1.3.3'
}
}

apply plugin: 'java'


apply plugin: 'eclipse'
apply plugin: '[Link]'
apply plugin: '[Link]'

group = '[Link]'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
mavenCentral()
}
dependencies {
compile('[Link]:spring-boot-
starter-web')
testCompile('[Link]:spring-boot-
starter-test')
}

Now, write a simple HTTP Endpoint and it returns the String


success as shown −

package [Link];

import [Link];
import
[Link]
ation;
import
[Link];
import
[Link];

@SpringBootApplication
@RestController
public class AppengineDemoApplication {
public static void main(String[] args) {
[Link]([Link],
args);
}
@RequestMapping(value = "/")
public String success() {
return "APP Engine deployment success";
}
}

Next, add the [Link] file under src/main/appengine directory as


shown −

runtime: java
env: flex

handlers:
- url: /.*
script: this field is required, but ignored

Now, go to the Google Cloud console and click the Activate


Google cloud shell at the top of the page.

Now, move your source files and Gradle file into home directory of
your google cloud machine by using google cloud shell.

Now, execute the command gradle appengineDeploy and it will


deploy your application into the Google Cloud appengine.

Note − GCP should be billing enabled and before deploying your


application into appengine, you should create appengine platform
in GCP.

It will take few minutes to deploy your application into GCP


appengine platform.
After build successful you can see the Service URL in console
window.

Now, hit the service URL and see the output.

Google Cloud SQL


To connect the Google Cloud SQL into your Spring Boot
application, you should add the following properties into your
[Link] file.

JDBC URL Format

jdbc:mysql://google/<DATABASE-NAME>?cloudSqlInstance =
<GOOGLE_CLOUD_SQL_INSTANCE_NAME> &socketFactory =
[Link]&user = <USERNAME>&password
= <PASSWORD>

Note − The Spring Boot application and Google Cloud SQL should
be in same GCP project.

The [Link] file is given below.

[Link] =
[Link]
[Link] =
jdbc:mysql://google/PRODUCTSERVICE?cloudSqlInstance =
springboot-gcp-cloudsql:asia-northeast1:springboot-gcp-
cloudsql-instance&socketFactory =
[Link]&user =
root&password = [Link] =
root

[Link] = root
[Link] = true
[Link] = true
[Link] =
60000
[Link] =
30000
[Link] = SELECT 1
[Link]-active = 15
[Link]-idle = 10
[Link]-wait = 8000

YAML file users can add the below properties to your


[Link] file.

spring:
datasource:
driverClassName: [Link]
url: "jdbc:mysql://google/PRODUCTSERVICE?
cloudSqlInstance=springboot-gcp-cloudsql:asia-
northeast1:springboot-gcp-cloudsql-
instance&socketFactory=[Link]
tFactory&user=root&password=root"
password: "root"
username: "root"
testOnBorrow: true
testWhileIdle: true
validationQuery: SELECT 1

max-active: 15
max-idle: 10
max-wait: 8000

Spring Boot - Google OAuth2


Sign-In
In this chapter, we are going to see how to add the Google
OAuth2 Sign-In by using Spring Boot application with Gradle build.

First, add the Spring Boot OAuth2 security dependency in your


build configuration file and your build configuration file is given
below.

buildscript {
ext {
springBootVersion = '[Link]'
}
repositories {
mavenCentral()
}
dependencies {
classpath("[Link]:spring-boot-
gradle-plugin:${springBootVersion}")
}
}

apply plugin: 'java'


apply plugin: 'eclipse'
apply plugin: '[Link]'

group = '[Link]'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
mavenCentral()
}
dependencies {
compile('[Link]:spring-boot-
starter')
testCompile('[Link]:spring-boot-
starter-test')
compile('[Link]:spring-
security-oauth2')
compile('[Link]:spring-boot-
starter-web')
testCompile('[Link]:spring-boot-
starter-test')
}
Now, add the HTTP Endpoint to read the User Principal from the
Google after authenticating via Spring Boot in main Spring Boot
application class file as given below −

package [Link];

import [Link];

import [Link];
import
[Link]
ation;
import
[Link];
import
[Link];

@SpringBootApplication
@RestController
public class GoogleserviceApplication {
public static void main(String[] args) {

[Link]([Link],
args);
}
@RequestMapping(value = "/user")
public Principal user(Principal principal) {
return principal;
}
}

Now, write a Configuration file to enable the OAuth2SSO for web


security and remove the authentication for [Link] file as
shown −

package [Link];

import
[Link].oauth2.
client.EnableOAuth2Sso;
import
[Link];
import
[Link]
[Link];
import
[Link]
[Link];

@Configuration
@EnableOAuth2Sso
public class WebSecurityConfiguration extends
WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws
Exception {
http
.csrf()
.disable()
.antMatcher("/**")
.authorizeRequests()
.antMatchers("/", "/[Link]")
.permitAll()
.anyRequest()
.authenticated();
}
}

Next, add the [Link] file under static resources and add the
link to redirect into user HTTP Endpoint to read the Google user
Principal as shown below −

<!DOCTYPE html>
<html>
<head>
<meta charset = "ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href = "user">Click here to Google Login</a>
</body>
</html>
Note − In Google Cloud console - Enable the Gmail Services,
Analytics Services and Google+ service API(s).

Then, go the Credentials section and create a credentials and


choose OAuth Client ID.

Next, provide a Product Name in OAuth2 consent screen.


Next, choose the Application Type as “Web application”, provide
the Authorized JavaScript origins and Authorized redirect URIs.
Now, your OAuth2 Client Id and Client Secret is created.
Next, add the Client Id and Client Secret in your application
properties file.

[Link] = <CLIENT_ID>
[Link] = <CLIENT_SECRET>
[Link] =
[Link]
[Link] =
[Link]
[Link] = oauth_token
[Link] = query
[Link] =
form
[Link] = profile email

[Link] =
[Link]
[Link] = false

Now, you can create an executable JAR file, and run the Spring
Boot application by using the following Gradle command.

For Gradle, you can use the command as shown −

gradle clean build

After “BUILD SUCCESSFUL”, you can find the JAR file under the
build/libs directory.

Run the JAR file by using the command java –jar <JARFILE> and
application is started on the Tomcat port 8080.

Now hit the URL [Link] and click the Google


Login link.

It will redirect to the Google login screen and provide a Gmail


login details.
If login success, we will receive the Principal object of the Gmail
user.

You might also like