无法触发构造函数级别的验证

我无法仅使用注释来运行构造函数参数验证。

我的代码:

@Data
@Validated
public class TestConstructorLevel
{
    private int a;
    private int b;

    private Integer nullValue;

    public TestConstructorLevel( int a,int b,@NotNull Integer nullValue )
    {
        this.a = a;
        this.b = b;
        this.nullValue = nullValue;
    }

}

我希望我能在某些服务中工作

new TestConstructorLevel(1,2,null)

由于 @NotNull ,我收到 ConstraintViolationException ,但这不起作用。

当我手动触发验证时,我可以运行它的唯一变体,例如:

validator.forExecutables().validateConstructorParameters(
            ReflectionUtils.accessibleConstructor( TestConstructorLevel.class,int.class,Integer.class ),new Object[] { 2,1,null } );

但这不合适。

我尝试将TestConstructorLevel类创建为Spring bean,并且没有任何更改。

所以我的问题是:我需要使用什么批注来触发构造方法上的验证。

PS。这是SpringBoot应用程序

oye07 回答:无法触发构造函数级别的验证

仅通过向构造函数或其参数添加注释就无法更改构造函数的功能。

注解就是:注解。它仅提供有关构造函数参数的一些元数据。

验证器可以使用此元数据来执行其验证工作。但是构造函数只保留一个:构造函数。

本文链接:https://www.f2er.com/2880852.html

大家都在问