JSR 303. Validate method parameter and throw exception

前端 未结 1 1811
闹比i
闹比i 2021-01-16 06:15

How can I use JSR-303 validate method parameters and throw exception if parameter is not valid?

For example like this: public void createUser(@ValidOrThrowExc

相关标签:
1条回答
  • 2021-01-16 06:51

    If you can upgrade to Hibernate Validator 4.2.0 or above, you can use its method validation feature, which provides support for the validation of method parameters and return values.

    The validation engine can be invoked automatically upon the invocation of constrained methods using means such dynamic proxies, AOP, interceptors etc. Depending on the framework you work with, you can e.g. use one of the following:

    • When working with Spring 3.1 or later: MethodValidationInterceptor
    • When working with CDI: Seam Validation (disclaimer: I'm the author of this)
    • When working with Guice: I've added an example for this on GitHub

    Note that as of Bean Validation 1.1 (to be finalized soon) and its reference implementation Hibernate Validator 5, method validation will be part of the standardized Bean Validation API.

    0 讨论(0)
提交回复
热议问题