DEV Community

Er. Bhupendra
Er. Bhupendra

Posted on

@GetMapping vs @RequestMapping

Aapne bilkul sahi pakda hai! Kaam dono ka Request Mapping hi hai, lekin dono mein ek bariki (subtle) antar hai.

Asal mein, @GetMapping ek shortcut hai @RequestMapping ka.

Neeche diye gaye points se aapko clear ho jayega:

1. Specific vs General (Khaas vs Aam)

  • @RequestMapping: Yeh "General" hai. Iska istemal aap kisi bhi tarah ki HTTP request (GET, POST, PUT, DELETE, PATCH) ke liye kar sakte hain. Lekin isme aapko batana padta hai ki kaunsa method use kar rahe ho.
    • Example: @RequestMapping(value = "/vendor", method = RequestMethod.GET)
  • @GetMapping: Yeh "Specific" hai. Yeh sirf aur sirf GET requests ke liye bana hai. Aapko alag se method = RequestMethod.GET likhne ki zaroorat nahi padti.
    • Example: @GetMapping("/vendor")

2. Likhne ka Tarika (Syntax)

Dono ka matlab ek hi hai, lekin @GetMapping likhna aasaan hai:

Annotation Syntax
@RequestMapping @RequestMapping(value = "/data", method = RequestMethod.GET)
@GetMapping @GetMapping("/data")

3. Kab Kaunsa Use Karein? (Best Practice)

Spring Boot community mein ek standard practice ban gayi hai:

  1. Class Level par @RequestMapping use karein:
    Pure controller ka ek base path set karne ke liye.

    @RestController
    @RequestMapping("/api/v1") // Pure class ke liye base URL
    public class MyController { ... }
    
  2. Method Level par @GetMapping, @PostMapping etc. use karein:
    Isse code padhne mein bahut aasaan ho jata hai. Dekhte hi pata chal jata hai ki yeh method data fetch (GET) kar raha hai ya data save (POST).

4. History (Kab Aaye?)

  • @RequestMapping: Yeh Spring ke shuruati dino se hai.
  • @GetMapping: Yeh Spring 4.3 version mein introduce kiya gaya tha taaki developers ko baar-baar method = RequestMethod.GET na likhna pade. Isse hum "Composed Annotation" kehte hain.

Summary

Agar aap @GetMapping ki jagah @RequestMapping(method = RequestMethod.GET) likhenge, toh program bilkul sahi chalega, koi error nahi aayegi. Lekin @GetMapping likhna "Clean Code" ki nishani hai kyunki yeh chota aur saaf hai.

Kya aapko @PostMapping ya @PutMapping ke baare mein bhi janna hai?

@RequestMapping aur @GetMapping dono mapping (route) hi karte hain, lekin same “level” ke nahi—@RequestMapping generic hai, @GetMapping GET-specific shortcut hai.

Similarity

Dono ka basic kaam:

  • URL ko controller method se connect karna (handler mapping)

Main difference

1) Scope: generic vs specific

  • @RequestMapping har HTTP method (GET/POST/PUT/DELETE…) ke liye use ho sakta hai. Agar aap method= specify na karo to wo multiple methods match kar sakta hai (context/config ke hisaab se).
  • @GetMapping sirf GET requests ke liye hai. Spring me @GetMapping actually @RequestMapping(method = RequestMethod.GET) ka convenience form hai. (Spring 4.3+) [Spring Docs]

2) Readability (code dekh ke immediately samajh aata hai)

  • @GetMapping("/vendors") dekh ke clear ho jata hai “ye GET endpoint hai”.
  • @RequestMapping("/vendors") me method mention na ho to ambiguity ho sakti hai (GET? POST? etc.)

3) Typical usage pattern

  • Class level par mostly @RequestMapping("/base") (base path) use hota hai.
  • Method level par @GetMapping, @PostMapping, @PutMapping, @DeleteMapping zyada common/clean hota hai.

Example:

@RestController
@RequestMapping("/cloud")   // base path for all endpoints in this controller
public class CloudAPIService {

  @GetMapping("/{vendorid}")  // GET /cloud/{vendorid}
  public CloudVentor get(@PathVariable String vendorid) { ... }

  @PostMapping("/")           // POST /cloud/
  public CloudVentor create(@RequestBody CloudVentor v) { ... }
}
Enter fullscreen mode Exit fullscreen mode

Kya @RequestMapping se GET ka endpoint bana sakte ho?

Haan, bilkul:

@RequestMapping(value="/{vendorid}", method=RequestMethod.GET)
Enter fullscreen mode Exit fullscreen mode

Ye effectively @GetMapping("/{vendorid}") ke equivalent hai. [Spring Docs]

Practical takeaway

  • Sirf GET endpoint hai → @GetMapping best (clean + explicit)
  • Multiple methods / special cases / base path → @RequestMapping (especially class-level)

References

Agar aap chaho to main aapke original code ko sahi mapping + @PathVariable ke saath exact working form me rewrite kar du.

Top comments (0)