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));
}
Thilo Schwarz
  • 640
  • 5
  • 24
Mateusz Sobczak
  • 1,551
  • 8
  • 33
  • 67

1 Answers1

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.

xerx593
  • 12,237
  • 5
  • 33
  • 64
  • 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. – Jedupont Apr 27 '23 at 21:04
  • ..the easier in this scenario, @Jedupont: We want (he wanted) to validate the json "inside", [in our new scenario](https://stackoverflow.com/q/76122829/592355), we want to validate the content type(/length/"file name"...) of a *(multi)"part"* which can/should be json... – xerx593 Apr 28 '23 at 01:22