3

I want to validate extension of Multipartfile object. I added @Valid and my custon annotation to parameter @ImageFileValid but it doesn't work.

@PutMapping("/{id}")
ProductDto updateProduct(@RequestPart @Valid ProductDto product, @PathVariable Long id,@RequestPart @Valid @ImageFileValid MultipartFile image) {
    return productMapper.productToProductDto(productService.update(productMapper.productDtoToProduct(product),id));
}
5
  • baeldung.com/spring-valid-vs-validated !? Commented Dec 4, 2021 at 20:57
  • @xerx593 This is not question about difference between valid and validation Commented Dec 8, 2021 at 20:41
  • the question not...but maybe the answer!? ;) Commented Dec 8, 2021 at 20:46
  • stackoverflow.com/a/57696133/592355 Commented Dec 8, 2021 at 21:03
  • Can you please share ImageFileValid validation ? Commented Dec 1, 2022 at 12:33

1 Answer 1

3

Very short but clear reference from Spring-Boot, Validation:

The method validation feature supported by Bean Validation 1.1 is automatically enabled as long as a JSR-303 implementation (such as Hibernate validator) is on the classpath. This lets bean methods be annotated with javax.validation constraints on their parameters and/or on their return value. Target classes with such annotated methods need to be annotated with the @Validated annotation at the type level for their methods to be searched for inline constraint annotations.

So, please annotate (the containing) controller class with @Validated & report if any issues.


A sample repo at github.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi @xerx593 , i'm using Spring boot 2.7 and i can't get to work. Do you have any idea please? I'm sending the multipart file with a json using mixed content-type.
..the easier in this scenario, @Jedupont: We want (he wanted) to validate the json "inside", in our new scenario, we want to validate the content type(/length/"file name"...) of a (multi)"part" which can/should be json...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.