0

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. Is there standard way to write a global handler , or a processor for that?

Would appreciate any help

matrixguy
  • 286
  • 1
  • 6
  • 30
  • this is usually done using spring aspects, https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#aop – Toerktumlare Apr 09 '21 at 10:15

1 Answers1

0

AspectJ can directly match parameter annotations, but not bind them to advice method values like class or method annotations. 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:

kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • Thanks for all the resources. Can this also be used to modify the annotated request parameter? – matrixguy Apr 13 '21 at 14:48
  • Yes, you can do that with AOP. But that is a different question. You should be able to find the answer here via search. If not, which would surprise me, feel free to ask a new question. But before you do that, please accept and upvote my answer if you agree that I answered your original question. – kriegaex Apr 13 '21 at 18:17
  • I think you forgot to accept + upvote my answer. – kriegaex Apr 23 '21 at 04:39