[英]Java execute code on parameter annotation
I am very new to Java annotations. 我对Java注释非常陌生。 But, how I understand is that they can't run any code them selves.
但是,据我了解,他们无法运行自己的任何代码。
I am trying to make a validation package and would like to use the @valid
parameter annotation to validate an object. 我正在尝试制作一个验证包,并想使用
@valid
参数注释来验证对象。
Example: 例:
public Response login(@valid LoginRequest request) {
return ...
}
I would like it to work this way, it is the same way as in spring validating. 我希望它以这种方式工作,这与春季验证中的方式相同。
So my question is: How can I execute code on the @valid
annotation to validate an object? 所以我的问题是:如何在
@valid
批注上执行代码以验证对象? I don't mind if the solution would only work with spring. 我不介意该解决方案是否仅适用于spring。
The reason I want to make my own validation package is because I want to have a more "laravel like" validation package. 我要制作自己的验证包的原因是因为我想拥有一个更像“ laravel like”的验证包。
It was pretty easy. 这很容易。 I just had to create an interceptor and check in the
preHandle
if there was an handler parameter that had the @valid
annotation. 我只需要创建一个拦截器并检查
preHandle
是否存在带有@valid
批注的处理程序参数。
If @valid
was present I could get the object and validate it, when failed just throw an exception and handle the exception with the exception handler. 如果存在
@valid
则可以获取对象并对其进行验证,如果失败,则仅引发异常并使用异常处理程序处理该异常。
I didn't know that I could access controller parameters from an interceptor... 我不知道我可以从拦截器访问控制器参数...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.