DEV Community

Tamilselvan K
Tamilselvan K

Posted on • Edited on

Day-100,101 19-Essential Spring Boot Annotations

1.@SpringBootApplication

  • Marks the main class of a Spring Boot application.
  • Enables auto-configuration,component scanning, and configuration setup.
@SpringBootApplication
public class MyApp {
    public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);
    }
}
Enter fullscreen mode Exit fullscreen mode

2.@RestController

  • Marks a class as a REST API controller.
  • Combines @Controller + @ResponseBody, so responses are returned as JSON.
@RestController
public class HelloController {
    @GetMapping("/hello")
    public String hello() {
        return "Hello Spring Boot!";
    }
}
Enter fullscreen mode Exit fullscreen mode

3.@Controller

  • Marks a class as a Spring MVC controller.
  • Used for serving web pages or templates (like Thymeleaf).
@Controller
public class PageController {
    @GetMapping("/home")
    public String homePage() {
        return "home"; // returns home.html
    }
}
Enter fullscreen mode Exit fullscreen mode

4.@GetMapping

  • Maps HTTP GET requests to a controller method.
@GetMapping("/users")
public List<User> getUsers() { ... }
Enter fullscreen mode Exit fullscreen mode

5.@PostMapping

  • Maps HTTP POST requests to a method.
@PostMapping("/users")
public User createUser(@RequestBody User user) { ... }
Enter fullscreen mode Exit fullscreen mode

6.@PutMapping

  • Maps HTTP PUT requests to a method for updating resources.
@PutMapping("/users/{id}")
public User updateUser(@PathVariable Long id, @RequestBody User user) { ... }
Enter fullscreen mode Exit fullscreen mode

7.@DeleteMapping

  • Maps HTTP DELETE requests to a method for deleting resources.
@DeleteMapping("/users/{id}")
public void deleteUser(@PathVariable Long id) { ... }
Enter fullscreen mode Exit fullscreen mode

8.@Autowired

  • Automatically injects Spring-managed beans.
@Autowired
private UserService userService;
Enter fullscreen mode Exit fullscreen mode

9.@Service

  • Marks a class as a service layer bean containing business logic.
@Service
public class UserService { ... }
Enter fullscreen mode Exit fullscreen mode

10.@Repository

  • Marks a class as a data access layer (DAO).
  • Supports exception translation for database operations.
@Repository
public interface UserRepository extends JpaRepository<User, Long> { }
Enter fullscreen mode Exit fullscreen mode

11.@Component

  • Generic annotation for any Spring-managed bean.
@Component
public class Utility { ... }
Enter fullscreen mode Exit fullscreen mode

12.@Entity

  • Marks a class as a JPA entity mapping to a database table.
@Entity
public class User { ... }
Enter fullscreen mode Exit fullscreen mode

13.@ Id

  • Marks the primary key in a JPA entity.
@Id
private Long id;
Enter fullscreen mode Exit fullscreen mode

14.@GeneratedValue

  • Configures automatic generation of primary key values (e.g., auto-increment).
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
Enter fullscreen mode Exit fullscreen mode

15.@Configuration

  • Marks a class as a source of bean definitions.
@Configuration
public class AppConfig { ... }
Enter fullscreen mode Exit fullscreen mode

16.@ Bean

  • Declares a bean inside a configuration class; managed by Spring.
@Bean
public RestTemplate restTemplate() {
    return new RestTemplate();
}
Enter fullscreen mode Exit fullscreen mode

17.@RequestBody

  • Binds HTTP request body (JSON) to a Java object.
@PostMapping("/users")
public User createUser(@RequestBody User user) { ... }
Enter fullscreen mode Exit fullscreen mode

18.@PathVariable

  • Binds a URI path variable to a method parameter.
@GetMapping("/users/{id}")
public User getUser(@PathVariable Long id) { ... }
Enter fullscreen mode Exit fullscreen mode

19.@RequestParam

  • Binds query parameters or form data to a method parameter.
@GetMapping("/users")
public List<User> getUsers(@RequestParam int page, @RequestParam int size) { ... }
Enter fullscreen mode Exit fullscreen mode

Conclusion

For freshers, mastering these 19 annotations is enough to handle most Spring Boot interview questions. They cover:

  • Application setup
  • REST controllers
  • Dependency injection
  • Service & repository layers
  • JPA / database mapping
  • Request handling

Top comments (0)