0
我有以下几点休息控制器:春天RESTful Web服务@InitBinder不允许其他验证
@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();
}
}
而这正是该请求将被投豆:
public class TestCustomAnotation
{
@ValidDocumentModifiedDate({"7Days", "30Days","60Days"})
String docModifiedDate;
@NotNull
String objectId;
@NotNull
String jobId;
Setter and GEtter
}
- 在控制器,如果我指定
binder.setValidator(new ChekAtleastOneValueValidator());
控制器将只去ChekAtleastOneValueValidator
它不会检查@notnull
@ ValidDocumentModifiedDate` - 如果我没有
binder.setValidator(new ChekAtleastOneValueValidator());
那么控件将检查@[email protected]
验证但不是ChekAtleastOneValueValidator
。
我的问题是:有没有在Spring的方式来使用春天验证,自定义注释和批注@NotNull,并得到所有的一切错误的验证或弹簧允许使用仅春季验证?
你需要['CompositeValidator'](https://github.com/mdeinum/spring-utils/blob/master/validation/src/main/java/biz/deinum/multitenant/validation/ CompositeValidator.java),其中包含多个其他验证器。注入你想要执行的2个验证器并注入组合实例'WebDataBinder'。 –