Custom Annotation & Spring Boot

Custom Annotation & Spring Boot


0






Rahul Kumar (@rahul)

Custom annotations in Spring Boot can be used to create reusable annotations, validation, logging etc. In this blog, we'll see how can we use custom annotation in spring boot and it's benefits.

What is an annotation?

An annotation is a note that includes a comment or explanation associated with a particular point in a document or other piece of information.

What is Java Annotation?

In Java, annotations are metadata tags used to provide additional information to the compiler and JVM. Annotations are not part of the program itself and do not affect the execution of the compiled program. Annotations can be used to provide information about the purpose of a class, method, or variable, or to indicate that a class or method is deprecated. Annotations can also be used to control the behaviour of the compiler and JVM. 

Here is an example of an annotation that is used to provide information about the purpose of a class

      @Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Service {
    String name();
    String description();
}
    

Spring Boot & Annotations 

Spring Boot heavily uses annotation for various purposes. @SpringBootApplication annotation is used to mark a class as the main class of the spring application. @RestController annotation is used to mark a class as a controller and acts as an interface for external users/environment. Similarly, there are multiple other annotations provided by spring boot and used for different purposes.

Custom Annotation & Spring Boot

We can create custom annotations in Spring Boot to create a reusable and decoupled functionality, for example logging arguments of a method, validating user authentication, etc.

Logging @Annotation
      @Service
public class DsaByteService{
    int count;
    
    DsaByteService(){
        count = 0;
    }
    
    
    @ArgLogger("val")
    public int getCounter(int val){
        count = count+val;
        return count;
    }
}
    

Here we have used @ArgLogger annotation and passed val as argument. This annotation tells that whenever this method gets called, log the val argument of the getCounter method and increment the counter as well. Although there is no such annotation exists, I have used it here just for demonstration purposes. 

Creating a custom annotation

Let's create a custom annotation extending the functionality of @GetMapping and @PostMapping annotations. 

      @Retention(RUNTIME)
@Target(METHOD)
@RequestMapping(produces = { MediaType.APPLICATION_JSON_VALUE }, method = RequestMethod.GET)
public @interface JsonGetMapping {
	@AliasFor(annotation = RequestMapping.class, attribute = "value")
	String[] value() default {};

	@AliasFor(annotation = RequestMapping.class, attribute = "params")
	String[] params() default {};

	@AliasFor(annotation = RequestMapping.class, attribute = "headers")
	String[] headers() default {};
}
    

Here we have combined the functionality of @RequestMapping annotation and created a @JsonGetMapping annotation. This annotation always makes sure that the media type is always application/json and the method is GET method.

Here is how we can use it...

      public class DsaByteTestController{
    @JsonGetMapping("/hello")
    public void String hello(){
        return "👋 @dsabyte";
    }
}
    

Add a thoughtful comment...

✨ Explore more tech insights and coding wonders with @dsabyte! Your journey in innovation has just begun. Keep learning, keep sharing, and let's continue to code a brighter future together. Happy exploring! 🚀❤️

  • #xmlnode
  • #spring
  • #spring-boot
  • #annotations
  • #annotate
  • #jvm
  • #jvm-bytecode
  • #jvm-languages

Subscribe to our newsletter.

Join the "News Later" community by entering your email. It's quick, it's easy, and it's your key to unlocking future tech revelations.

Weekly Updates

Every week, we curate and deliver a collection of articles, blogs and chapters directly to your inbox. Stay informed, stay inspired, and stay ahead in the fast-paced world of technology.

No spam

Rest assured, we won't clutter your inbox with unnecessary emails. No spam, only meaningful insights, and valuable content designed to elevate your tech experience.

© 2024 @dsabyte. All rights reserved