简体   繁体   中英

MethodArgumentNotValidException not thrown

My controller looks like the following:

@RequestMapping(value = "/cars/{types}", method = RequestMethod.PUT,
        headers = "Accept=application/json")
@ResponseStatus(HttpStatus.OK)
public void startEngine(
        @PathVariable @Min(0) String types, @RequestBody @Valid someObject request, BindingResult result)
        throws MethodArgumentNotValidException {

    if(result.hasErrors())
    {
        System.out.println("Error");
        //Should I be throwing MethodArgumentNotValidException here? And if so how? I don't know how to retrieve the first parameter for it's constructor (MethodParameter object)
    }
    //Controller code
}

So after I verify whether or not my result object encountered any errors during validation, how can I then throw the MethodArgumentNotValidException? Or should Spring be already throwing that exception during validation?

If I remember correctly, Spring should throw MethodArgumentNotValidException only if you have not provided an Errors (here, BindingResult ) parameter for the @Valid annotated parameter.

You can throw it yourself if you would like to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM