Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top