Spring 5中的服务层Bean验证?

huangapple go评论90阅读模式
英文:

Service layer bean validation in Spring 5?

问题

我需要在 Spring 5 应用程序的服务层方法中验证作为参数传递的 bean。我可以让控制器上的验证起作用,但在服务层上,@Valid 注解被忽略了。该服务类带有 @Validated 注解,并且它的一个实例被自动注入到调用该方法的控制器类中。

我需要在非 Boot 应用程序中实现这一点。我找到了一些说明,但它们都是特定于 Boot 的。

我在这里漏掉了什么,为什么这在控制器上运行,但在从控制器调用服务层的调用中却不起作用?根据一些搜索,在控制器级别上,这可能与 DispatcherServlet 有某种连接?

我需要做什么才能让这在服务层起作用?我希望在服务层执行此操作的原因是,在其他应用程序的服务层上进行了验证(Jersey 框架 + 旧版 Spring),而其他人希望保持一致性。

英文:

I need to validate beans given as argument to service layer methods in a Spring 5 application. I can get validation to work on controllers but on service layer the @Valid annotation is ignored. The service class is annotated with @Validated and an instance of it is autowired into the controller class thats making the method call.

I need to do this in a non-Boot application. I've found some instructions but they have been Boot-specific.

What am I missing here, why does this work on controller but not on the call from controller to service layer? Based on some googling, on controller level this might be somehow connected to the DispatcherServlet?

What do I need to do to make this work on service layer? The reason I want to do this on service layer is that we have validation on service layer in other apps (Jersey framework + older Spring) and others want consistency.

答案1

得分: 1

以下是翻译好的内容:

对于 @Controller 类处理程序方法起作用的原因是,Spring 使用了一个特殊的 HandlerMethodArgumentResolver,称为 ModelAttributeMethodProcessor,它在解析要在处理程序方法中使用的参数之前执行 验证

我们称之为服务层的部分只是一个普通的 bean,没有额外的行为。

您可以显式地使用 SpringValidator

@Bean
public SpringValidator<ClassToValidate> springValidator() {
        SpringValidator<ClassToValidate> springValidator = new SpringValidator<>();
        springValidator.setValidator(localValidatorFactoryBean());
        return springValidator;
    }

然后直接调用 validate() 方法

    try {
     springValidator.validate(ObjectToValidate);
    } catch (Exception e) {
      return log.error("error validation", e);
    }
英文:

The reason it works for @Controller class handler methods is that Spring uses a special HandlerMethodArgumentResolver called ModelAttributeMethodProcessor which performs validation before resolving the argument to use in your handler method.

The service layer, as we call it, is just a plain bean with no additional behavior.

You can explicitly use SpringValidator

@Bean
public SpringValidator&lt;ClassToValidate&gt; springValidator() {
        SpringValidator&lt;ActivityCsvDTO&gt; springValidator = new SpringValidator&lt;&gt;();
        springValidator.setValidator(localValidatorFactoryBean());
        return springValidator;
    }

and directelly call validate() method

    try {
     springValidator.validate(ObjectToValidate);
    } catch (Exception e) {
      return log.error(&quot;error validation&quot;, e);
    }

huangapple
  • 本文由 发表于 2020年8月27日 23:04:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63618897.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定