我正在使用弹簧验证(@Validated),如何进一步停止验证,以便第一次验证失败时,返回错误响应
控制器:
@RestController
public class TestController {
public ResponseEntity<?> Test(@RequestBody @Validated final TestPojo testPojo){
}
}
验证:
public class TestValidator implements Validator {
@Override
public boolean supports(Class<?> clazz) {
return clazz != null && TestPojo.class.isAssignableFrom(clazz);
}
@Override
public void validate(Object target, Errors errors) {
if (!(target instanceof TestPojo)) {
errors.reject("validation.invalid.type");
} else {
rejectIfEmptyOrWhitespace(errors, "fname", "validation.field.required");
rejectIfEmptyOrWhitespace(errors, "lname", "validation.field.required");
rejectIfEmptyOrWhitespace(errors, "email", "validation.field.required");
}
}
}
输出回复:
Errors:
fname is required
lname is required
email is required
显示所有错误对客户端来说不是一个好主意,因此如果fname验证失败,如何进一步停止验证。