Spring Boot Annotations: Powering Mid-Scale Apps

Table of contents
Building a mid-scale Spring Boot app—like an Employee Management System—means organizing code into layers: Configuration, Entity, Controller, Service, and Repository. Annotations are the glue that makes these layers work together. This post shares the essential annotations for a mid-scale project, with clear examples to help you create robust apps. Perfect for developers stepping up to bigger challenges!
1. Configuration Layer
Sets up your app’s foundation.
@SpringBootApplicationKicks off your app with auto-configuration.
@SpringBootApplication
public class EmployeeApp {
public static void main(String[] args) {
SpringApplication.run(EmployeeApp.class, args);
}
}
@ConfigurationDefines custom settings or beans.
@Configuration
public class AppConfig {
@Bean
public PasswordEncoder encoder() {
return new BCryptPasswordEncoder();
}
}
@BeanCreates reusable objects for Spring.Example: See above (PasswordEncoder).
2. Entity Layer
Shapes your data, like employee records.
@EntityMarks a class as a database table.
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String email;
}
@IdSets the unique ID for each record.Example: See above.
@GeneratedValueAuto-creates IDs.Example: See above.
@ColumnMaps fields to table columns.
@Column(unique = true)
private String email;
@NotNullEnsures fields aren’t empty.
@NotNull
private String name;
3. Controller Layer
Handles API requests for your app.
@RestControllerManages REST API requests, returning data like JSON.
@RestController
@RequestMapping("/api/employees")
public class EmployeeController {
@GetMapping
public List<Employee> getAll() {
return service.findAll();
}
}
@RequestMappingSets a base URL for endpoints.Example: See above.
@GetMapping, @PostMapping, @PutMapping, @DeleteMappingTie methods to HTTP actions.
@PostMapping
public Employee create(@RequestBody Employee employee) {
return service.save(employee);
}
@RequestBodyTurns JSON into Java objects.Example: See above.
@PathVariableGrabs URL values (e.g., /employees/1).
@GetMapping("/{id}")
public Employee getById(@PathVariable Long id) {
return service.findById(id);
}
@ValidChecks input data for errors.
@PostMapping
public Employee create(@Valid @RequestBody Employee employee) {
return service.save(employee);
}
4. Service Layer
Runs your app’s core logic.
@ServiceMarks a class for business logic.
@Service
public class EmployeeService {
public Employee save(Employee employee) {
return repo.save(employee);
}
}
@AutowiredLinks services to other components.
@Autowired
private EmployeeRepository repo;
@TransactionalKeeps database actions safe and complete.
@Transactional
public Employee save(Employee employee) {
return repo.save(employee);
}
5. Repository Layer
Connects to your database.
@RepositoryHandles database tasks.
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
}
@QueryCreates custom database queries.
@Query("SELECT e FROM Employee e WHERE e.email = ?1")
Employee findByEmail(String email);
Example: Employee Management App
Here’s a working app using these annotations:
// App setup
@SpringBootApplication
public class EmployeeApp {
public static void main(String[] args) {
SpringApplication.run(EmployeeApp.class, args);
}
}
// Data model
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotNull
@Column
private String name;
@NotNull
@Column(unique = true)
private String email;
// getters and setters
}
// Database access
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
@Query("SELECT e FROM Employee e WHERE e.email = ?1")
Employee findByEmail(String email);
}
// Business logic
@Service
public class EmployeeService {
@Autowired
private EmployeeRepository repo;
@Transactional
public Employee save(Employee employee) {
return repo.save(employee);
}
public Employee findById(Long id) {
return repo.findById(id).orElse(null);
}
}
// API endpoints
@RestController
@RequestMapping("/api/employees")
public class EmployeeController {
@Autowired
private EmployeeService service;
@PostMapping
public Employee create(@Valid @RequestBody Employee employee) {
return service.save(employee);
}
@GetMapping("/{id}")
public Employee getById(@PathVariable Long id) {
return service.findById(id);
}
}
Try it out: send a POST to /api/employees or GET /api/employees/1 to manage employees!
Why These Matter
These annotations handle everything a mid-scale app needs:
Setup: Launch and configure your app.
Data: Store and validate employee details.
APIs: Create endpoints for adding or viewing data.
Logic: Process and protect database operations.
Database: Fetch or save data effortlessly.
Quick Tips
Test it: Use Postman to try your APIs.
Avoid pitfalls:
Always add @Id to entities.
Use @Valid with @NotNull for clean data.
Don’t skip @Transactional for saves.
Grow further: Next, try @ExceptionHandler for errors or @ManyToOne for linking data.
These annotations will fuel your mid-scale Spring Boot projects. What app are you building next? 🚀
Subscribe to my newsletter
Read articles from Sampat Karehonna directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
