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

问题描述 投票:0回答:1

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

我的代码:

@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, int.class, Integer.class ), new Object[] { 2,1, null } );

但是这不合适。 

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

所以我的问题是:

我需要使用什么注释来触发对构造方法的验证。

PS。这是SpringBoot应用程序,
java validation bean-validation spring-annotations spring-validator
1个回答
0
投票
仅通过向其或其参数添加注释就无法更改构造函数的功能。

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

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

© www.soinside.com 2019 - 2024. All rights reserved.