Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 1019 Vote(s) - 3.48 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Validation of a list of objects in Spring

#21
To add to the above by @laffuste with Lombok, in Spring Boot 2.7, I have a MyDtoList validator that delegates back down to the singular validator for a plural argument. In my Spring RestController that has singular and plural arguments:

In application yaml:

spring:
jackson:
deserialization:
accept-single-value-as-array: true

In my controller:


@InitBinder("myDto")
public void addMyDtoValidator(WebDataBinder binder) {
binder.addValidators(myDtoValidator);
}

@InitBinder("myDtoList")
public void addMyDtoListValidator(WebDataBinder binder) {
binder.addValidators(myDtoListValidator);
}

Then the validator code:


private MyDtoValidator singleDtoValidator;

public MyDtoListValidator(MyDtoValidator singleDtoValidator) {
this.singleDtoValidator = singleDtoValidator;
}


@Override
public boolean supports(Class<?> clazz) {
return ValidList.class.isAssignableFrom(clazz);
}

@Override
public void validate(Object target, Errors errors) {
if (target == null) {
errors.rejectValue(null, "should not be null error");
} else {
ValidList<MyDto> list = (ValidList<MyDto>) target;
for (MyDtodto: list) {
singleDtoValidator.validate(dto, errors);
}
}
}

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through