Checking the parameters of the base type (String, Integer, ...) on the Spring Controller

I found many tutorials and documentation on input validation using Spring ( https://spring.io/guides/gs/validating-form-input/ ) and using JSR 303. But all of these documents are about how to validate a custom bean (e.g. , user, employer or classes). I really need to check a controller that accepts basic types such as String, Integer, and Lists. My API controller looks like

@Override
@RequestMapping(value="/v1/{var:.*}/resource", method = RequestMethod.GET)
public ResponseEntity<Response> readDocuments(
        @PathVariable(value = "var") String param1,
        @RequestParam(value = "param2", required = false, defaultValue = "string1") String param2,
        @RequestParam(value = "param3") List<String> param3,
        @RequestParam(value = "param4", required = false) Integer param4)
    { 
        // do something
    } 

I want to define some restrictions on the parameters, something like

@Override
@RequestMapping(value="/v1/{var:.*}/resource", method = RequestMethod.GET)
public ResponseEntity<Response> readDocuments(
        @PathVariable(value = "var") String param1,
        @RequestParam(value = "param2", required = false, defaultValue = "string1") String param2,
        @RequestParam(value = "param3") @Size(min = 1) List<String> param3,
        @RequestParam(value = "param4", required = false) @Min(0) @Max(10) Integer param4)
    { 
        // do something
    }

Is it possible? If so, can I define custom annotations according to the JSR 303 directives?

+4
source share

Source: https://habr.com/ru/post/1687540/


All Articles