DEV Community

Likitha Chendrimada Suguna
Likitha Chendrimada Suguna

Posted on

7 Spring Boot Annotations Every Beginner Should Know

When I first started learning Spring Boot, I was overwhelmed by annotations.

Every file seemed to have symbols starting with @.

@SpringBootApplication

@RestController

@Service

@Autowired

At first, I treated them like magic spells. I copied them from tutorials and hoped everything would work.

Eventually, I realized that understanding a few key annotations made Spring Boot much less intimidating.

If you're just starting your Spring Boot journey, these are the annotations I believe you should understand first.


1. @SpringBootApplication

This is usually the first annotation you'll see in a Spring Boot project.

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
Enter fullscreen mode Exit fullscreen mode

Think of it as the starting point of your application.

When Spring Boot sees this annotation, it knows:

  • Where the application begins
  • Which components need to be scanned
  • Which configurations should be loaded

Without it, your Spring Boot application won't know how to start properly.


2. @RestController

If you're building REST APIs, you'll use this annotation frequently.

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        return "Hello, World!";
    }
}
Enter fullscreen mode Exit fullscreen mode

A class marked with @RestController tells Spring:

"The methods inside this class will handle HTTP requests and return data."

Instead of returning web pages, it usually returns:

  • JSON
  • Strings
  • Objects
  • API responses

Whenever I create a new API endpoint, this is one of the first annotations I add.


3. @GetMapping

This annotation is used when you want to handle GET requests.

@GetMapping("/students")
public String getStudents() {
    return "List of students";
}
Enter fullscreen mode Exit fullscreen mode

A GET request is typically used to retrieve information.

Examples:

  • Get user details
  • Fetch products
  • View student records

Whenever a client requests data from the server, @GetMapping often comes into play.


4. @PostMapping

While @GetMapping retrieves data, @PostMapping is commonly used to create or submit data.

@PostMapping("/students")
public String addStudent() {
    return "Student added";
}
Enter fullscreen mode Exit fullscreen mode

Examples:

  • Registering a user
  • Adding a product
  • Creating a new record

One easy way to remember it:

  • GET → Read data
  • POST → Send data

5. @Service

As projects grow, putting all logic inside controllers becomes messy.

That's where @Service helps.

@Service
public class StudentService {

    public String getStudent() {
        return "Student Data";
    }
}
Enter fullscreen mode Exit fullscreen mode

A service class contains the application's business logic.

For example:

  • Calculations
  • Validation
  • Processing requests
  • Application rules

I like to think of it as the "brain" of the application.

Controllers receive requests.

Services decide what should happen.


6. @Repository

The repository layer is responsible for interacting with the database.

@Repository
public interface StudentRepository extends JpaRepository<Student, Long> {
}
Enter fullscreen mode Exit fullscreen mode

A repository helps perform operations like:

  • Insert data
  • Update data
  • Delete records
  • Retrieve information

Instead of writing complex database code manually, Spring Data JPA makes many operations available automatically.

This annotation tells Spring:

"This component works with data storage."


7. @Autowired

When I first encountered Dependency Injection, I found it confusing.

Then I discovered @Autowired.

@Autowired
private StudentService studentService;
Enter fullscreen mode Exit fullscreen mode

This annotation allows Spring to automatically provide an object when needed.

Instead of creating objects manually using:

StudentService service = new StudentService();
Enter fullscreen mode Exit fullscreen mode

Spring creates and manages them for you.

This reduces boilerplate code and makes applications easier to maintain.

Although constructor injection is often preferred in modern Spring Boot projects, understanding @Autowired is important because you'll see it in many existing applications.


Final Thoughts

When I began learning Spring Boot, annotations felt mysterious.

But after understanding these seven annotations, the framework started making much more sense.

If you're a beginner, focus on mastering these first:

@SpringBootApplication – Starts the application

@RestController – Handles API requests

@GetMapping – Retrieves data

@PostMapping – Sends data

@Service – Contains business logic

@Repository – Communicates with the database

@Autowired – Injects dependencies

Spring Boot has many more annotations, but these are the ones I encountered repeatedly while building my first projects.

And honestly, understanding them made the rest of Spring Boot feel a lot less magical and a lot more logical.


*Which Spring Boot annotation confused you the most when you started learning? Let me know in the comments! *

Top comments (0)