Spring-Boot-Fundamentals and Annotations
Spring-Boot-Fundamentals and Annotations
Fundamentals
By Ramesh Fadatare (Java Guides)
Spring Boot
Externalized Con guration
What is a Problem?
Basically, Spring-based applications have a lot of con gurations
For example
When we develop Spring MVC web application using Spring MVC then we need to con gure:
Component scan, Dispatcher Servlet, View resolver, Web jars(for delivering static content) among
other things
When we use Hibernate/JPA in the same Spring MVC application then we would need to con gure a
Data source, Entity manager factory/session factory, Transaction manager among other things.
When you use cache, message queue, NoSQL in the same Spring MVC application then we need to
con gure
Cache con guration
Message queue con guration
NoSQL database con guratio
One more major problem - We need to maintain all integration of different Jar dependencies and it’s
compatible versions
Ramesh Fadatare ( Java Guides)
fi
:
fi
.
fi
fi
fi
.
fi
fi
What is a Solution
> Spring Boot is the solutio
> Spring Boot automatically con gures the
con gurations based on the jar dependencies that we
add to our project
fi
Spring Boot Starters
1. These starters are pre-con gured with the most commonly used
library dependencies so you don’t have to search for the compatible
library versions and con gure them manually
2. For example, when we add the spring-boot-starter-web
dependency, it will by default pull all the commonly used libraries
while developing Spring MVC applications, such as spring-
webmvc, jackson-json, validation-api, and tomcat
3. One more example, the spring-boot-starter-data-jpa starter module
includes all the dependencies required to use Spring Data JPA,
along with Hibernate library dependencies, as Hibernate is the
most commonly used JPA implementation.
Ramesh Fadatare ( Java Guides)
fi
fi
.
fi
fi
@RestController Annotation
1. In order to develop REST web services using Spring MVC, we
need to use @Controllerand @ResponseBody annotations
2. Spring 4.0 introduced @RestController, a specialized version
of the @Controller which is a convenience annotation that
does nothing more than adding the @Controller and
@ResponseBody annotations.
3. In order to create Restful web services using Spring MVC,
you need annotate a Java class with @RestController
annotation.
Ramesh Fadatare ( Java Guides)
@ResponseBody Annotation
1. The @ResponseBody annotation tells a controller that the
object returned is automatically serialized into JSON and
passed back into the HttpResponse object
2. When you use the @ResponseBody annotation on a method,
Spring uses HTTPMessageConverters to converts the return
value based on the MIME types and writes it to the HTTP
response automatically.
@RequestMapping Annotation
1. @RequestMapping is the most common and widely used
annotation in Spring MVC. It is used to map web requests
onto speci c handler classes and/or handler methods
2. @RequestMapping can be applied to the controller class as
well as methods.
ResponseEntity
1. ResponseEntity represents the whole HTTP response: status
code, headers, and body. As a result, we can use it to fully
con gure the HTTP response
2. If we want to use it, we have to return it from the endpoint;
Spring takes care of the rest
3. ResponseEntity is a generic type. Consequently, we can use
any type as the response body.
@PostMapping Annotation
1. The POST HTTP method is used to create a resource and
@PostMapping annotation for mapping HTTP POST requests
onto speci c handler methods.
2. Speci cally, @PostMapping is a composed annotation that acts
as a shortcut for @RequestMapping(method =
RequestMethod.POST).
@RequestBody Annotation
1. The @RequestBody annotation is responsible for retrieving the
HTTP request body and automatically converting it to the Java
object
2. Annotation indicating a method parameter should be bound to
the body of the web request. The body of the request is passed
through an HttpMessageConverter to resolve the method
argument depending on the content type of the request.
@PutMapping Annotation
1. The PUT HTTP method is used to update the resource and
@PutMapping annotation for mapping HTTP PUT requests
onto speci c handler methods.
2. Speci cally, @PutMapping is a composed annotation that acts
as a shortcut for @RequestMapping(method =
RequestMethod.PUT).
@DeleteMapping Annotation
1. The DELETE HTTP method is used to delete the resource and
@DeleteMapping annotation for mapping HTTP DELETE
requests onto speci c handler methods.
2. Speci cally, @DeleteMapping is a composed annotation that
acts as a shortcut for @RequestMapping(method =
RequestMethod.DELETE).
@PathVariable Annotation
1. Spring boot @PathVariable annotation used on a method
argument to bind it to the value of a URI template variable.
Query parameters