Implementing Custom Annotation in SpringBoot (II)

Shubhankar Kotnala
3 min readJun 18, 2023

This article is an extension of a previous article where we understand the basics of Custom Annotations in Spring Boot, if you have’t checked it out, it is highly recommended to read the first part.

What more can we do with annotations?

Now that we know how to implement a custom annotation and accept parameters, we now the basics of custom annotations and can fiddle around more to explore more on this.

However remember that we said “joinPoint will hold the context of the current method that is to be executed”. Does that mean we can also get method data out of it? YES! We can get method related data in our aspect method.

Let’s see how we can extract the method parameters from joinPoint.
For this we create a new API just for this purpose accepting a body and a parameter.

@PostMapping
@RequestLogger
public String testParam(@RequestBody String body, @RequestParam String param) {
log.info("Inside API method with body : {} param: {}", body, param);
return body;
}

Now we can update our aspect to extract method parameters.

log.info("{} : Request received", request.getRequestURI());

//Extracting method signature
CodeSignature codeSignature = (CodeSignature) joinPoint.getSignature();
//Extracting method arguments name
String[] parameterNames = codeSignature.getParameterNames();
//Extracting method arguments value
Object[] args = joinPoint.getArgs();
Map<String, Object> fieldToValue = new HashMap<>();
for (int i = 0; i < parameterNames.length; i++) {
fieldToValue.put(parameterNames[i], args[i]);
}
fieldToValue.forEach((key, value) -> log.info("Arg : {} = {}", key, value));
Object obj = joinPoint.proceed();
log.info("{} : Request finished", request.getRequestURI());
return obj;

We have extracted the method signature to get the parameter names, and also we have extracted the actual argument values of the method.
Notice we receive Object[] for getArgs() , which we can cast to object as and when required.

On hitting the API again let’s observe the different in the logging for both the APIs.

While for the initial API we don’t have any different in the API since it does not have any parameters, but we do see a difference in logs for the POST API.

Passing request parameter param as Test and request body as Hello we see the expected response, but what about the logs?

We see that there were two arguments to the method, and their value is also present indicating we have successfully extracted method arguments within our annotation logic.

Conclusion

We have seen how can we,

  • Implement a custom Spring AOP Annotation
  • Accept Parameters for annotation
  • Extract Method information within the Aspect using JoinPoint

With this you are now ready to explore and create your own custom annotations.

Custom annotations can be a powerful tool for adding metadata to your code. By using custom annotation, you can make your code more organised and easier to understand. Make sure to give it a try :D

I hope this article helped you understand Spring AOP Annotations a little better. If you liked the work, follow me on Medium for more such articles, and Github for more such repositories.

Resource

All of the code used in this article can be found in this Github Repo

--

--