Spring Controller: RequestParam not validated despite @Valid and @Size

我与影子孤独终老i 提交于 2021-02-09 11:14:26

问题


I have a simple Controller method :

@GetMapping("/search")
    public List<Result> search(@RequestParam @Valid @NotNull @Size(min = 4) String query) {
        return searchService.search(query);
    }

When I omit the "query" parameter, I get a 400 Bad Request, as expected.

Testing the method with these query parameters doesn't work.

All but the last test should return a "400 Bad Request".

"/search"             --> actual 400 Bad Request, test passes
"/search?query="      --> actual 200 Ok, expected 400 because @Size(min=4)
"/search?query=a"     --> actual 200 Ok, expected 400 because @Size(min=4)
"/search?query=ab"    --> actual 200 Ok, expected 400 because @Size(min=4)
"/search?query=abc"   --> actual 200 Ok, expected 400 because @Size(min=4)
"/search?query=abcd"  --> actual 200 Ok, test passes

Why is the @Size(min=4) annotation being ignored?


回答1:


Validating RequestParameters does not work like that out of the box.

You could wrap the parameters in a class like

class SearchRequest {
    @Size(min=4)
    @NotNull
    private String query;
    ...
}

Then change your controller code to

@GetMapping("/search")
public List<Result> search(@ModelAttribute @Valid SearchRequest searchRequest) {
    return searchService.search(searchRequest.getQuery());
}

Now this is one way to do it, what you want is probably achievable using the @Validated annotation on the class, but I don't know anything about that but it seems to be covered here: https://sdqali.in/blog/2015/12/04/validating-requestparams-and-pathvariables-in-spring-mvc/




回答2:


You can use RequestParameters validation out of the box)

You need to add MethodValidationPostProcessor to your configuration

@org.springframework.context.annotation.Configuration
public class Configuration {

    @Bean
    public MethodValidationPostProcessor methodValidationPostProcessor() {
        return new MethodValidationPostProcessor();
    }

}

And @Validated Annotation to your controller

@RestController
@Validated
public class MyController {

    @GetMapping("/search")
    public String search(@RequestParam @NotNull @Size(min = 4) String query) {
        return "success";
    }

}

Remember also, that you may want to handle exception, which is thrown if method parameters not valid, you may do it like this

@ControllerAdvice
@Component
public class ExceptionHandler {
    @ExceptionHandler
    @ResponseBody
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public List handle(MethodArgumentNotValidException exception) {
        //do your stuff here
        return exception.getBindingResult().getFieldErrors()
                .stream()
                .map(FieldError::getDefaultMessage)
                .collect(Collectors.toList()));
    }


    @ExceptionHandler
    @ResponseBody
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public List handle(ConstraintViolationException exception) {
       //do your staff here
    }

}


来源:https://stackoverflow.com/questions/53719825/spring-controller-requestparam-not-validated-despite-valid-and-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!