I have multiple endpoints in my RestController
s that follow some similar signature:
@GetMapping
public SomeItem get(@RequestParam String sortBy,
@RequestParam String sortField,
@RequestParam int pageNumber,
@RequestParam int pageSize) {
QueryOptions queryOptions = QueryOptions.of(sortyBy, sortField, pageNumber, pageSize);
// ...
}
I was wondering if there is a way of removing this code duplication from all the different methods and move the QueryOptions
construction somewhere before the RestController
method, so that I could use a method like the following:
@GetMapping
public SomeItem get(QueryOptions queryOptions) {
// ...
}
How can I do this? Maybe adding a filter in the filterchain?
It turns out that this is supported out of the box:
@Getter
@Setter
public class QueryOptions {
private String pageNumber;
private String pageSize;
private String orderBy;
private String sortField;
}
And then you can accept this Class in the Controller method:
@GetMapping
public SomeItem get(QueryOptions queryOptions) {
// ...
}