Spring Boot Part-1
What is Spring Boot?
Spring boot makes it easy to create
stand-alone , production grade spring based application that you can "just
run".
..create...Spring based Applications...
What is Spring?
Application framework - built entreprise
java application.
Programming and configuration model - focus
on business logic and rest spring will take care. datalayer, service,
controller, mvc address using annotation.
Problems with Spring
- Huge framework - Address all the issue but its overlapping.
- Multiple setup steps - connect to the db. needs lot of configuration is need
- Multiple configuration steps
- Multiple build and deploy steps - cost is involved.
Can we abstract these steps?
You can focus on business logic that is 80%
and rest of the 20% configuration part. That we will configure depends on the
environment .
- Opinionated - Start with your basic configuration and later you can change as per your wish. If required.
- Convention over configuration - If it is default configuration. then there is no need to change configuration are required.
- Stand alone - Built the jar file and run it stand alone.
- Production ready - take the jar file and ready to deploy.
Java 8 SDK
Spring STS - Supported by Spring.
Sprint Tool Suite
Java dependent jars are put it the class path and
application use those files.
Create a simple project based on the
arch-type and the download the dependent jars which are defined in the pom.xml
from the repository
Creating
a spring Boot Project
Create a project - Multiple approch to
create Boot project.
Creating a maven Java Project
Steps
Open STS
Right click the Package Explorer. New
-> Maven Project.
Enable the Checkbox "Create a simple
project (skip archetype selection)" -> Next
Group Id: (Package name)
io.java.springbootquickstart
Artifact Id:(Project Name)course-api
Version: 0.0.1-SNAPSHOT
Packaging: jar
Name:Java Briains Course API
Click Finish
Convert Sample to Spring Boot Project in
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
Above set of tags, consider our project as
child project, which inherits the parent project. That is spring boot.
spring-boot-starter-parent project provide default configuration
that is 80%. If you wish to change 20% you can change as per your environment.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
Once you save this file. You will find the Maven dependencies in the
Package Explorer
Select the Project -> Maven -> Update
Project. If you find any error
Add
Java version.
<properties> <java.version>1.8</java.version></properties>
Starting
a Spring Boot application
Create a java class.
Go to src/main/java -> new -> class
package: io.javabrains.springbootstarter
Name: CoursseApiApp
Select the checkbox "public static
void main method"
Step 1: Inform it spring application. Add
the annotation in the class level
@SpringBootApplication
Step 2: Add the below line in the public
static method of CourseApiApp.
SpringApplication.run(classname
where you have main method, arguments to main method)
Run as "Java Application"
Starting
Spring Boot
SpringApplication.run(App.class, args)
1. Sets up
default configuration (address 80% configuration)
2. Start
Spring application context
3. performs
class path scan
4. Starts
Tomcat server
Starting
a Spring Boot App
1. Spring
Initializr
2. Spring
Boot CLI
3. STS IDE
Configuration
spring Boot
application.properties
Places your properties under
src/main/resources
create file -> application.properties
Configure the property in properties file
(if want to change the property)
server.port=3000
spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
http://localhost:3000
Common application properties which give your configuration to
change
Adding
a REST Controller
Let's add a controller
A Java Class
Marked with annotations
Has info about
What URL access triggers it
What method to run when accessed?
When we call url
localhost:8080/hello it return hi
Create a new controller class
Package:io.javabrains.springbootstarter.hello
Name:HelloController
Add @RestController - Becomes to rest
controller.
Add method with
@RequestMapping("/hello")
public String sayHi(){
return "Hi";
}Returning Objects From Controller
Create a TopicController
class
package io.javabrains.springbootStarter.topic
@RestController
public class TopicController{
@RequestMapping("/topics")
public
String getAllTopics(){
return
"all topics"
}
}
Return the response in JSON
Create a Topic class (model). create member
with getter and setters, no arg constructor and constructor with parameters
Change the Return type of TopicController
When you use @RestController then return
response of the list as JSON
What's happening here in Spring boot
project (pom.xml)?
(If you change the version of the parent
tag. which automatic download right version of their dependent in their
project.
<parent><version>1.4.2</version></parent>
to
<parent><version>1.4.0</version></parent>
This will change the depend jars file
should match to 1.4.0 version.
Bill of materials
What's
happening here?Embedded Tomcat Server
Convenience - You no need to download the
tomcat.
Servlet container config is now application
config
Standalone application
Useful for microservice architecture -You
no need to deploy for 10 ten time for 10 services. Even a single command you
can achieve it.
1.
Request comes from
user
2.
Request goes to
appropriate controller
3.
Check for uri
4.
Check for the HTTP
method (get, post, put, delete)
5.
Return the response in
JSON format because of @RestController annotation.
Create
a business service
Create a Topic service
@Service -> Sterotype.
It is registered as service in servlet container.
Controller -> Service->
return list -> response JSON
Run the
application
Getting
a single resource
Add
a method to get a single topic in the service class
Add
a method in the controller which invokes the service single resource
Create
a new resource using POST
Add
a service method to add a element in the list
Add
a method in the controller which accept the post topic request
Postman
GET
send
GET
send
method: POST
header:
content-type: application/json
body: raw
{
"id": "javaee",
"name": "Enterprise ",
"description": "Enterprise
Java"
}
Send
Check the response in the postman
Comments
Post a Comment