What is the difference between @PathVariable and @RequestParam? @PathVariable और @RequestParam में क्या अंतर है?
@PathVariable extracts a value from the URI path itself, used when the value identifies a specific resource, e.g. /users/{id}. @RequestParam extracts a value from the query string, used for optional filters, pagination, or sorting parameters, e.g. /users?page=2&size=10.
Both can be marked required or optional and support default values, but path variables are conventionally used for resource identity while request parameters are used for query-level options.
@GetMapping("/users/{id}")
public User getUser(@PathVariable Long id) { ... }
@GetMapping("/users")
public List<User> search(@RequestParam(defaultValue = "0") int page,
@RequestParam(required = false) String name) { ... }@PathVariable URI पाथ से ही एक वैल्यू निकालता है, जब वह वैल्यू किसी विशिष्ट रिसोर्स की पहचान करती है, जैसे /users/{id}। @RequestParam क्वेरी स्ट्रिंग से वैल्यू निकालता है, जो वैकल्पिक फिल्टर, पेजिनेशन या सॉर्टिंग पैरामीटर्स के लिए उपयोग होता है, जैसे /users?page=2&size=10।
दोनों को required या optional चिह्नित किया जा सकता है और डिफ़ॉल्ट वैल्यू सपोर्ट करते हैं, लेकिन पाथ वेरिएबल्स परंपरागत रूप से रिसोर्स पहचान के लिए और रिक्वेस्ट पैरामीटर्स क्वेरी-स्तर के विकल्पों के लिए उपयोग होते हैं।
@GetMapping("/users/{id}")
public User getUser(@PathVariable Long id) { ... }
@GetMapping("/users")
public List<User> search(@RequestParam(defaultValue = "0") int page,
@RequestParam(required = false) String name) { ... }Was this answer clear?