What is the difference between RequestMapping and GetMapping in Spring Boot?

devquora
devquora

Posted On: Feb 22, 2018

 

    1 Answer Written

  •  devquora
    Answered by Satyam

    Difference between Requestmapping and Getmapping

    RequestMapping: Requestmapping is one of the annotations of Spring MVC. Spring is a popular Java application framework used for building enterprise applications. Requestmapping annotation is basically used for mapping web requests onto some specific handler classes and some particular handler methods. The main benefit of this annotation is that it can be applied to the controller class as well as methods. Generally, @requestmapping is seen as a class-level annotation. Requestmapping is refined with the help of method level, get mapping.

    It is always advised to be specific while declaring requestmapping on the controller methods. This annotation is lengthy and requires more typing than getmapping method. As a result, the method level is always left behind during the process. Thus, because of its time taking nature and requirements of long typed code in requestmapping, it is often used for framing the base path and not for the further levels. Coding with the help of this notation can result in a complex and lengthy form of code which may trouble coders as it becomes difficult to take out errors from so many lines of code.

    Getmapping: Getmapping is also a Spring notation that is widely used in mapping HTTP GET requests, onto some specific handler methods. Getmapping is not generally used in mapping handler classes. This feature differentiates getmapping and requestmapping annotation from each other. It can be considered as an annotation that acts as a shortcut of requestmapping annotation. Getmapping requests handler paths onto specific handler methods. The lines of code are comparatively less than that of requestmapping.

    Its request method can be framed as method= RequestMethod.GET. Getmapping is a newer annotation that has been developed to overcome the drawbacks of requestmapping. It supports several attributes such as consume like @requestmapping. Mostly it is a specialization with requestmapping annotation that can easily be used to map and get requests only. The code mentioned below uses the get mapping annotation rather than requestmapping annotation which is a method attribute and not specified as getmapping always maps in HTTP Get method.

    @Getmapping("/peson/{personId}") By default, the entire requests are assumed to be originated from HTTP Get type.

Related Questions

Please Login or Register to leave a response.

Related Questions

Spring Boot Interview Questions

What does Spring Boot mean?

Spring Boot is a system from “The Spring Team” to facilitate the bootstrapping and development of new Spring Applica..

Spring Boot Interview Questions

What are the various Advantages Of Using Spring Boot?

Here are some of the various advantages of using Spring Boot:It is quite easy to create Spring Based applications with..

Spring Boot Interview Questions

What are the various features of Spring Boot?

Various Spring Boot Features are as follows:Web Development Spring Application Application occasions and listeners Adm..