🗺️
Spring MVC

Request Mapping

@GetMapping, @PostMapping waghera
💡 Request mapping ek POSTAL ADDRESS SYSTEM hai — har URL (address) EK SPECIFIC method (house) tak POINT karta hai. Class-level @RequestMapping ek "society name" hai, method-level mapping us society ke andar "house number".

@RequestMapping URL patterns ko controller methods se JODता hai. @GetMapping, @PostMapping, @PutMapping, @DeleteMapping SPECIFIC HTTP methods ke SHORTHAND versions hain — REST conventions follow karte hain (GET = fetch, POST = create, PUT = update, DELETE = remove).

Class-level @RequestMapping("/api/users") ek COMMON PREFIX define karta hai — HAR method ka apna mapping ISKE RELATIVE hota hai. Isse related endpoints EK controller class mein logically ORGANIZE hote hain.

@RestController
@RequestMapping("/api/orders")
public class OrderController {
    @GetMapping                          // GET /api/orders
    public List<Order> getAll() { }

    @GetMapping("/{id}")                 // GET /api/orders/{id}
    public Order getOne(@PathVariable Long id) { }

    @PostMapping                          // POST /api/orders
    public Order create(@RequestBody OrderDto dto) { }

    @PutMapping("/{id}")                 // PUT /api/orders/{id}
    public Order update(@PathVariable Long id, @RequestBody OrderDto dto) { }

    @DeleteMapping("/{id}")              // DELETE /api/orders/{id}
    public void delete(@PathVariable Long id) { }
}
🗺️
Request mapping ek POSTAL ADDRESS SYSTEM hai — har URL (address) EK SPECIFIC method (house) tak POINT karta hai. Class-level @RequestMapping ek "society name" hai, method-level mapping us society ke andar "house number".
1 / 2
⚡ Quick Recap
  • @GetMapping/@PostMapping/@PutMapping/@DeleteMapping = REST-friendly shortcuts
  • Class-level mapping = common prefix, method-level = specific path
  • RESTful convention: HTTP method = operation, URL = resource
Is page mein (2 subtopics)

@RequestMapping GENERIC hai (method=RequestMethod.GET specify karna padta hai). @GetMapping, @PostMapping, @PutMapping, @DeleteMapping, @PatchMapping SHORTHAND versions hain — zyaada READABLE, aur most common use-case ke liye SPECIFICALLY design kiye gaye.

// Verbose:
@RequestMapping(value = "/users", method = RequestMethod.GET)

// Concise (PREFERRED):
@GetMapping("/users")

Controller CLASS par @RequestMapping se ek COMMON base path define kiya ja sakta hai — HAR method ka mapping is base path ke RELATIVE hota hai. Isse related endpoints EK class mein LOGICALLY GROUP hote hain.

@RestController
@RequestMapping("/api/users")
public class UserController {
    @GetMapping             // → GET /api/users
    public List<User> getAll() { }

    @GetMapping("/{id}")    // → GET /api/users/{id}
    public User getOne(@PathVariable Long id) { }

    @PostMapping             // → POST /api/users
    public User create(@RequestBody User user) { }
}