Spring Boot
Spring Boot
Spring Boot is a Spring module that provides the RAD (Rapid Application
Development) feature to the Spring framework.
We can use Spring STS IDE or Spring Initializr to develop Spring Boot
Java applications.
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:
Web Development
It is a well-suited Spring module for web application development. We can
easily create a self-contained HTTP application that uses embedded
servers like Tomcat, Jetty, or Undertow. We can use the spring-boot-
starter-web module to start and run the application quickly.
SpringApplication
Admin Support
Spring Boot provides the facility to enable admin-related features for the
application. It is used to access and manage applications remotely. We
can enable it in the Spring Boot application by
using spring.application.admin.enabled property.
Externalized Configuration
Properties Files
YAML Support
It provides a convenient way of specifying the hierarchical configuration. It
is a superset of JSON. The SpringApplication class automatically supports
YAML. It is an alternative of properties file.
Type-safe Configuration
Logging
Spring Boot uses Common logging for all internal logging. Logging
dependencies are managed by default. We should not change logging
dependencies if no customization is needed.
Security
Spring Boot applications are spring bases web applications. So, it is secure
by default with basic authentication on all HTTP endpoints. A rich set of
Endpoints is available to develop a secure Spring Boot application.
Creating Project
Spring Initializr
Spring Initializr is a web-based tool provided by the Pivotal Web
Service. With the help of Spring Initializr, we can easily generate the
structure of the Spring Boot Project. It offers extensible API for creating
JVM-based projects.
It also provides various options for the project that are expressed in a
metadata model. The metadata model allows us to configure the list of
dependencies supported by JVM and platform versions, etc. It serves its
metadata in a well-known that provides necessary assistance to third-
party clients.
Skip 10s
Generating a Project
Before creating a project, we must be friendly with UI. Spring Initializr UI
has the following labels:
Step 2: Provide the Group and Artifact name. We have provided Group
name com.javapoint and Artifact spring-boot-example.
File -> Import -> Existing Maven Project -> Next -> Browse -> Select the
project -> Finish
It takes some time to import the project. When the project imports
successfully, we can see the project directory in the Package Explorer.
The following image shows the project directory:
Spring Boot Annotations
Spring Boot Annotations is a form of metadata that provides data about a
program. In other words, annotations are used to
provide supplemental information about a program. It is not a part of the
application that we develop. It does not have a direct effect on the
operation of the code they annotate. It does not change the action of the
compiled program.
Example
Example
1. @Component
2. public class Customer
3. {
4. private Person person;
5. @Autowired
6. public Customer(Person person)
7. {
8. this.person=person;
9. }
10.}
Example
1. @Configuration
2. public class Vehicle
3. {
4. @BeanVehicle engine()
5. {
6. return new Vehicle();
7. }
8. }
@ComponentScan: It is used when we want to scan a package for
beans. It is used with the annotation @Configuration. We can also specify
the base packages to scan for Spring Components.
Example
1. @ComponentScan(basePackages = "com.javapoint")
2. @Configuration
3. public class ScanComponent
4. {
5. // ...
6. }
Example
1. @Bean
2. public BeanExample beanExample()
3. {
4. return new BeanExample ();
5. }
Example
1. @Component
2. public class Student
3. {
4. .......
5. }
Example
1. @Controller
2. @RequestMapping("books")
3. public class BooksController
4. {
5. @RequestMapping(value = "/{name}", method = RequestMethod.G
ET)
6. public Employee getBooksByName()
7. {
8. return booksTemplate;
9. }
10.}
@Service: It is also used at class level. It tells the Spring that class
contains the business logic.
Example
1. package com.javapoint;
2. @Service
3. public class TestService
4. {
5. public void service1()
6. {
7. //business code
8. }
9. }
1. package com.javapoint;
2. @Repository
3. public class TestRepository
4. {
5. public void delete()
6. {
7. //persistence code
8. }
9. }
Example
1. @Controller
2. public class BooksController
3. {
4. @RequestMapping("/computer-science/books")
5. public String getAllBooks(Model model)
6. {
7. //application code
8. return "bookList";
9. }
o @GetMapping: It maps the HTTP GET requests on the specific handler
method. It is used to create a web service endpoint that fetches It is used
instead of using: @RequestMapping(method = RequestMethod.GET)
o @PostMapping: It maps the HTTP POST requests on the specific
handler method. It is used to create a web service endpoint
that creates It is used instead of using: @RequestMapping(method =
RequestMethod.POST)
o @PutMapping: It maps the HTTP PUT requests on the specific handler
method. It is used to create a web service endpoint
that creates or updates It is used instead of
using: @RequestMapping(method = RequestMethod.PUT)
o @DeleteMapping: It maps the HTTP DELETE requests on the specific
handler method. It is used to create a web service endpoint that deletes a
resource. It is used instead of using: @RequestMapping(method =
RequestMethod.DELETE)
o @PatchMapping: It maps the HTTP PATCH requests on the specific
handler method. It is used instead of using: @RequestMapping(method
= RequestMethod.PATCH)
o @RequestBody: It is used to bind HTTP request with an object in a
method parameter. Internally it uses HTTP MessageConverters to
convert the body of the request. When we annotate a method parameter
with @RequestBody, the Spring framework binds the incoming HTTP
request body to that parameter.
o @ResponseBody: It binds the method return value to the response body.
It tells the Spring Boot Framework to serialize a return an object into JSON
and XML format.
o @PathVariable: It is used to extract the values from the URI. It is most
suitable for the RESTful web service, where the URL contains a path
variable. We can define multiple @PathVariable in a method.
o @RequestParam: It is used to extract the query parameters form the
URL. It is also known as a query parameter. It is most suitable for web
applications. It can specify default values if the query parameter is not
present in the URL.
o @RequestHeader: It is used to get the details about the HTTP request
headers. We use this annotation as a method parameter. The optional
elements of the annotation are name, required, value,
defaultValue. For each detail in the header, we should specify separate
annotations. We can use it multiple time in a method
o @RestController: It can be considered as a combination
of @Controller and @ResponseBody annotations. The @RestController
annotation is itself annotated with the @ResponseBody annotation. It
eliminates the need for annotating each method with @ResponseBody.
o @RequestAttribute: It binds a method parameter to request attribute. It
provides convenient access to the request attributes from a controller
method. With the help of @RequestAttribute annotation, we can access
objects that are populated on the server-side.