Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Interview question

What are the common HTTP mapping annotations in Spring Boot (@GetMapping, @PostMapping, etc.)? स्प्रिंग बूट में सामान्य HTTP मैपिंग एनोटेशन (@GetMapping, @PostMapping आदि) क्या हैं?

Answer

Spring Boot provides shorthand annotations for each HTTP method, all specializations of @RequestMapping: @GetMapping for retrieving resources, @PostMapping for creating resources, @PutMapping for full updates, @PatchMapping for partial updates, and @DeleteMapping for removing resources.

Each maps a controller method to a URL path and restricts it to that HTTP verb, making the API's intent explicit and letting Spring route requests to the correct method automatically based on both path and method.

@RestController
@RequestMapping("/api/products")
public class ProductController {
    @GetMapping public List<Product> getAll() { ... }
    @PostMapping public Product create(@RequestBody Product p) { ... }
    @PutMapping("/{id}") public Product update(@PathVariable Long id, @RequestBody Product p) { ... }
    @DeleteMapping("/{id}") public void delete(@PathVariable Long id) { ... }
}

स्प्रिंग बूट हर HTTP मेथड के लिए शॉर्टहैंड एनोटेशन देता है, जो सभी @RequestMapping की विशेषज्ञताएँ हैं: रिसोर्स प्राप्त करने के लिए @GetMapping, बनाने के लिए @PostMapping, पूर्ण अपडेट के लिए @PutMapping, आंशिक अपडेट के लिए @PatchMapping, और हटाने के लिए @DeleteMapping

हर एनोटेशन एक कंट्रोलर मेथड को URL पाथ से मैप करता है और उसे उस HTTP वर्ब तक सीमित करता है, जिससे API का इरादा स्पष्ट होता है।

@RestController
@RequestMapping("/api/products")
public class ProductController {
    @GetMapping public List<Product> getAll() { ... }
    @PostMapping public Product create(@RequestBody Product p) { ... }
    @PutMapping("/{id}") public Product update(@PathVariable Long id, @RequestBody Product p) { ... }
    @DeleteMapping("/{id}") public void delete(@PathVariable Long id) { ... }
}

Was this answer clear?