Imagine you need to develop a REST API in a Spring Boot application that allows clients to manage user data. Explain how you would structure your application.

A REST API for managing user data is best organized into three layers -- Controller, Service, and Repository -- so each part has a single, clear responsibility.

Key Points: • Controllers expose endpoints (e.g. /users) and translate HTTP requests into calls on the service layer. • Services hold business logic, such as validating user data before it is persisted. • Repositories (typically Spring Data JPA interfaces) handle the actual database access. • DTOs decouple the API contract from the internal entity model. • This layering makes the app easier to test, since each layer can be mocked independently.

Example: A GET request to /users/5 hits UserController, which calls userService.findById(5), which in turn calls userRepository.findById(5) to fetch the record from the database.

Code Example:

@RestController
@RequestMapping("/users")
public class UserController {
    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("/{id}")
    public UserDto getUser(@PathVariable Long id) {
        return userService.findById(id);
    }

    @PostMapping
    public UserDto createUser(@RequestBody UserDto dto) {
        return userService.create(dto);
    }
}

Interview Tip: A concise interview answer is:

"I'd split the API into Controller, Service, and Repository layers -- controllers handle HTTP and routing, services own business logic and validation, and repositories handle persistence via Spring Data JPA. This separation keeps the code testable and lets each layer change independently."