Hibernate验证器。如何使用@Valid注释? [英] Hibernate Validator. How to work with @Valid annotation?

查看:213
本文介绍了Hibernate验证器。如何使用@Valid注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@Valid 注释放在方法参数级别时有什么用途?

What purpose of @Valid annotation when putting it on method parameter level?

public void (@Valid Person p) { ... }

我创建了一个测试,并传递给了这个方法非有效对象,但没有任何反应。

我希望得到一个例外。

I created a test, and passed to this method non-valid object, but nothing happens.
I expect to get an exception.

推荐答案

对象上的@ 有效注释表示对象验证框架来处理带注释的对象。当在方法的参数上使用时,这称为 方法级别验证 。请注意,方法级别验证是 核心规范的一部分,实际上只有在将Bean Validation集成到容器类型框架(JSF,CDI,Java EE)时才支持)。当Bean Validation集成到这样的支持容器中时,会发生的事情是,当在bean上调用生命周期方法时,容器会检测方法参数上的JSR 303注释并触发关联bean的验证。

The @Valid annotation on an object is an indication to the validation framework to process the annotated object. When used on a parameter of a method this is referred to as method level validation. Note that method level validation is not part of the core specification, and in fact is only supported when Bean Validation is integrated into a container type framework (JSF, CDI, Java EE). When Bean Validation is integrated into such a supporting container, what happens is that, as lifecycle methods are called on the bean, the container detects JSR 303 annotations on the methods parameters and triggers validation of the associated bean.

例如,如果您在JAX-RS资源类中有以下方法定义:

So for example, if you had the following method definition in a JAX-RS resource class:

@Path("/example")
public class MyExampleResourceImpl {

    @POST
    @Path("/")
    public Response postExample(@Valid final Example example) {
        // ....
    }
}

当响应JAX-RS容器处理的请求调用 postExample 方法时,示例 bean将被验证。将此行为与运行独立Java SE应用程序时会发生的情况进行对比:

When the postExample method is called in response to a request being handled by the JAX-RS container, the example bean will be validated. Contrast this behavior with what would happen if you were running a standalone Java SE app:

public class MyMainClass {
    public static void main(final String[] args) {
        final MyMainClass clazz = new MyMainClass();
        clazz.echo(new Example());
    }

    public Example echo(@Valid final Example example) {
        // ...
    }
}

在这种情况下,运行程序将 触发验证示例参数,即使您包含了所有JSR 303运行时JAR。这是因为没有可用的容器实现方法级别验证。 Bean验证规范附录C中详细描述了所有这些。为了您的利益,我在下面引用了一些内容:

In this case, running the program will not trigger validation of the Example parameter, even if you included all the JSR 303 runtime JARs. This is because there is no container available that implements method level validation. The Bean Validation Specification describes all this in some detail in Appendix C. I've quoted some of it below for your benefit:


方法级验证的提案

Proposal for method-level validation

此提议尚未集成到核心规范
中,并且不属于它。它仍然留在这里用于考古目的,并且将认真考虑
,以便将来修订这个
规范。这个提议可能与
的其他规范工件有点不同步。

This proposition has not been integrated into the core specification and is not part of it. It remains here for archaeological purposes and will be seriously considered for a future revision of this specification. This proposal is likely to be a bit out of sync with the rest of the specification artifacts.

注意: Bean验证提供程序可以自由实现此提案为
a特定扩展名。例如,可以通过使用Validator.unwrap方法访问
这样的特定扩展。

一个流行的需求是提供一种方法,参数级验证
机制重用规范的约束描述。这个
的API集应该被拦截器框架使用,例如:

A popular demand was to provide a method and parameter level validation mechanism reusing the constraint descriptions of the specification. This set of APIs is meant to be used by interceptor frameworks such as:


  • 应用程序框架,如

    • JSR-299

    这些框架可以将验证API调用到
    验证参数列表或返回值a方法
    调用此方法时。更确切地说,验证发生在
    方法调用周围。 Bean Validation API的这个扩展允许
    重用核心引擎以及约束定义和
    声明用于此类方法级别验证。

    These frameworks can call the validation APIs to validate either the parameter list or the returned value of a method when such method is called. More precisely, validation occurs around a method invocation. This extension of the Bean Validation API allows to reuse the core engine as well as the constraint definition and declaration for such method level validations.

    这篇关于Hibernate验证器。如何使用@Valid注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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