@NotNull 注释不起作用 - 传递 null 时不抛出异常

@NotNull annotation not working - Doesn't throw an exception when null is passed

即使我将 null 传递给函数,下面的代码也会输出“内部方法”。 它应该抛出异常。 下面的代码有什么问题?为什么传空参数不抛异常

    import org.springframework.validation.annotation.Validated;
    import javax.validation.constraints.NotNull;
    @Validated
    class MyClass {
        public static void main(String[] args) {
            MyClass m = new MyClass();
            m.wontTakeNull(null);
        }
        
        public void wontTakeNull(@NotNull String param) {
            System.out.println("Inside Method");
        }
        
    }

如果要验证 MyClass,它需要是一个 Spring-managed bean。目前您正在自己创建它,然后在其上调用一个方法:

MyClass m = new MyClass();
m.wontTakeNull(null);

这意味着 Spring 对 m 一无所知,并且它不是 Spring 托管 bean。因此,当您使用 null 调用 wontTakeNull 时,Spring 无法执行验证。

要使 m 成为 Spring 管理的 bean,您需要使用 @Component 注释它并使用组件扫描或 return 它的一个实例@Bean 配置中的方法 class。然后你需要让 Spring 注入一个 MyClass 的实例而不是你自己创建它。这是一个完整的示例,展示了基于 @Component 的方法:

package com.example.demo;

import javax.validation.constraints.NotNull;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

@SpringBootApplication
public class ValidationExampleApplication {
    
    public ValidationExampleApplication(MyClass myClass) {
        myClass.wontTakeNull(null);
    }

    public static void main(String[] args) {
        SpringApplication.run(ValidationExampleApplication.class, args);
    }
    
    @Component
    @Validated
    static class MyClass {
        
        public void wontTakeNull(@NotNull String param) {
            System.out.println("Inside Method");
        }
        
    }

}

此应用程序无法启动,根本原因如下:

Caused by: javax.validation.ConstraintViolationException: wontTakeNull.param: must not be null
    at org.springframework.validation.beanvalidation.MethodValidationInterceptor.invoke(MethodValidationInterceptor.java:120) ~[spring-context-5.3.9.jar:5.3.9]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.9.jar:5.3.9]
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750) ~[spring-aop-5.3.9.jar:5.3.9]
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:692) ~[spring-aop-5.3.9.jar:5.3.9]
    at com.example.demo.ValidationExampleApplication$MyClass$$EnhancerBySpringCGLIB$020c75.wontTakeNull(<generated>) ~[main/:na]
    at com.example.demo.ValidationExampleApplication.<init>(ValidationExampleApplication.java:14) ~[main/:na]
    at com.example.demo.ValidationExampleApplication$$EnhancerBySpringCGLIB$c0df4b8.<init>(<generated>) ~[main/:na]
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490) ~[na:na]
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:208) ~[spring-beans-5.3.9.jar:5.3.9]
    ... 20 common frames omitted