I have the following 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();
}
}
And this is the bean where the request will be cast:
public class TestCustomAnotation
{
@ValidDocumentModifiedDate({"7Days", "30Days","60Days"})
String docModifiedDate;
@NotNull
String objectId;
@NotNull
String jobId;
Setter and GEtter
}
binder.setValidator(new
ChekAtleastOneValueValidator());
the contol will only go to
ChekAtleastOneValueValidator
it will not check for @notnull
@ValidDocumentModifiedDate`binder.setValidator(new
ChekAtleastOneValueValidator());
then the control will check for
@notnull@ValidDocumentModifiedDate
validation but not
ChekAtleastOneValueValidator
.My question is: is there a way in Spring to use Spring validation, custom annotation and @notnull annotation and get all the error of all the validation or spring allows to use only Spring validators?
Actually the question itself was wrong. I got the answer I use a Spring Validator class to validate all the request comming in and then use @validated in stead of @valid. I don't use annotation at the request anymore and let the class be a POJO. thats it problem solved