我有以下休息控制器:
@RestController
public class DocumentSearchController_global
{
@InitBinder//("TestCustomAnotation")
protected void initBinder(WebDataBinder binder) {
binder.setValidator(new ChekAtleastOneValueValidator());
}
@RequestMapping(value = "/validator", method = RequestMethod.POST, produces = { MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE })
protected DocumentSearchResponse validatortest(@Valid @RequestBody TestCustomAnotation objDMSRequest, Errors e, BindingResult br) throws AppException
{
if(br.hasErrors())
System.out.println("ERRor");
if (e.hasErrors())
{
System.out.println("Got Error: "+ e.getFieldError());
}
DocumentSearchResponse objDocSearchResponse = null;
return objDocSearchResponse;
}
@ExceptionHandler
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
@ResponseBody
public String handleMethodArgumentNotValidException(
MethodArgumentNotValidException error) {
System.out.println("ERROR-->>>>>>>>>>>>>>>>>>>>>>>>" +error.getMessage());
return "Bad request: " + error.getMessage();
}
}
这是请求将被强制转换的 bean:
public class TestCustomAnotation
{
@ValidDocumentModifiedDate({"7Days", "30Days","60Days"})
String docModifiedDate;
@NotNull
String objectId;
@NotNull
String jobId;
Setter and GEtter
}
- 在控制器中,如果我指定contol
binder.setValidator(new ChekAtleastOneValueValidator());
只会转到ChekAtleastOneValueValidator
它不会检查@notnull
@ValidDocumentModifiedDate` - 如果我没有,
binder.setValidator(new ChekAtleastOneValueValidator());
那么控件将检查@notnull@ValidDocumentModifiedDate
验证但不 检查ChekAtleastOneValueValidator
。
我的问题是:在 Spring 中有没有办法使用Spring 验证、自定义注释和 @notnull 注释并获取所有验证的所有错误,或者 spring 只允许使用 Spring 验证器?