@RequestMapping, @GetMapping, @PostMapping — Spring Technical Interview Questions & Answers
Mapping HTTP routes to controller methods.
@RequestMapping, @GetMapping, @PostMapping
- Mapping annotations connect a URL + HTTP method to a Java method.
- @GetMapping for read, @PostMapping for create, @PutMapping for update, @DeleteMapping for delete.
- Class-level @RequestMapping sets a shared base path like /api/orders.
- These shortcuts replace longer @RequestMapping(method = ... ) forms.
- Show one class-level path + one method-level GetMapping.
Technical Interview Tip: Map CRUD verbs to the four mapping annotations.
Code Examples for @RequestMapping, @GetMapping, @PostMapping
@RequestMapping, @GetMapping, @PostMapping — working example
import java.util.Map;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/users")
class UserController {
@GetMapping("/{id}")
ResponseEntity<String> get(@PathVariable Long id) {
return ResponseEntity.ok("user-" + id);
}
@GetMapping
String byRole(@RequestParam(defaultValue = "user") String role) {
return "role=" + role;
}
@PostMapping
ResponseEntity<String> create(@RequestBody Map<String, String> body) {
return ResponseEntity.status(201).body("created:" + body.get("email"));
}
}@RequestMapping, @GetMapping, @PostMapping — @RestController = API JSON/text; @PathVariable from URI, @RequestParam from query, @RequestBody from JSON body.
How to Explain @RequestMapping, @GetMapping, @PostMapping to an Interviewer
What is @RequestMapping, @GetMapping, @PostMapping (sb rest)?
Used for GET /api/marks/{roll} a campus API Not just the heading
Give one small example of @RequestMapping, @GetMapping, @PostMapping (sb rest).
@RestController + service call Mapping HTTP routes to controller methods
What mistake do beginners make with @RequestMapping, @GetMapping, @PostMapping (sb rest)?
business logic inside the controller Show the fix
Where do you use @RequestMapping, @GetMapping, @PostMapping (sb rest)?
a campus API GET /api/marks/{roll}