Introduction to basic spring boot annotations
Posted By : Simran Ahuja | 28-Sep-2021
Spring Boot Annotations
Spring Boot Annotations is a form of metadata that provides data about a program or set of code. In other words, annotations are used to provide supplemental information about a program or set of code.
Core Spring Framework Annotations
@Required: It applies to the bean setter method. It is used to tell that the annotated bean must get populated at configuration time with that required properties, else it will throw an exception BeanInitilizationException.
@Autowired: Spring provides annotation-based auto-wiring by giving us an @Autowired annotation. It is used to autowire spring bean on the setter methods, instance variable, and even constructor. When we use @Autowired annotation, the spring container auto-wires the bean with the matching data-type.
@Configuration: It is for class-level annotation. The class annotated with @Configuration is used by Spring Containers as a source of bean definitions.
@ComponentScan: It is used when we want to scan a certain package for beans. It is used with the annotation called @Configuration. We can also specify the required base packages to scan for Spring Components.
@Bean: It is used as a method-level annotation. It tells the method to produce a bean so as to be managed by Spring Container.
Spring Framework Stereotype Annotations
@Component: It is for a class-level annotation. It is used to declare a Java class as a bean. A Java class annotated as @Component is found during the classpath. The Spring Framework pick it up and configure it in the application context with a Spring Bean.
@Controller: The @Controller is also a class-level annotation. It is a specialization of the @Component. It is marked a class which acts as an web request handler. It is often used to serve the web pages. By default, it returns a string that indicates which route to redirect onto. It is mostly used with @RequestMapping or various other mapping annotations.
@Service: It is also used as a class-level annotation. It tells the Spring that class contains all of the the business logic.
@Repository: It is also a class-level annotation. The repository is a (Data Access Object) that haves the access of database directly. The repository does all of the operations related to the database.
@EnableAutoConfiguration: It auto-configures the bean that is present there 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 have been provided with an alternative of the annotation, i.e. @SpringBootApplication.
@SpringBootApplication: It is a combination of all of the three annotations @EnableAutoConfiguration, @ComponentScan, and @Configuration.
Spring MVC and REST Annotations
- @RequestMapping: It is used to handle and map the web requests. It contains many elements like header and the, method, name, params, path, produces,consumes and value. We can use it with the class as well as the method.
- @GetMapping: It is use to map the HTTP GET requests on the specific handler method. It is used to generate a web service endpoint that fetches or gets the data. It can be used instead of using: @RequestMapping(method = RequestMethod.GET)
- @PostMapping: It is used to map the HTTP POST requests on the specific handler method. It is used to generate a web service endpoint that creates .It can be used instead of using: @RequestMapping(method = RequestMethod.POST)
- @PutMapping: It is used to map the HTTP PUT requests on the specific handler method. It is used to generate a web service endpoint that creates or updates It can be used instead of using: @RequestMapping(method = RequestMethod.PUT)
- @DeleteMapping:It is used to map the HTTP DELETE requests on the specific handler method. It is used to generate a web service endpoint that deletes a resource. It can be used instead of using: @RequestMapping(method = RequestMethod.DELETE)
- @PatchMapping: It is used to map the HTTP PATCH requests on the specific handler method, used when to update only a specific property of object. It can be used instead of using: @RequestMapping(method = RequestMethod.PATCH),
- @RequestBody: It is used to bind HTTP request with a complete object in a method parameter. Internally it basically uses HTTP MessageConverters to convert the body of the request. When we annotate a method parameter with annotaion @RequestBody, the Spring framework binds the incoming HTTP request body to that specific parameter.
- @ResponseBody: It binds the method returning value to the response body. It tells the Spring Boot Framework to serialize a return an object into JSON or XML format.
- @PathVariable: It is used to draw out the values from the URI.
- @RequestParam: It is used to draw out the query parameters form the URL of api. It is also known as a query parameters. It is most suitable for the web applications. It can specify default values if the query parameter is not present in that URL.
- @RequestHeader: It is used to get all the details about the HTTP request headers. We can use this annotation as a method parameter . For each and every detail in the header, we should specify separate annotations. We can also use it multiple time in a method
- @RestController: It can be considered as a combination of both @Controller and @ResponseBody annotations. The @RestController annotation is in itself annotated with the @ResponseBody annotation. It eliminates the need for every time annotating each method with @ResponseBody.