HTTP Methods in Spring Boot
Fast recall
Quick revision
Core answer
@GetMapping, @PostMapping, @PutMapping, @PatchMapping, and @DeleteMapping. A controller still has to implement the matching HTTP semantics, validate input, call application logic, and return an appropriate status and representation.Interview-ready guide
Interview answer
In Spring MVC, a mapping annotation connects an HTTP method and path to one controller method. @GetMapping, @PostMapping, @PutMapping, @PatchMapping, and @DeleteMapping are method-specific shortcuts for @RequestMapping. A class-level @RequestMapping("/orders") can hold the common path, while each method adds its own path and HTTP method.
The method parameters say where input comes from. @PathVariable long id reads the 42 in /orders/42. @RequestParam String status reads a query value such as ?status=paid. @RequestBody CreateOrderRequest request asks an HTTP message converter, commonly backed by Jackson for JSON, to create the DTO. Adding @Valid runs Bean Validation before normal controller logic when validation is configured.
Because @RestController includes response-body handling, returning an OrderResponse normally serializes that DTO into the response. Use ResponseEntity when the result needs an explicit status or headers. For a successful create, return ResponseEntity.created(location).body(created); sends 201 Created, a Location header, and the created representation. A delete with nothing to return can use a 204 response instead.
The annotation chooses the handler; it does not make the endpoint correct. A method marked @GetMapping must still avoid a requested state change, and a @PutMapping implementation must preserve PUT's idempotent replacement meaning. Keep HTTP translation in the controller, call a service for business work, and use request and response DTOs instead of exposing persistence entities as the public contract.
HTTP Methods in Spring Boot
Direct Answer
Spring MVC maps methods with @GetMapping, @PostMapping, @PutMapping, @PatchMapping, and @DeleteMapping. A controller still has to implement the matching HTTP semantics, validate input, call application logic, and return an appropriate status and representation.
Deep Dive & Architecture
Spring MVC's DispatcherServlet receives the request. Handler mapping compares the HTTP method, path, requested media types, and other mapping conditions to select one controller method. If the path matches but the HTTP method is not allowed, the request must not be sent to a differently annotated handler.
After a handler is selected, argument resolvers read path and query values. An HttpMessageConverter reads the request body; in a typical Spring Boot JSON API, Jackson converts JSON into the request DTO. When the parameter has @Valid and Bean Validation is available, invalid input can be rejected before the controller calls its service.
On the way back, a return-value handler and message converter build the HTTP response. A plain object from a @RestController is normally written as response content. ResponseEntity is useful when the controller must also choose a status or add headers such as Location. The mapping starts the request flow; the controller still has to return the correct HTTP result and keep business work in an application service.
Code Example
@RestController
@RequestMapping("/orders")
class OrderController {
private final OrderService orders;
OrderController(OrderService orders) {
this.orders = orders;
}
@PostMapping
ResponseEntity<OrderResponse> create(
@Valid @RequestBody CreateOrderRequest request) {
OrderResponse created = orders.create(request);
URI location = URI.create("/orders/" + created.id());
return ResponseEntity.created(location).body(created);
}
@GetMapping("/{id}")
OrderResponse find(@PathVariable("id") long id) {
return orders.find(id);
}
}
OrderService, CreateOrderRequest, and OrderResponse are application types. They are assumed here so the example can stay focused on the controller's HTTP mapping.
Check your understanding
Answer these without looking back at the article.
- What is the difference between `@Controller` and `@RestController`?
- When should a handler return `ResponseEntity`?
- Why should request DTOs be separate from JPA entities?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.