Important Annotations to review

@RestController

Whenever we try to build REST APIs with the help of Java methods and Http annotations like @GetMapping, @PostMapping, we need to make sure we are putting @RestController on top of the class.

Used on a Controller class that exposes REST APIs.

This way we are communicating to the Spring Boot framework to expose all your Java methods as REST APIs.

@Controller + @ResponseBody

Scenario – If you ever are in a scenario where inside your Controller class you want to write both REST APIs and Spring MVC related methods, then mentioning @RestController is not a good option.

Instead, you should mention @Controller annotation on top of your class and whatever Java methods which you want to expose with the help of REST APIs, on top of them, you can mention @ResponseBody annotation.

So a combination of @Controller annotation + @ResponseBody annotation will provide the same behavior of @RestController.

@ResponseBody 

Whenever you are into a scenario where you are using the @Controller annotation on top of your Java class, you need to mention this @ResponseBody annotation to tell to the spring boot framework that you are expecting the response to be in a JSON format but not in the spring MVC style like HTML format or any other UI format.

When a controller method is annotated with @ResponseBody, Spring automatically serializes the return value of the method to the appropriate format (e.g., JSON, XML) and writes it directly to the HTTP response body.

ResponseEntity class

Whenever you are trying to send all kinds of information like headers, status and body inside the response to your clients, then it is a good idea to use this ResponseEntity.

The ResponseEntity class in Spring MVC is used to customize and control the HTTP response sent back to clients from a controller method. 

It provides more flexibility and control compared to @ResponseBody, allowing you to set not only the response body but also the HTTP status code, headers, and other response details.

@ControllerAdvice annotation

Using this annotation along with the @ExceptionHandler annotation, we can write global exception logic.

Whatever logic you have written inside a Java method that is annotated with @ExceptionHandler and tag that against an exception type.

Whenever that exception occurs inside your controller layer, the logic that you have written inside the ExceptionHandler methods will get executed.

So to activate that behavior, we need to make sure we are using @ControllerAdvice annotation.

→ If you ever run into a scenario where you want strictly your exception methods to return the JSON as an output, then please feel free to use @RestControllerAdvice annotation.

@RequestBody annotation

@RequestBody binds the contents of the HTTP request body to a Java object, typically a DTO (Data Transfer Object) or a domain model that represents the structure of the data being sent by the client.

The @RequestBody annotation in Spring MVC is used to bind the HTTP request body to a method parameter in a controller. It is commonly used in RESTful APIs to extract data sent by the client in the request body, such as JSON, XML, or form data, and convert it into a Java object.