SPRING BOOT
BCS 403 by Surendra Kumar, AKGEC
Spring Boot is a Spring module that provides the RAD
(Rapid Application Development) feature to the Spring
framework. All topics of Spring Boot such, as features,
project, maven project, starter project wizard, Spring
Initializr, CLI, applications, annotations, dependency
management, properties, starters, Actuator, JPA, JDBC, etc.
What is Spring Boot ?
Spring Boot is a project that is built on the top of the Spring
Framework. It provides an easier and faster way to set up,
configure, and run both simple and web-based applications.
BCS 403 by Surendra Kumar, AKGEC
It is a Spring module that provides the RAD (Rapid
Application Development) feature to the Spring
Framework. It is used to create a stand-alone Spring-based
application that you can just run because it needs minimal
Spring configuration.
STS IDE or Spring Initializr to
develop Spring Boot Java
applications.
In short, Spring Boot is the
combination of Spring
Framework and Embedded
Servers.
BCS 403 by Surendra Kumar, AKGEC
Why should we use Spring Boot Framework?
The dependency injection approach is used in Spring
Boot.
It contains powerful database transaction management
capabilities.
It simplifies integration with other Java frameworks like
JPA/Hibernate ORM, Struts, etc.
It reduces the cost and development time of the
application.
BCS 403 by Surendra Kumar, AKGEC
Along with the Spring Boot Framework, many other Spring sister
projects help to build applications addressing modern business
needs. There are the following Spring sister projects are as
follows:
Spring Data: It simplifies data access from the relational
and NoSQL databases.
Spring Batch: It provides powerful batch processing.
Spring Security: It is a security framework that provides
robust security to applications.
Spring Social: It supports integration with social
networking like LinkedIn.
Spring Integration: It is an implementation of Enterprise
Integration Patterns. It facilitates integration with
other enterprise applications using lightweight messaging and
BCS 403 by Surendra Kumar, AKGEC
declarative adapters.
Advantages of Spring Boot
• It creates stand-alone Spring applications that can be started using
Java -jar.
• It tests web applications easily with the help of
different Embedded HTTP servers such as Tomcat, Jetty, etc. We
don't need to deploy WAR files.
• It provides opinionated 'starter' POMs to simplify our Maven
configuration.
• It provides production-ready features such as metrics, health
checks, and externalized configuration.
• There is no requirement for XML configuration.
• It offers a CLI tool for developing and testing the Spring Boot
application.
• It offers the number of plug-ins.
• It also minimizes writing multiple boilerplate codes (the code that
has to be included in many places with little or no alteration), XML
configuration, and annotations.
• It increases productivity and reduces development time.
BCS 403 by Surendra Kumar, AKGEC
Limitations of Spring Boot
Spring Boot can use dependencies that are not going to be
used in the application.
These dependencies increase the size of the application.
Prerequisite of Spring Boot
To create a Spring Boot application, following are the
prerequisites. Here, we will use Spring Tool Suite (STS) IDE.
• Java 1.8
• Maven 3.0+
• Spring Framework 5.0.0.BUILD-SNAPSHOT
• An IDE (Spring Tool Suite) is recommended.
BCS 403 by Surendra Kumar, AKGEC
Goals of Spring Boot
The main goal of Spring Boot is to reduce development, unit
test, and integration test time.
• Provides Opinionated Development approach
• Avoids defining more Annotation Configuration
• Avoids writing lots of import statements
• Avoids XML Configuration.
By providing or avoiding the above points, Spring Boot Framework
reduces Development time, Developer Effort, and increases
productivity.
BCS 403 by Surendra Kumar, AKGEC
Spring Boot Features
• Web Development
• SpringApplication
• Application events and listeners
• Admin features
• Externalized Configuration
• Properties Files
• YAML Support
• Type-safe Configuration
• Logging
• Security
BCS 403 by Surendra Kumar, AKGEC
SpringApplication
The SpringApplication is a class that provides a convenient
way to bootstrap a Spring application. It can be started from
the main method. We can call the application just by calling a
static run() method.
public static void main(String[] args)
{
SpringApplication.run(ClassName.class, args);
}
BCS 403 by Surendra Kumar, AKGEC
Reactive Spring
Servlet stack
Servlet Container
Servlet API
Spring MVC
Reactive Stack
Netty, Servlet 3.1, Undertow
Reactive HTTP Layer
Spring WebFlux
Functional API
Kotlin Support
BCS 403 by Surendra Kumar, AKGEC
Spring vs. Spring Boot vs. Spring MVC
Spring vs. Spring Boot
Spring: Spring Framework is the most popular application
development framework of Java. The main feature of the
Spring Framework is dependency Injection or Inversion
of Control (IoC). With the help of Spring Framework, we can
develop a loosely coupled application. It is better to use if
application type or characteristics are purely defined.
Spring Boot: Spring Boot is a module of Spring Framework.
It allows us to build a stand-alone application with minimal
or zero configurations. It is better to use if we want to
develop a simple Spring-based application or RESTful
services.
BCS 403 by Surendra Kumar, AKGEC
Spring Spring Boot
Spring Framework is a widely used Java EE Spring Boot Framework is widely used to
framework for building applications. develop REST APIs.
It aims to simplify Java EE development that It aims to shorten the code length and provide the
makes developers more productive. easiest way to develop Web Applications.
The primary feature of the Spring Framework The primary feature of Spring Boot
is dependency injection. is Autoconfiguration. It automatically configures
the classes based on the requirement.
It helps to make things simpler by allowing us to It helps to create a stand-alone application with
develop loosely coupled applications. less configuration.
The developer writes a lot of code (boilerplate It reduces boilerplate code.
code) to do the minimal task.
To test the Spring project, we need to set up the Spring Boot offers embedded server such
sever explicitly. as Jetty and Tomcat, etc.
It does not provide support for an in-memory It offers several plugins for working with an
database. embedded and in-memory database such as H2.
Developers manually define dependencies for the Spring Boot comes with the concept of starter in
Spring project in pom.xml. pom.xml file that internally takes care of
downloading the dependencies JARs based on
Spring Boot Requirement.
BCS 403 by Surendra Kumar, AKGEC
Spring Boot is a module of Spring for Spring MVC is a model view controller-
packaging the Spring-based application based web framework under the Spring
with sensible defaults. framework.
It provides default configurations to It provides ready to use features for
build Spring-powered framework. building a web application.
There is no need to build configuration It requires build configuration manually.
manually.
There is no requirement for a A Deployment descriptor is required.
deployment descriptor.
It avoids boilerplate code and wraps It specifies each dependency
dependencies together in a single unit. separately.
It reduces development time and It takes more time to achieve the same.
increases productivity.
BCS 403 by Surendra Kumar, AKGEC
Before understanding the Spring Boot
Architecture, we must know the different layers
and classes present in it. There are four layers in
Spring Boot are as follows:
Presentation Layer
Business Layer
Persistence Layer
Database Layer
BCS 403 by Surendra Kumar, AKGEC
BCS 403 by Surendra Kumar, AKGEC
Presentation Layer: The presentation layer
handles the HTTP requests, translates the JSON
parameter to object, and authenticates the request
and transfer it to the business layer. In short, it
consists of views i.e., frontend part.
Business Layer: The business layer handles all
the business logic. It consists of service classes
and uses services provided by data access layers.
It also performs authorization and validation.
Persistence Layer: The persistence layer contains
all the storage logic and translates business
objects from and to database rows.
Database Layer: In the database
layer, CRUD (create, retrieve, update, delete)
operations areBCSperformed.
403 by Surendra Kumar, AKGEC
Spring Boot Flow Architecture
BCS 403 by Surendra Kumar, AKGEC
Generating a Project
Before creating a project, we must be friendly with UI. Spring
Initializr UI has the following labels:
Project: It defines the kind of project. We can create
either Maven Project or Gradle Project. We will create
a Maven Project throughout the tutorial.
Language: Spring Initializr provides the choice among three
languages Java, Kotlin, and Groovy. Java is by default
selected.
Spring Boot: We can select the Spring Boot version. The
latest version is 2.2.2.
Project Metadata: It contains information related to the
project, such as Group, Artifact, etc. Group denotes
the package name; Artifact denotes the Application name.
The default Group name is com.example, and the default
Artifact name is demo.
Dependencies: Dependencies are the collection of artifacts
that we can add BCSto 403
our project.
by Surendra Kumar, AKGEC
There is another Options section that contains the following
fields:
Name: It is the same as Artifact.
Description: In the description field, we can write
a description of the project.
Package Name: It is also similar to the Group name.
Packaging: We can select the packing of the project. We
can choose either Jar or War.
Java: We can select the JVM version which we want to use.
We will use Java 8 version throughout the tutorial.
There is a Generate button. When we click on the button, it
starts packing the project and downloads the Jar or War file,
which you have selected.
BCS 403 by Surendra Kumar, AKGEC
How to Run Spring Boot Application
In this section, we will create and run a simple Spring Boot
application.
Creating a Spring Boot Application
Step 1: Open the Spring Initializr https://start.spring.io/.
Step 2: Select the Spring Boot version 2.2.2.BUILD-SNAPSHOT.
Step 3: Provide the Group name. We have provided the Group
name com.javatpoint.
Step 4: Provide the Artifact. We have provided the Artifact spring-
boot-application-run.
Step 5: Add the Spring Web dependency.
Step 6: Click on the Generate button. When we click on the
Generate button,
it wraps all the specifications related to application into a Jar file and
downloads
it to the local system.
Step 7: Extract the jar file.
Step 8: Copy the folder and paste it in the STS workspace.
BCS 403 by Surendra Kumar, AKGEC
Step 9: Import the project.
File -> Import -> Existing Maven
Projects -> Next -> Browse -> Select
the folder spring- spring-boot-
application-run -> Select Folder ->
Finish
It takes time to import the project.
When the project imports
successfully, we can see it in
the Package Explorer section of the
IDE.
We see that two files are automatically
created, one is pom.xml, and the
other is Application.java file.
BCS 403 by Surendra Kumar, AKGEC
Step 10: Create a Controller. We have created a controller with the
name HelloWorldController.
HelloWorldController.java
package com.javatpoint;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController
{
@RequestMapping("/")
public String hello()
{
return "Hello User";
}
}
Now we have created all the required files related to Spring Boot application.
BCS 403 by Surendra Kumar, AKGEC
Run the Spring Boot application
For running the Spring Boot application, open the main application file, and run it
as Java Application.
When the application runs successfully, it shows the message in the console, as
shown below.
BCS 403 by Surendra Kumar, AKGEC
Now, open the browser and invoke the URL http://localhost:8080.
It shows the message that we have returned to the controller.
BCS 403 by Surendra Kumar, AKGEC
package com.javatpoint.springbootexample;
import org.springframework.boot.SpringApplicat
ion;
import org.springframework.boot.autoconfigure.
SpringBootApplication;
@SpringBootApplication
public class SpringBootExampleApplication
{
public static void main(String[] args)
{
SpringApplication.run(SpringBootExampleApplic
ation.class, args);
}
}
BCS 403 by Surendra Kumar, AKGEC
Introduction to RESTful Web Services
REST stands for REpresentational State Transfer. It is developed by Roy
Thomas Fielding, who also developed HTTP.
The main goal of RESTful web services is to make web services more effective.
RESTful web services try to define services using the different concepts that are
already present in HTTP. REST is an architectural approach, not a protocol.
It does not define the standard message exchange format. We can build REST
services with both XML and JSON. JSON is more popular format with REST.
The key abstraction is a resource in REST. A resource can be anything.
It can be accessed through a Uniform Resource Identifier (URI). For example:
The resource has representations like XML, HTML, and JSON.
The current state capture by representational resource.
When we request a resource, we provide the representation of the resource.
BCS 403 by Surendra Kumar, AKGEC
The important methods of HTTP are:
GET: It reads a resource.
PUT: It updates an existing resource.
POST: It creates a new resource.
DELETE: It deletes the resource.
For example, if we want to perform the following actions in the social media
application, we get the corresponding results.
POST /users: It creates a user.
GET /users/{id}: It retrieves the detail of a user.
GET /users: It retrieves the detail of all users.
DELETE /users: It deletes all users.
DELETE /users/{id}: It deletes a user.
GET /users/{id}/posts/post_id: It retrieve the detail of a specific post.
POST / users/{id}/ posts: It creates a post of the user.
BCS 403 by Surendra Kumar, AKGEC
Further, we will implement these URI in
our project.
HTTP also defines the following standard
status code:
404: RESOURCE NOT FOUND
200: SUCCESS
201: CREATED
401: UNAUTHORIZED
500: SERVER ERROR
RESTful Service Constraints
There must be a service producer and
service consumer.
The service is stateless.
The service result must be cacheable.
The interface is uniform and exposing
resources.
The service should assume a layered
architecture.
BCS 403 by Surendra Kumar, AKGEC
Advantages of RESTful web services
RESTful web services are platform-
independent.
It can be written in any programming
language and can be executed on any
platform.
It provides different data format
like JSON, text, HTML, and XML.
It is fast in comparison to SOAP because
there is no strict specification like SOAP.
These are reusable.
They are language neutral.
BCS 403 by Surendra Kumar, AKGEC
Initializing a RESTful Web Services Project with Spring Boot
Download the Spring Tool Suite
Step 1:
(STS) from https://spring.io/tools3/sts/all and extract it.
Step 2: Launch the STS.
Step 3: Click on File menu -> New -> Spring Starter Project ->
If the Spring Starter Project is not enlisted, then click on Other at the bottom of
the menu. A dialog box appears on the screen. Type Spring Starter Project in
the Wizards text box and click on the Next button.
BCS 403 by Surendra Kumar, AKGEC
Step 4: provide the name, group, and package of the project. We have
provided:
Name: restful-web-services
Group: com.javatpoint
Package: com.javatpoint.server.main
Click on the Next button.
BCS 403 by Surendra Kumar, AKGEC
BCS 403 by Surendra Kumar, AKGEC
Step 5: Choose the Spring Boot
Version 2.1.8.
BCS 403 by Surendra Kumar, AKGEC
Step 6: We can see the project structure in the project explorer window.
BCS 403 by Surendra Kumar, AKGEC
Step 7: Go to the Maven Repository https://mvnrepository.com/ and
add Spring Web MVC, Spring Boot DevTools, JPA, and H2 dependencies in
the pom.xml. After adding the dependencies, the pom.xml file looks like the
following:
pom.xml
BCS 403 by Surendra Kumar, AKGEC
BCS 403 by Surendra Kumar, AKGEC
BCS 403 by Surendra Kumar, AKGEC
Step 8: Now open
the RestfulWebServicesApplication.java file and Run the
file as Java Application.
BCS 403 by Surendra Kumar, AKGEC