gpt4 book ai didi

java - Spring RESTful Web 服务 @initbinder 不允许其他验证

转载 作者:行者123 更新时间:2023-12-01 11:32:57 25 4
gpt4 key购买 nike

我有以下 Rest Controller :

 @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. 在 Controller 中,如果我指定 binder.setValidator(new
    ChekAtleastOneValueValidator());
    控件只会去 ChekAtleastOneValueValidator它不会检查 @notnull@ValidDocumentModifiedDate`
  2. 如果我没有binder.setValidator(new
    ChekAtleastOneValueValidator());
    然后 Controller 将检查 @notnull@ValidDocumentModifiedDate验证但不 ChekAtleastOneValueValidator .

我的问题是:Spring中有没有一种方法可以使用Spring验证,自定义注释和@notnull注释并获取所有验证或Spring允许的所有错误仅使用 Spring validator ?

最佳答案

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

关于java - Spring RESTful Web 服务 @initbinder 不允许其他验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30273143/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com