Hibernate Validator. How to work with @Valid annotation?

前端 未结 2 1203
梦如初夏
梦如初夏 2021-02-15 17:12

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

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

I created a te

2条回答
  •  你的背包
    2021-02-15 17:56

    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.

    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) {
            // ....
        }
    }
    

    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) {
            // ...
        }
    }
    

    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.

    Note: Bean Validation providers are free to implement this proposal as a specific extension. Such specific extension could for example be accessed via the use of the Validator.unwrap method.

    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:

    • application frameworks like
      • JSR-299
    • component frameworks like Enterprise Java Beans
    • aspect based frameworks

    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.

提交回复
热议问题