Unit-5 Spring Boot
Unit-5 Spring Boot
• Spring Boot uses all the modules of Spring-like Spring MVC, Spring Data, etc.
The architecture of Spring Boot is the same as the architecture of Spring
MVC, except for one thing: there is no need for DAO and DAOImpl classes in
Spring boot. As a summary, in a simple spring boot flow:
• Data access layer gets created and CRUD operations are performed.
• The client makes the HTTP requests.
• The request goes to the controller, and the controller maps that request and
handles it. After that, it calls the service logic if required.
• In the service layer, all the business logic performs. It performs the logic on
the data that is mapped to JPA with model classes.
• A response page is returned to the user if no error occurs.
•
Spring Boot Architecture
Properties file
•
Spring Boot run() method
•
Spring Boot resources folder
•
Spring Boot Normal Application
“pom.xml”
•
Spring Boot Web Application “pom.xml”
•
Spring Boot Runners
• In Spring Boot, runners are components used to execute code when
the application is started. They are typically implemented using
Spring's ApplicationRunner or CommandLineRunner interfaces. These
runners allow you to perform tasks such as database initialization,
data loading, or any custom startup logic.
Application Runner
• The
ApplicationRunner
interface in Spring
Boot provides a way
to execute code
after the
application context
is initialized and
before Spring Boot
starts servicing
incoming requests
CommandLine Runner
• Similar to
ApplicationRunner, import org.springframework.boot.CommandLineRunner;
CommandLineRunn import org.springframework.stereotype.Component;
er is an alternative
interface that @Component
provides a run public class MyCommandLineRunner implements
method with an CommandLineRunner {
array of String
arguments. These @Override
arguments are public void run(String... args) throws Exception {
passed directly to // Code to execute on application startup
the application System.out.println("CommandLineRunner is running...");
when it is started }
from the command }
line.
Springboot Logger
• 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.
Restful Web Services
Create a new package as com.example.demo.controller and create a new class as SampleController , now
make this class as RestController using @RestController annotation
Now, Apply @GetMapping annotation to create an end point to access this controller, given below is
code:
2. PostMapping
3. Spring – Request Body
• @RequestBody: Annotation is used to get the request body
in the incoming request.
4. Spring – Request Mapping and
ResponseBody
• @RequestMapping Annotation which is used to map HTTP requests to
handler methods of MVC and REST controllers.
• The @RequestMapping annotation can be applied to class-level and/or
method-level in a controller.
• The class-level annotation maps a specific request path or pattern onto a
controller.
• You can then apply additional method-level annotations to make mappings
more specific to handler methods.
• @ResponseBody annotation tells a controller that the object returned is
automatically serialized into JSON and passed back into the HttpResponse
object. When you use the @ResponseBody annotation on a method, Spring
converts the return value and writes it to the HTTP response automatically.
5. Spring – Request Mapping and
ResponseBody
6. Spring – PathVariable
• The @PathVariable annotation is used to extract the value from the URI. It is
most suitable for the RESTful web service where the URL contains some
value. Spring MVC allows us to use multiple @PathVariable annotations in
the same method. A path variable is a critical part of creating rest resources.
@GetMapping(path="/hello-world/path-variable/{name}")
public HelloWorldBean helloWorldPathVariable(@PathVariable String name)
{
return new HelloWorldBean(String.format("Hello World, %s", name));
}
URL: http://localhost:8080/hello-world/path-variable/ABES O/P- {“message”:”Hello World,
ABES”}
7. Spring – Request Parameter
• The @RequestParam annotation is used to extract data from
the query parameters in the request URL. Query parameters
are the key-value pairs that appear after the ? in a URL.
Rest API