简体   繁体   English

RequestParam 和 PathVariable 上的自定义注释

[英]custom annotation on RequestParam and PathVariable

I have created a custom annotation我创建了一个自定义注释

annotation class UserControl(
    val userIdentifier: String
)

I wan to apply this annotation on query parameters, and path variables in different controllers.我想将此注释应用于查询参数和不同控制器中的路径变量。

    fun userWithMobile(
        @UserControl("PhoneNumber")
        @RequestParam mobile: String
    ): RegisteredUser {
        return userManager.getUserWithPhoneNumber(mobile))
    }

How can i check if the query parameters have the UserControl annotation or not, and do some processing on that.我如何检查查询参数是否具有 UserControl 注释,并对其进行一些处理。 Is there standard way to write a global handler, or a processor for that?是否有标准的方法来编写全局处理程序,或者为此编写一个处理器?

Would appreciate any help将不胜感激任何帮助

AspectJ can directly match parameter annotations, but not bind them to advice method values like class or method annotations. AspectJ 可以直接匹配参数注释,但不能将它们绑定到通知方法值,如 class 或方法注释。 So if you only want to match them, a simple pointcut is enough.所以如果你只想匹配它们,一个简单的切入点就足够了。 If you want to access the annotations and maybe their parameter values, you need a little bit of reflection magic.如果你想访问注解和参数值,你需要一点反射魔法。 I have answered related questions many times already, which is why I am going to close this one as a duplicate.我已经多次回答了相关问题,这就是为什么我要关闭这个作为重复的问题。 But first, here are the resources you want to read.但首先,这是您要阅读的资源。 They all related to your question, showing examples of how to handle different specific situations:它们都与您的问题有关,展示了如何处理不同特定情况的示例:

Basically, the pointcut you want the following or some variation of it:基本上,您想要以下切入点或它的一些变体:

execution(* *(.., @my.package.UserControl (*), ..))

The naive, less efficient approach without matching the parameter in the pointcut, using only relfection:不匹配切入点中的参数的天真、效率较低的方法,仅使用 relfection:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM