简体   繁体   English

当从同一个 class 调用方法时,有效不工作 spring 启动

[英]valid not working spring boot when method is called from same class

My car model with bean validation我的车 model 带 bean 验证

@Document(collection = "Cars")
    public class Car {

        public static final String NAME = "car";
        @Id
        private String id;

        @NotBlank(message = "Brand name should n't be empty")
        @CsvBindByName(column = "Car Brand")
        private String brand;

        @NotBlank(message = "Model name should n't be empty")
        @CsvBindByName(column = "Car Model")
        private String model;
    }

Car service汽车服务

 @Service
    @Validated
    public class CarServices {

        @Autowired
        CarRepo repo;

        public Car addCar(@Valid Car car, String traceId) {
              //save to repo
         }

    }
 public HashMap<String, Object> addCars(MultipartFile file, String traceId) {

         //reading csv and passing each car object to addCar   
       Call to addCar()
  }

} }

When I'm calling addCar from controller Valid is working fine,But when I'm calling it from method which is in the same Service class it is not validating Car model.当我从 controller 调用 addCar 时,Valid 工作正常,但是当我从同一服务 class 中的方法调用它时,它没有验证汽车 Z20F35E630DAF44DBFA4C3F68F5399D8Z。

I'm calling addCars from controller我从 controller 打电话给 addCars

How to solve this?What should I do to make that work?如何解决这个问题?我应该怎么做才能做到这一点? What changes I have to make in code?我必须对代码进行哪些更改?

First, you need to understand how spring invokes validators.首先,您需要了解 spring 如何调用验证器。 If you look at spring validation starter, you will see that it defines bean post processor that wraps all beans annotated with valid annotation with proxy object and adds aspects that intercepts methods with valid parameters.如果您查看 spring 验证启动器,您将看到它定义了 bean 后处理器,该处理器使用代理 object 包装所有使用有效注释注释的 bean,并添加了拦截具有有效参数的方法的方面。 So when validated bean/service is injected into dependant object, the proxy is injected instead.因此,当将经过验证的 bean/service 注入依赖的 object 时,会改为注入代理。 Then when the service method is called, the call is being intercepted and validators are executed for every valid parameter.然后当调用服务方法时,调用被拦截,并且为每个有效参数执行验证器。 The same happens for return value.返回值也是如此。 Having said this, ask yourself the question: on which instance you call addCars method?说了这么多,问自己一个问题:在哪个实例上调用 addCars 方法? Proxy or real bean?代理还是真正的bean?

The problem here is that addCar method is not intercepted because is called directly by this instance这里的问题是 addCar 方法没有被拦截,因为是this实例直接调用的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Boot Redis @Cacheable方法未从另一个类调用 - Spring boot Redis @Cacheable method not getting called from another class 从Spring Boot测试调用的@Caching方法[注有@Transactional]无法正常工作 - @Caching method called from spring boot test [annotated with @Transactional] not working 从同一类中的另一个安全方法调用安全方法时,@ PreAuthorize不起作用 - @PreAuthorize not working when a secured method is called from another secured method in same class 带有 Spring Boot 和 Gradle 的 Aws Lambda 无法正常工作。 调用 AWS Lambda 函数时找不到类异常 - Aws Lambda with Spring Boot and Gradle not working. Class not found exception when AWS Lambda function is called 如何模拟同一个类从Spring的method1调用的method2? - How to mock method2 called from method1 from same class wtih Spring? 由另一个 class 调用的同一 class 中的方法调用的事务方法 - Transactional method called by method in same class called from another class @Valid 注释在 spring boot 中不起作用 - @Valid annotation is not working in spring boot @Valid 不工作 @PostMapping spring 启动 - @Valid Not working @PostMapping spring boot Spring 在我将方法移动到同一个 class 后,Boot @Cacheable 停止工作 - Spring Boot @Cacheable stopped working after I moved method to the same class 从 Spring Boot 中具有 @transactional 注释的方法调用时,@Cacheable 不创建缓存键 - @Cacheable not creating cache keys when called from a method having @transactional annotation in spring boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM