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!

@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
return true;
}
}
return false;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    if (acceptableExtensions.length == 0) {
        return true;
    }

    return Arrays.stream(acceptableExtensions)
            .anyMatch(acceptable -> acceptable.equalsIgnoreCase(extension));

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment has been corrected

Signed-off-by: Алексей Яхненко <firend6334@gmail.com>
@lifefire1 lifefire1 force-pushed the feature/addAceptableExtension branch from 3ec4588 to 3f1a2ed Compare November 12, 2025 05:05
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.

4 participants