Spring Boot
LearnCodeWith Durgesh
What is Spring Boot
Spring Boot is module of Spring from which we speed up the
development
Spring Boot makes it easy to create stand-alone,
production-grade Spring based Applications that you can
"just run"
LearnCodeWith Durgesh
What is Spring Boot?
It provides an easier and faster way to set up, configure,
and run both simple and web-based applications.
Spring Embedded Boot
[Configuration]
Framework Servers
LearnCodeWith Durgesh
What is Spring Boot?
Convention over configuration software design style.
It decreases the effort of the developer.
Opinionated Default- Automatically configure
LearnCodeWith Durgesh
What is Spring Boot?
Scan the class path and find the dependency it will
automatically configure the things.
LearnCodeWith Durgesh
Advantages of Spring Boot
• It creates stand-alone Spring applications that can be started using
Java -jar.
• Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
• Provide opinionated 'starter' dependencies to simplify your build configuration
• Automatically configure Spring and 3rd party libraries whenever possible
• Provide production-ready features such as metrics, health checks, and
externalized configuration
• Absolutely no code generation and no requirement for XML configuration
LearnCodeWith Durgesh
How spring boot magic works?
No magic
Programmatic Configurations
LearnCodeWith Durgesh
Starter POM
spring-boot-starter-web
spring-boot-starter-data-jpa
Other…..
LearnCodeWith Durgesh
When we add starter jars
then it pull all the jars
Jar contain
META-INF/spring.factories
What should be enabled or
disabled based on some condition
LearnCodeWith Durgesh
How to start with spring boot
1. Create a maven project and add starter
dependencies
2. Use spring initializr
3. Use IDE like STS
4. Spring boot command line interface
application.properties
Application Configuration
Our Customize
Preconfigured
Application
Application
application.yml
JPA (Java Persistent API)
ORM
Class User
{ Id Name City Profile
int id
String name
String city
String profile
}
JPA (Java Persistent API)
Implementation EntityManagerFactory
Hibernate EntityManager
Eclipse link Create
Open JPA Update
Read , Delete
Spring Boot Makes easier to
perform operation with JPA
CRUDRespository
User JPARespository
UserRespository
Custom Finder Methods
API(Application Programming Interface)
It is a set of rules that allow programs to talk to each
other. The developer creates the API on the server and
allows the client to talk to it.
Request
Our
Website API
Response YouTube Server
REST(Representational State Transfer)
A set of constraints to be used for
creating Web services.
Client-Server Cacheable
Stateless Layered
For example,
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.
HTTP also defines the following standard
status code:
•404: RESOURCE NOT FOUND
•200: SUCCESS
•201: CREATED
•401: UNAUTHORIZED
•500: SERVER ERROR
Spring Boot DevTools
DevTools stands for Developer Tool. The aim of the module is to try and
improve the development time while working with the Spring Boot
application. Spring Boot DevTools pick up the changes and restart the
application.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
Features of DevTools
1. Property Defaults
2. Automatic Restart
3. Live Reload
4. Remote Applications
IDE using For building REST API
VS Code
Setup is very important
Watch our VS code Spring
Boot Setup video
How to call API
Postman
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.
Rest API
HTTP method URI Description Valid HTTP status codes
POST /books Create a book 201
GET /books/{bookId} Read a book 200
PUT /books/{bookId} Update a book 200
DELETE /books/{bookId} Delete a book 204
GET /books Retrieve all books 200, 204, 206