Walk me through building a REST endpoint in Spring Boot, from request to database.
Answer
- Controller (
@RestController): maps the URL, reads input (@PathVariable, @RequestBody with @Valid), returns a DTO and the right status (ResponseEntity). - Service (
@Service): business rules and @Transactional boundaries. - Repository (
JpaRepository): database access; entity with @Entity. - Error handling:
@RestControllerAdvice turns exceptions into 400/404/409 responses. - Config:
application.yml, profiles, secrets from environment variables. - Tests: unit tests with Mockito for services,
@WebMvcTest for controllers, @DataJpaTest or Testcontainers for repositories. - Ops: Actuator health endpoint, structured logs.
Practice this question in Repaso