Spring Boot Annotations

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Spring Boot Annotations

o @EnableAutoConfiguration:
- It auto-configures the bean that is present in the classpath and configures it to run the
methods.
- The use of this annotation is reduced in Spring Boot 1.2.0 release because developers
provided an alternative of the annotation, i.e. @SpringBootApplication.
o @SpringBootApplication:
- It is a combination of three annotations @EnableAutoConfiguration,
@ComponentScan, and @SpringBootConfiguration.

Spring MVC and REST Annotations

o @RequestMapping:
- It is used to map the web requests. It has many optional elements like consumes,
header, method, name, params, path, produces, and value. We use it with the class
as well as the method.
o @GetMapping:
- It maps the HTTP GET requests on the specific handler method in a Spring MVC
controller.
- It is used to create a web service endpoint that fetches data.
- It is used instead of using: @RequestMapping(method = RequestMethod.GET)
o @PostMapping:
- It maps the HTTP POST requests on the specific handler method in a Spring MVC
controller.
- 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 to a specific handler method in a Spring MVC
controller.
- It is used to create a web service endpoint that creates or updates resources.
- It is used instead of using: @RequestMapping(method = RequestMethod.PUT)
o @DeleteMapping:
- It maps the HTTP DELETE requests on the specific handler method in a Spring MVC
controller.
- 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 in a Spring MVC
controller.
- It is used to create a web service endpoint that handles partial updates to a resource.
- It is an alternative to 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.

You might also like