You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Bean Validation重复注解异常求助:自定义字段约束引发异常

Fixing Bean Validation's Duplicate Annotation Exception for Your Field Constraints

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.xml with:
    <!-- 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>
    
    If you're on Jakarta EE (Java EE 8+), use jakarta.validation:jakarta.validation-api instead.
  • Once upgraded, you can ditch @Pattern.List entirely and repeat @Pattern directly—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

  1. Scan the field for redundant standalone @Pattern annotations and delete them.
  2. Confirm your validation dependencies are up-to-date.
  3. Validate the syntax of your @Pattern.List (if sticking to older versions) to ensure no missing brackets or commas.

内容的提问来源于stack exchange,提问作者akuma8

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:28:26