RoadToWebDev Day#7-Spring Boot Annotations

This is a part of my ongoing series about exploring the Road To Web Development, you can find rest of the blogs here

Spring Boot is a project that is built on the top of the Spring Framework. It provides an easier and faster way to set up, configure, and run both simple and web-based applications. Spring Boot provides a good platform for Java developers to develop a stand-alone and production-grade spring application that you can just run.

Before we build a REST API, it is necessary to learn some important annotations. So, here they are!

Spring Boot Annotations:

Annotations are a form of metadata that provides data about the program. They are not part of the program itself. Annotations do not have a direct effect on the operation of the code they annotate.

Some important Annotations are explained here:

@SpringBootApplication :

@SpringBootApplication is used to annotate the main class of the spring boot application. It enables Spring Boot autoconfiguration and component scanning.

The SpringApplication.run() method is mandatory because it helps to initiate the Spring framework.

So, @SpringBootApplication is the combination of three annotations that are: @Configuration, @ComponentScan, and @EnableAutoConfiguration. Before understanding these three annotations, let us first know about @Bean

A Spring IoC container is at the core of the spring framework. The container will create the objects, wire them together, configure them, and manage their complete life cycle from creation till destruction. These objects are known as Spring Beans.

@Bean annotation signifies that the given method creates a bean which is managed by the Spring Container. @Configuration annotation indicates that a class declares one or more @Bean methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime.

Spring needs to know in which package beans have been created. For that @ComponentScan is used. It tells Spring in which packages you have annotated classes which should be managed by the Spring.

The spring boot auto-configuration feature tries to configure automatically your Spring application based upon the JAR dependency you have added in the classpath. By default, this auto-configuration feature is not enabled, and you need to opt-in for it by adding the @EnableAutoConfiguration or @SpringBootApplicaiton annotations to one of your @Configuration classes, generally the Main class which is used to run your application. This simplifies the developer’s work by guessing the required beans from the classpath and configure it to run the application.

Spring MVC:

A Spring MVC is a Java framework that is used to build web applications. The Model-view-controller design pattern helps in separating the business logic, presentation logic, and navigation logic. Models are responsible for encapsulating the application data. The Views render a response to the user with the help of the model object. Controllers are responsible for receiving the request from the user and calling the back-end services.

MVC Architecture

@RequestMapping annotation maps HTTP requests to handler methods of MVC and REST controllers. Here I am assuming that you are well acquainted with the HTTP methods. This annotation can be used both at the class and at the method level. For example, the syntax for @RequestMapping for GET method is:

 @RequestMapping(value = “/get/{id}”, method = RequestMethod.GET)

and, it is made simpler by using @GetMapping:

@GetMapping("/get/{id}")

So, the HTTP method-specific shortcut variants of @RequestMapping are:

Spring @RestController annotation is a convenience annotation that is itself annotated with @Controller and @ResponseBody. @RestController takes care of mapping request data to the defined request handler method. Once the response body is generated from the handler method, it converts it to JSON or XML response.

I have not explained all the Spring Boot Annotations here, but this article covers the necessary annotations that a web developer should know before building the REST API. I hope you understood the article!