Sub topic 10
Binding several query parameters to one object
This chapter assumes you know @RequestParam and Bean Validation with @Valid from the earlier chapters. It also assumes Spring Boot 3.2 or later (Spring Framework 6.1+), which affects the exception types shown below.
The problem with long parameter lists
A search endpoint often grows many optional filters. Each filter adds another @RequestParam, and the method signature gets hard to read. Defaults and validation rules end up scattered across the parameters.
Spring can instead bind all the query parameters to one object. Put the object in the method signature with no annotation. Spring treats an unannotated parameter of a non-simple type as a model attribute. It reads request parameters by name and fills the object from them.
A record as the query object
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
public record BookSearch(
String author,
@Min(0) Integer page,
@Min(1) @Max(100) Integer size) {
public BookSearch {
if (page == null) page = 0;
if (size == null) size = 20;
}
}Spring calls the record's constructor and passes each query parameter to the component with the same name. A parameter that is absent arrives as null. That is why the components are Integer and not int, and why the compact constructor applies the defaults. Validation runs after construction, so the constraints see the defaulted values.
The controller method takes the record:
import jakarta.validation.Valid;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/books")
public class BookSearchController {
private final BookService service;
public BookSearchController(BookService service) {
this.service = service;
}
@GetMapping("/search")
public List<Book> search(@Valid BookSearch search) {
return service.search(search.author(), search.page(), search.size());
}
}BookService and Book are the types from the earlier chapters. There is no @RequestBody here. The data still comes from the query string, not from JSON.
Trying it
curl "http://localhost:8080/api/books/search?author=Le%20Guin&size=10"
curl "http://localhost:8080/api/books/search?size=500"The first call binds author and size, and page defaults to 0. The second call fails the @Max(100) rule and returns a 400.
Errors on bound objects
Type conversion works differently here than with @RequestParam. A request like ?page=abc does not throw MethodArgumentTypeMismatchException. The binder records a field error on the object, and Spring then throws MethodArgumentNotValidException. Constraint violations throw the same exception.
So the MethodArgumentNotValidException handler from the error-handling chapter already covers both cases. The errors map in its response will include a field such as page with a conversion message.
Choosing between the two styles
| Situation | Better choice |
|---|---|
| One or two parameters | `@RequestParam` |
| Many optional filters | Query object |
| Defaults and rules shared by several endpoints | Query object |
| Each parameter needs its own required flag | `@RequestParam` |
Common mistakes
- Adding
@RequestBodyto the query object. Spring then tries to read JSON from the body of a GET request and fails. - Using primitive components such as
intand expectingnullfor a missing value. A primitive cannot holdnull, so the default is not what you intended. - Expecting unknown parameters to cause errors. Extra query parameters are silently ignored.
- Reusing the path of an existing
@GetMappingthat takes@RequestParam. Two methods mapped to the same path and method cause an ambiguous mapping error at startup.
Exercise
A client calls /api/books/search?page=abc. The endpoint takes @Valid BookSearch search, where page is an Integer. Which exception is thrown, and how does it differ from calling a method that has @RequestParam int page?
Show answer
Spring throws MethodArgumentNotValidException, with a type mismatch field error for page. With @RequestParam int page, the failure is a MethodArgumentTypeMismatchException instead, so it needs a separate handler.