Sitemap
Ece Aslan

Ece Aslan

Some Annotations in Java

4 min readApr 15, 2026

--

And some from my Spring Boot project that I’ve used and what they do.

What do annotations do?

  • They mark the classes, methods or variables with metadata (information).
  • They provide additional information basically for the usual use.

Standard/Built-in annotation types are 5; Marker, Single value, Full, Type and Repeating.

  1. Marker Annotations
    - Just marks it, has no elements or values.
  2. Single Value Annotations
    - Contains only one element. Does not need element name when entering value.
  3. Full Annotations
    - Multiple elements and their values as key-value pairs. If no default, elements must be defined.
  4. Type Annotations
    - Used for type checking. Declared with @TargetElementType.TYPE_USE).
  5. Repeating Annotations
    - Allows the same annotation to be applied multiple times on a single element. Declared using @Repeatable.
  • Standard annotations come built-in with libraries itself.

But we can also create our own annotations!! Here is how:

  • Rule 1: Define where you annotation can be used.
@Target(ElementType.METHOD) //Here we say it can be used only with methods.
  • Rule 2: Define how long it will stay alive.
@Retention(RetentionPolicy.RUNTIME) 
//Here we said our annotation will stay alive only in runtime.
  • Rule 3: Design your annotation’s purpose.
public @interface workItOnceMore {
int sayi() default 1; //If not there is no parameter sent, default is 1.
}

And voila! Your annotation is ready to use!

Get Ece Aslan’s stories in your inbox

Join Medium for free to get updates from this writer.

— — — — — — — — — — — — —

What did I use with my Spring Boot project? Let’s see some of them and their purposes:

i. Spring Framework / Spring Boot: Architecture, routes and dependencies are set with these annotations.

  • @Configuration: Says that that’s marked is a setting/configuration file.
  • @Bean: Adds a permanent object to Spring’s general memory, so everyone can use it as they like.
  • @Service: Means that this is where the actual code and calculations are made. Business logic is here and happens.
  • @Repository: Marks the classes that do CRUD operations, speaks directly to the database.
  • @RestController: Tells that the class that’s been marked with this annotation gets the HTTP requests and returns the answers with JSON format, not HTML page.
  • @RequestMapping: Gives a general URL to a class or a method.
  • @GetMapping: When user only wants to get/read data from inside, this annotation works.
  • @PostMapping: When user wants to send new data or save it to the database, this annotation works.
  • @ControllerAdvice: Collects all the errors across the app and lets us control them from one place.
  • @ExceptionHandler: Used in @ControllerAdvice, helps us create read-friendly error messages for users. Or us when developing/testing.
  • @SpringBootApplication: A combo package annotation that works 3 annotations all together. Which are @Configuration, @ComponentScan and @EnableAutoConfiguration. When you run the app, this annotation, scans all the files, connects the between relations and says, “We’re ready to go, let’s get this app working!’’.

ii. Lombok: Prevents boilerplate code.

  • @Getter and @Setter: Automatically writes the get and set methods for the variables. Developer does not see them. They’re written in the background.
  • @Data: Includes @Getter, @Setter along with other constructors like @ToString, @EqualsAndHashCode and etc. But if you’re not using other constructor, suggestion is towards to use only @Getter and @Setter.
  • @Builder: When creating an object, it saves us the trouble of creating long constructor. We just go like, .name(…).age(…).build(); and there you go, your one-line constructor.
  • @Slf4j: Adds a log object to the class in the background, helps with the error messages to be more read-friendly.

iii. JPA/Hibernate: Helps with data permanency, relational databases tables.

  • @Entity: Indicates the class it’s been marked with, represents a table in the database.
  • @Table: Not necessarily used but when we want a different table name in the database, we indicate it with this annotation. If not used, class name will be used also as the table name.
  • @Id: Marks the variable that’s going to be used as a primary key in the table.
  • @GeneratedValue: Means that the ID will not be manually created. It will be generated automatically by the database.
  • @Column: Specifies the column’s attributes like is it unique or can it be empty and some others.

iv. MapStruct: Simplifies converting objects from entity to DTO, generates boilerplate mapping code during complications.

  • @Mapper: Converts objects from one to another. Like, Entity to DTO.

v. Java Core: Basic Java foundation with fundamental concepts.

  • @Override: Pure Java annotation. Tells the compiler that the method is will be overwritten from an upper class or interface.

— — — — — — — — — — — — — — —

@ArgsConstructor Annotations

From Lombok library, we have three types of ArgsConstructor annotations, that saves us from writing long constructors when creating objects!

  1. @NoArgsConstructor: Creates an empty constructor that has no parameters. When using JPA/Hibernate, this is a must. Because Hibernate would want to create an empty object/instance when pulling a row of data. After that, it sets data in its attributes. Also, when converting JSON (Jackson lib) need this annotation too.
  2. @AllArgsConstructor: Creates a constructor with all attributes of the class in order they have been declared. Used with DTO classes, or tests. But if there are too many attributes, it’s more likely to get an error so, we use @Builder instead.
  3. @RequiredArgsConstructor: Creates a constructor with the only required parameters. The attributes with the @final keyword cannot be changed after created and they must be given a value upon creating. @NonNull attributes also cannot be null or left empty. This is used when doing Dependency Injection. This constructor only takes the attributes with @final and @NonNull.

— — — — — — — — — — — — — —

--

--