Skip to content

Conversation

@lifefire1
Copy link

Description

Hi Spring team!

I'm a frequent Spring user and I really enjoy working with it. However, I often find myself writing boilerplate code to validate file extensions when handling file uploads.

To solve this, I've created an @AcceptableExtension annotation that allows easy declarative validation of file extensions.

Example Usage

Before:

@PostMapping("/upload")
public String upload(@RequestParam("file") MultipartFile file) {
    String filename = file.getOriginalFilename();
    String extension = filename.substring(filename.lastIndexOf(".") + 1);
    if (!List.of("jpg", "png", "pdf").contains(extension.toLowerCase())) {
        throw new IllegalArgumentException("Invalid file extension");
    }
    // process file...
    return "success";
}

After:

@PostMapping("/upload")
public String upload(
        @AcceptableExtension(extensions = {"jpg", "png", "pdf"})
        @RequestParam("file") MultipartFile file) {
    // file is already validated
    return "success";
}

What's Included

  • @AcceptableExtension annotation with customizable extensions and error messages
  • AcceptableExtensionMethodArgumentResolver for validation logic
  • Comprehensive test suite (15+ test cases)
  • Case-insensitive extension matching

All tests pass successfully.

Thank you for considering this contribution!

Signed-off-by: Алексей Яхненко <yahnenko_av@da.gov.ru>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Nov 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: waiting-for-triage An issue we've not yet triaged or decided on

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants