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

我的问题是:有没有在Spring的方式来使用春天验证,自定义注释和批注@NotNull,并得到所有的一切错误的验证或弹簧允许使用仅春季验证?

+0

你需要['CompositeValidator'](https://github.com/mdeinum/spring-utils/blob/master/validation/src/main/java/biz/deinum/multitenant/validation/ CompositeValidator.java),其中包含多个其他验证器。注入你想要执行的2个验证器并注入组合实例'WebDataBinder'。 –

回答

0

其实问题本身是错误的。我得到了答案我使用Spring Validator类来验证所有请求,然后使用@validated而不是@valid。我不再在请求中使用注释,并让课程成为POJO。多数民众赞成它问题解决