0

I want to check file extension on upload usin ConstraintValidator. If the file extension is not as specified in the annotation, then the user should get common constraint validation error response. But when it happens this exception occurs

java.io.FileNotFoundException: C:\Users\Tensky\AppData\Local\Temp\tomcat.4393944258717178584.8443\work\Tomcat\localhost\ROOT\upload_a19ba701_88a1_4eab_88b7_eede3a273fe0_00000011.tmp

And user get Bad Request page

My classes: Annotation @UploadFileTypes:

@Documented
@Constraint(validatedBy = UploadFileTypesValidator.class)
@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface UploadFileTypes {

    String message() default "Example message";

    UploadFileType[] allowedTypes() default {UploadFileType.AUDIO, UploadFileType.VIDEO, UploadFileType.DOCUMENT, UploadFileType.IMAGE};

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};

    @Documented
    @Target({ElementType.FIELD, ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    @interface List{
        UploadFileTypes[] value();
    }
}

Class UploadFileTypesValidator:

public class UploadFileTypesValidator implements ConstraintValidator<UploadFileTypes, MultipartFile> {

    @Override
    public void initialize(UploadFileTypes constraintAnnotation) {
    }

    @Override
    public boolean isValid(MultipartFile value, ConstraintValidatorContext context) {
        return false; //<- always exception
    }
}

And controller (never reach, becouse isValid always return false:

@RequestMapping(path = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity addProperty(@Valid PropertyCreateRequest request) {
        return ResponseEntity.ok(null);
}

Why does this exception occur and how to avoid it?

1 Answers1

0

Problem solved. Spring tried to serialize MultipartFile in error response

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {
...
@Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
        ...
        builder.serializers(new JsonSerializer<MultipartFile>() {
            @Override
            public Class<MultipartFile> handledType() {
                return MultipartFile.class;
            }

            @Override
            public void serialize(MultipartFile value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
                gen.writeStartObject();
                gen.writeStringField("contentType", value.getContentType());
                gen.writeStringField("filename",value.getName());
                gen.writeStringField("originalFilename", value.getOriginalFilename());
                gen.writeNumberField("size", value.getSize());
                gen.writeEndObject();
            }
        });

        converters.add(new MappingJackson2HttpMessageConverter(builder.build()));
    }
}