Spring Boot MVC: validate a multipart/form-data RequestBody

Hello! I have the need to fill a form with some meta data and a file. The meta data required fields depends on the media "type" that you're selecting from a drop-down. I've thus created a custom validator, associated to a custom annotation and put all on the PostMapping action annotating the parameter with @Valid @MyCustomAnnotation (see below). The thing is that the validator is not being called for a reason beyond my knowledge. May you please help me understand what I'm doing wrong? Thank you very much 🙂 The DTO + validator:
@Data
public class CreateMediaRequestResource {
private MediaType mediaType;
// TODO: various attributes depending on mediaType
private MultipartFile media;

@Documented
@Constraint(validatedBy = CreateMediaRequestValidator.class)
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface CreateMediaRequestValidation {
public String message() default "";
public Class<?>[] groups() default {};
public Class<? extends Payload>[] payload() default {};
}

@Component
public class CreateMediaRequestValidator implements ConstraintValidator<CreateMediaRequestValidation, CreateMediaRequestResource> {
@Override
public boolean isValid(CreateMediaRequestResource value, ConstraintValidatorContext context) {
System.out.println(value);
System.out.println(context);

return false;
}}
}
@Data
public class CreateMediaRequestResource {
private MediaType mediaType;
// TODO: various attributes depending on mediaType
private MultipartFile media;

@Documented
@Constraint(validatedBy = CreateMediaRequestValidator.class)
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface CreateMediaRequestValidation {
public String message() default "";
public Class<?>[] groups() default {};
public Class<? extends Payload>[] payload() default {};
}

@Component
public class CreateMediaRequestValidator implements ConstraintValidator<CreateMediaRequestValidation, CreateMediaRequestResource> {
@Override
public boolean isValid(CreateMediaRequestResource value, ConstraintValidatorContext context) {
System.out.println(value);
System.out.println(context);

return false;
}}
}
The controller's method:
// ...
public ResponseEntity<MediaMetadataResource> createMedia(
@RequestBody @ModelAttribute
@Valid @CreateMediaRequestValidation
CreateMediaRequestResource data
) {
// this won't fail (and the validator won't print)
// ...
public ResponseEntity<MediaMetadataResource> createMedia(
@RequestBody @ModelAttribute
@Valid @CreateMediaRequestValidation
CreateMediaRequestResource data
) {
// this won't fail (and the validator won't print)
I've shortened the code as much as possible to stay in the chars limit, please ask me anything which might be related. Thank you very much!
7 Replies
JavaBot
JavaBot•2y ago
⌛ This post has been reserved for your question.
Hey @elegos! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
elegos
elegosOP•2y ago
Google Docs
spring-poc.zip
elegos
elegosOP•2y ago
(swagger interface on http://localhost:54321/swagger-ui/index.html to test it out)
JavaBot
JavaBot•2y ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one.
elegos
elegosOP•2y ago
Still unanswered. Can you help me please? In few words: is it possible to create a validator for the whole model, instead of the single attributes? Thank you 🙂
JavaBot
JavaBot•2y ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
JavaBot
JavaBot•2y ago
Post Closed
This post has been closed by <@366713959396409346>.
Want results from more Discord servers?
Add your server