I have a standard @RestController
method. I introduced a parameter-object (MyMapQuery
) in order to avoid large number of method arguments:
@RestController
public class MyController {
@RequestMapping(value = "/api/search")
@ResponseBody
public SearchResponse search(MyMapQuery query) {
[...]
}
MyMapQuery
is a standard java bean with setters and getters, so when I pass multiple url parameters http://.../api/search?west=1&east=2&north=20&south=0
, they are correctly filled in.
How do I implement required validation on some of the url parameters?
Spring automatically responds with 400(bad request), when parameters are mapped like below, but does no validation in case of MyMapQuery
.
public SearchResponse search(@RequestParam BigDecimal east, @RequestParam BigDecimal west, ...) {
Just use
public SearchResponse search(@Valid MyMapQuery query) {
and add the necessary bean-validation annotations to the fields of MyMapQuery.