Bean Validation重复注解异常求助:自定义字段约束引发异常
Hey there, let's break down why you're hitting this Duplicate Annotation Exception and how to fix it.
First, let's recap your setup: you've applied multiple @Pattern constraints via @Pattern.List to a private field, along with @Size and @NotBlank. The error usually pops up for a few common reasons—let's go through them one by one.
Common Causes & Solutions
1. Accidental Mix of Single @Pattern and @Pattern.List
Double-check if you've added a standalone @Pattern annotation in addition to the @Pattern.List on the same field. For example, this is a mistake that triggers the exception:
// ❌ Wrong: Single @Pattern + @Pattern.List causes duplication conflict @Pattern(regexp = ".+") @Pattern.List({...}) private String password;
Fix: Remove any standalone @Pattern annotations from the field—only keep the @Pattern.List wrapping all your regex constraints.
2. Outdated Bean Validation Version
If you're using Bean Validation 1.0 (JSR-303, the older spec), while it supports container annotations like @Pattern.List, some implementations (like Hibernate Validator 4.x) have bugs around parsing these containers. Plus, Java 8+ and Bean Validation 2.0 (JSR-380) introduced native support for repeatable annotations, making your code cleaner and avoiding container-related issues.
Fix:
- Upgrade your dependencies to Bean Validation 2.0+ and a compatible Hibernate Validator version. For Maven, update your
pom.xmlwith:
If you're on Jakarta EE (Java EE 8+), use<!-- Bean Validation 2.0 API --> <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>2.0.1.Final</version> </dependency> <!-- Hibernate Validator implementation --> <dependency> <groupId>org.hibernate.validator</groupId> <artifactId>hibernate-validator</artifactId> <version>6.1.7.Final</version> </dependency>jakarta.validation:jakarta.validation-apiinstead. - Once upgraded, you can ditch
@Pattern.Listentirely and repeat@Patterndirectly—this is more readable and less error-prone:@Size(min = 5) @NotBlank @Pattern(regexp = "(?=.*[0-9]).+", message = "至少包含一个数字") @Pattern(regexp = "(?=.*[a-z]).+", message = "至少包含一个小写字母") @Pattern(regexp = "(?=.*[A-Z]).+", message = "至少包含一个大写字母") @Pattern(regexp = "(?=.*[!@#$%^&*+=?-_()/\"\\.,<>~`;:]).+", message = "至少包含一个特殊字符") @Pattern(regexp = "(?=\\S+$).+", message = "不允许包含空格") private String password;
3. Conflicting Annotations from Different Libraries
Make sure all your @Pattern annotations come from the correct package: javax.validation.constraints (or jakarta.validation.constraints for Jakarta EE). If you've accidentally imported a different @Pattern from another third-party library, it can cause duplication conflicts.
Fix: Check your imports—remove any non-validation-api @Pattern imports.
Quick Verification Steps
- Scan the field for redundant standalone
@Patternannotations and delete them. - Confirm your validation dependencies are up-to-date.
- Validate the syntax of your
@Pattern.List(if sticking to older versions) to ensure no missing brackets or commas.
内容的提问来源于stack exchange,提问作者akuma8

