Request Handling and Controller annotations

Some important annotations comes under this category are:

  • @Controller
  • @RestController
  • @RequestMapping
  • @RequestParam
  • @PathVariable
  • @RequestBody
  • @ResponseBody
  • @ModelAttribute

1. @Controller Annotation

This annotation provides Spring MVC features. It is used to create Controller classes and simultaneously it handles the HTTP requests. Generally we use @Controller annotation with @RequestMapping annotation to map HTTP requests with methods inside a controller class.

Example:

Java




//Create a Java class and use @Controller annotation to make it controller class
@Controller
public class MyController{
  public String GFG(){
    //insert code here
  }
}


2. @RestController Annotation

This annotation is used to handle REST APIs such as GET, PUT, POST, DELETE etc. and also used to create RESTful web services using Spring MVC.

It encapsulates @Controller annotation and @ResponseBody annotation with their default attributes.

@RestController = @Controller + @ResponseBody

Example:

Java




//Create a Java class and use @RestController annotation
// to make the class as a request handler
@RestController
public class HelloController{
  public String GFG(){
    //insert code here
  }
}


3. @RequestMapping Annotation

This annotation is used to map the HTTP requests with the handler methods inside the controller class.

Example:

Java




//Java program to demonstrate @RequestMapping annotation
@RestController
public class MyController{
  @RequestMapping(value=" ",method=RequestMapping.GET)
  public String GFG(){
    //insert code here
  }
}


For handling specific HTTP requests we can use

  • @GetMapping
  • @PutMapping
  • @PostMapping
  • @PatchMapping
  • @DeleteMapping

NOTE : We can manually use GET, POST, PUT and DELETE annotations along with the path as well as we can use @RequestMapping annotation along with the method for all the above handler requests

4. @RequestParam Annotation

This annotation is basically used to obtain a parameter from URI. In other words, we can say that @RequestParam annotation is used to read the form data and binds the web request parameter to a specific controller method.

Example:

Java




//Java code to demonstrate @RequestParam annotation
@RestController
public class MyController{
    
  @GetMapping("/authors")
  public String getAuthors(@RequestParam(name="authorName") String name){
    //insert code here
  }
}


5. @PathVariable Annotation

This annotation is used to extract the data from the URI path. It binds the URL template path variable with method variable.

Example:

Java




//Java Program to Demonstrate @PathVariable annotation
@RestController
public class MyController{
    
    @GetMapping("/author/{authorName}")
    public String getAuthorName(@PathVariable(name = "authorName") String name){
      //insert your code here
    }
    
}


6. @RequestBody Annotation

This annotation is used to convert HTTP requests from incoming JSON format to domain objects directly from request body. Here method parameter binds with the body of the HTTP request.

Example:

Java




// Java Code to Demonstrate @RequestBody annotation
@RestController
public class MyController{
    
  @GetMapping("/author")
  public void printAuthor(@RequestBody Author author){
    //insert code here
  }
}


7. @ResponseBody Annotation

This annotation is used to convert the domain object into HTTP request in the form of JSON or any other text. Here, the return type of the method binds with the HTTP response body.

Java




//Java code to demonstrate @ResponseBody annotation
@Controller
public class MyController{
    
  public @ResponseBody Author getAuthor(){
    Author author = new Author();
    author.setName("GFG");
    author.setAge(20);
    return author;
  }
}


8. @ModelAttribute Annotation

This annotation refers to model object in Spring MVC. It can be used on methods or method arguments as well.

Example:

@ModelAttribute("author")
public Author author(){
//insert code here
}

Here, we don’t need to add object explicitly to the model, because automatically object will be the part of the attribute.



Spring Boot – Annotations

Spring Boot Annotations are a form of metadata that provides data about a spring application. Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and setup. Spring Boot is a microservice-based framework and making a production-ready application in it takes very little time. Following are some of the features of Spring Boot:

  • It allows for avoiding heavy configuration of XML which is present in the spring
  • It provides easy maintenance and creation of REST endpoints
  • It includes embedded Tomcat-server
  • Deployment is very easy, war and jar files can be easily deployed in the Tomcat server

Annotations are used to provide supplemental information about a program. It does not have a direct effect on the operation of the code they annotate. It does not change the action of the compiled program. So in this article, we are going to discuss some important Spring Boot Annotations that are available in Spring Boot with examples.

Similar Reads

Spring Boot Annotations

Spring annotations are present in the org.springframework.boot.autoconfigure and org.springframework.boot.autoconfigure.condition packages are commonly known as Spring Boot annotations....

Spring Boot Annotations List

Some of the annotations that are available in this category are:...

Request Handling and Controller annotations:

...

Contact Us