Annotations used

  • @Controller: This annotation implicitly marks the class as a component making it eligible for component scanning while signifying that this class invokes business logic, handles incoming web requests and returns customized responses.
  • @ResponseBody: This annotation is used on both class level as well as on method level to indicate that the return value generated by this class or method doesn’t need to be resolved to a view page like HTML or JSP, rather it should be directly serialized into HTTP response body. And this serialization from Java Object to JSON is done by a technology called – Jackson Project that performs Jackson Data Binding under the hood.

Our request method can return any type of data – POJOs (Plain Old Java Objects) and this data will be serialized into JSON by the Jackson Project.

Note: @ResponseBody can be ignored if you’re using @RestController instead of @Controller.

Outputs for every endpoint on POSTMAN:

Endpoint – 1: http://localhost:8080/test/hello

Endpoint – 2: http://localhost:8080/test/sport

Endpoint – 3: http://localhost:8080/test/today/tasks

Notice that we’re selecting the type of request as GET in our POSTMAN this is because all of our methods are only returning some data, not making any changes in a database (as we’re not connected to any database).

And to be more specific, the actual use of @RequestMapping is to define the start of URL and the request handler method will perform some operation and depending on the type of operation a method performs, we will annotate it with GET, PUT, POST or DELETE. Let’s look at some of the most commonly used type of annotation we can use with a specific web request in brief:

  • @GetMapping: This annotation is associated with the requests that are requesting a resource without making any changes in the database.
  • @PostMapping: This annotation is associated with the requests that are trying to add new data in the database. Ex: Adding a new student record that contains all the information regarding a student like student_id, name, enrollment_number, branch etc.
  • @PutMapping: This annotation is associated with the update requests that wants to update or change some already existing data in our database.
  • @DeleteMapping: This annotation is associated with deleting persistent objects (already existing data) from our database.

Spring Boot – @Requestmapping

Spring Boot is the most popular framework of Java for building enterprise-level web applications and back-ends. Spring Boot has a handful of features that support quicker and more efficient web app development. Some of them are Auto-configuration, Embedded Server, opinionated defaults, and Annotation Support. In this article, we’ll be exploring the core annotation of Spring Boot – @RequestMapping which is part of the set of annotations that Spring Boot employs for defining URL endpoints and REST APIs.

Similar Reads

@RequestMapping

This annotation is a versatile and flexible annotation that can be used with a controller (class) as well as the methods to map specific web requests with the handler methods and controllers. This annotation is part of a larger set of annotations provided by Spring Framework to define URL endpoints and simplify the development of Spring Boot applications....

Step-By-Step Implementation of @RequestMapping annotation

For this article, we’ll be using the following tools:...

Annotations used:

...

Conclusion

...

Contact Us