0

我有以下休息控制器:

 @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
        }
  1. 在控制器中,如果我指定contolbinder.setValidator(new ChekAtleastOneValueValidator());只会转到 ChekAtleastOneValueValidator它不会检查@notnull @ValidDocumentModifiedDate`
  2. 如果我没有,binder.setValidator(new ChekAtleastOneValueValidator()); 那么控件将检查 @notnull@ValidDocumentModifiedDate验证但不 检查ChekAtleastOneValueValidator

我的问题是:在 Spring 中有没有办法使用Spring 验证、自定义注释和 @notnull 注释并获取所有验证的所有错误,或者 spring 只允许使用 Spring 验证器?

4

1 回答 1

1

其实这个问题本身就是错的。我得到了答案,我使用 Spring Validator 类来验证所有传入的请求,然后使用 @validated 而不是 @valid。我不再在请求中使用注释,而是让​​类成为 POJO。就是这样,问题解决了

于 2015-06-10T02:44:11.897 回答