Spring @Validated 在服务层 [英] Spring @Validated in service layer

查看:34
本文介绍了Spring @Validated 在服务层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿嘿

我想在执行如下方法之前使用 @Validated(group=Foo.class) 批注来验证参数:

I want to use the @Validated(group=Foo.class) annotation to validate an argument before executing a method like following:

public void doFoo(Foo @Validated(groups=Foo.class) foo){}

当我将此方法放在我的 Spring 应用程序的控制器中时,@Validated 被执行并在 Foo 对象无效时抛出错误.但是,如果我将相同的内容放在应用程序服务层的方法中,则不会执行验证,即使 Foo 对象无效,该方法也会运行.

When i put this method in the Controller of my Spring application, the @Validated is executed and throws an error when the Foo object is not valid. However if I put the same thing in a method in the Service layer of my application, the validation is not executed and the method just runs even when the Foo object isn't valid.

你不能在服务层使用@Validated注解吗?还是我必须做一些额外的配置才能让它工作?

Can't you use the @Validated annotation in the service layer ? Or do I have to do configure something extra to make it work ?

更新:

我在 service.xml 中添加了以下两个 bean:

I have added the following two beans to my service.xml:

<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
<bean class="org.springframework.validation.beanvalidation.MethodValidationPostProcessor"/>

并将 @Validate 替换为 @Null,如下所示:

and replaced the @Validate with @Null like so:

public void doFoo(Foo @Null(groups=Foo.class) foo){}

我知道这是一个非常愚蠢的注释,但我想检查一下,如果我现在调用该方法并传递 null,它会抛出一个违规异常.那么为什么它执行 @Null 注释而不是 @Validate 注释?我知道一个来自 javax.validation 另一个来自 Spring 但我认为这与它没有任何关系?

I know it is a pretty silly annotation to do but I wanted to check that if I call the method now and passing null it would throw an violation exception which it does. So why does it execute the @Null annotation and not the @Validate annotation ? I know one is from javax.validation and the other is from Spring but I do not think that has anything to do with it ?

推荐答案

在 Spring MVC 堆栈的眼中,没有服务层这样的东西.它适用于 @Controller 类处理程序方法的原因是 Spring 使用一个特殊的 HandlerMethodArgumentResolver 称为 ModelAttributeMethodProcessor 它在解析要使用的参数之前执行验证您的处理程序方法.

In the eyes of a Spring MVC stack, there is no such thing as a service layer. 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.

服务层,正如我们所说的,只是一个普通的 bean,没有从 MVC (DispatcherServlet) 堆栈中添加额外的行为.因此,您不能期望来自 Spring 的任何验证.您需要自己动手,可能使用 AOP.

The service layer, as we call it, is just a plain bean with no additional behavior added to it from the MVC (DispatcherServlet) stack. As such you cannot expect any validation from Spring. You need to roll your own, probably with AOP.

MethodValidationPostProcessor,看看javadoc

With MethodValidationPostProcessor, take a look at the javadoc

适用的方法在其上有 JSR-303 约束注释参数和/或它们的返回值(在后一种情况下指定在方法级别,通常作为内联注释).

Applicable methods have JSR-303 constraint annotations on their parameters and/or on their return value (in the latter case specified at the method level, typically as inline annotation).

验证组可以通过 Spring 的 Validated 指定包含目标类的类型级别的注释,应用到该类的所有公共服务方法.默认情况下,JSR-303 将仅针对其默认组进行验证.

Validation groups can be specified through Spring's Validated annotation at the type level of the containing target class, applying to all public service methods of that class. By default, JSR-303 will validate against its default group only.

@Validated 注释仅用于指定验证组,它本身不强制任何验证.您需要使用 javax.validation 注释之一,例如 @Null@Valid.请记住,您可以在方法参数上使用任意数量的注释.

The @Validated annotation is only used to specify a validation group, it doesn't itself force any validation. You need to use one of the javax.validation annotations like @Null or @Valid. Remember that you can use as many annotations as you would like on a method parameter.

这篇关于Spring @Validated 在服务层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆