我的控制器如下所示:
@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
}
因此,在验证我的结果对象在验证期间是否遇到任何错误之后,如何抛出MethodArgumentNotValidException?或者Spring应该在验证期间抛出异常?
答案 0 :(得分:9)
如果我没记错的话,如果你没有为{{1}提供MethodArgumentNotValidException
(此处为Errors
)参数,那么Spring应该只抛出BindingResult
带注释的参数。
如果您愿意,可以自己投掷。