@RequestParam vs @RequestBody — Query String vs JSON Body
Fast recall
Quick revision
Core answer
Understanding where HTTP request data lives and how to extract it in Spring MVC
Trying to use @RequestParam to capture complex JSON objects instead of @RequestBody
Explain the HTTP fundamentals: query params are visible in URL and limited to simple values; request body carries structured data and isn't visible in browser address bar
Interview-ready guide
Interview answer
POST /orders?dryRun=true can bind dryRun with @RequestParam and the order command with @RequestBody. Spring uses Content-Type to choose how to read the body. text/plain can bind String; application/json can bind a DTO through Jackson. A long nested object is clearer in a body, but simple query filters are easier to cache, link, and inspect in the URL.
@RequestParam vs @RequestBody — Query String vs JSON Body
Direct Answer
@RequestParam binds request parameters such as query-string or form values. @RequestBody reads the request body through an HttpMessageConverter. The body is not necessarily JSON and query parameters are not limited to GET. Choose from the API contract: filters/options often fit query parameters, while a structured create/update command commonly belongs in the body.
Code Example
@RestController
@RequestMapping("/api/products")
public class ProductController {
// @RequestParam: extracts from query string
@GetMapping
public List<Product> search(
@RequestParam String category,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "20") int size) {
// Called via: GET /api/products?category=electronics&page=0&size=20
return productService.findByCategory(category, page, size);
}
// @RequestBody: extracts from request body (JSON payload)
@PostMapping
public Product create(@RequestBody CreateProductRequest request) {
// Called via: POST /api/products with JSON body:
// {"name":"Laptop","price":999.99,"category":"electronics"}
return productService.create(request);
}
}
record CreateProductRequest(String name, double price, String category) {}
Common Mistakes & Pitfalls
- Using @RequestParam for complex objects: Freshers sometimes try to pass a whole User object as query params like
?name=John&email=john@example.com&age=25. This works for tiny objects but becomes unmaintainable and hits URL length limits. Complex objects should go in @RequestBody as JSON. - Forgetting @RequestParam is required by default: If a query param is missing, Spring throws an error unless you add
required = falseor provide adefaultValue. Freshers often forget this and get confusing 400 Bad Request errors. - Using @RequestBody with GET requests: HTTP GET requests typically don't have a body (some clients ignore it). If you need to send complex criteria for a search, either use POST for the search endpoint or flatten the params into query strings with multiple @RequestParam annotations.
Check your understanding
Answer these without looking back at the article.
- Can a GET request have a @RequestBody?
- How do you handle both @RequestParam and @RequestBody in the same POST endpoint?
- What is @ModelAttribute and how does it differ from @RequestBody?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.