Skip to main content

Command Palette

Search for a command to run...

Spring Boot Annotations Explained Simply: From Basics to REST APIs

Updated
3 min read
Spring Boot Annotations Explained Simply: From Basics to REST APIs
N
Java & Spring Boot learner | Writing beginner-friendly technical articles | Exploring backend development

When I first started learning Spring Boot, annotations felt like magic. Instead of writing long XML configurations, everything was simplified into clean, readable code.

But behind that simplicity lies powerful concepts like Dependency Injection, IoC (Inversion of Control), and Bean Lifecycle Management.

In this blog, I will break down the most important Spring Boot annotations, especially focusing on the Controller layer, in a way that actually makes sense.

What are Annotations in Spring Boot?

Annotations in Spring Boot are metadata that provide instructions to the Spring framework at runtime.

They help:

  • Reduce boilerplate code

  • Configure application behavior

  • Manage dependencies automatically

Think of annotations as shortcuts that replace complex configurations.

Core Spring Boot Annotations

@SpringBootApplication

This is the entry point of any Spring Boot application.

@SpringBootApplication
public class MyApplication {
    public static void main(String[] args){
    
         SpringApplication.run(MyApplication.class, args);
   }
}

Combines:

  • @Configuration

  • @EnableAutoConfiguration

  • @ComponentScan

@Component

Marks a class as a Spring-managed bean.

@Component
public class MyService{
}

@Autowired

Used for dependency injection.

@Autowired
private MyService myService;

Spring automatically injects the required object.

Controller Layer Annotations

This is where your backend communicates with the outside world (frontend, APIs, etc.).

@RestController

Combination of:

  • @Controller

  • @ResponseBody

@RestController
public class MyController{
}

Used for building REST APIs (returns JSON directly).

@RequestMapping

Defines the base URL for endpoints.

@RequestMapping("/api")
public class MyController{
}

@GetMapping

Handles HTTP GET requests.

@GetMapping("/hello")
public String sayHello(){
  return "Hello World";
}

@PostMapping

Handles HTTP POST requests.

@PostMapping("/save")
public String saveData(){
   return "Data Saved";
}

@PutMapping and @DeleteMapping

Used for update and delete operations.

@PutMapping("/update")
@DeleteMapping("/delete")

@PathVariable

Extracts values from URL.

@GetMapping("/user/{id}")
public String getUser(@PathVariable int id){
    return "User ID:" + id;
}

@RequestParam

Used for query parameters.

@GetMapping("/search")
public String search(@RequestParam String name){
 return name;
}

@RequestBody

Used to accept JSON data from request.

@PostMapping("/user")
public String addUser(@RequestBody User user){
    return "User Added";
}

Why Annotations Matter?

Without Annotations:

  • Heavy XML

  • Hard to maintain

  • More boilerplate

With Annotations:

  • Clean code

  • Faster development

  • Better readability

Real-World Flow Example

@RestController
@RequestMapping("/api")
public class UserController{

    @GetMapping("/users/{id}")
     public String getUser(@PathVariable int id){
         return "User ID:" + id;
     }
    @PostMapping("/users")
    public String createUser(@RequestBody String user){
         return "User Created";
    }
}

This small code handles:

  • Routing

  • Request parsing

  • Response handling

Best Practices

  • Use @RestController for APIs instead of @Controller

  • Keep controllers thin (move logic to service layer)

  • Use meaningful endpoint names

  • Avoid field injection -> prefer constructor injection

My Learning Insight

Initially, I used annotations without understanding them.

But once I connected them with:

  • IoC

  • Dependency Injection

  • Spring Container

Everything started making sense.

Now, annotations don't feel like magic-they feel like tools I control.

Conclusion

Spring Boot annotations are the backbone of modern Java backend development.

They:

  • Simplify configuration

  • Improve readability

  • Speed up development

If you truly understand annotations, you're already halfway to mastering Spring Boot.