Review a Spring REST Controller for Correctness and Per-Request Waste
Company: Schwab
Role: Java Developer
Category: Software Engineering Fundamentals
Difficulty: easy
Interview Round: Onsite
# Review a Spring REST Controller for Correctness and Per-Request Waste
You are given the implementation of a Spring REST controller. Review it and explain concrete improvements, with particular attention to error handling, HTTP semantics, validation, dependency boundaries, performance, and utility objects constructed repeatedly for each request.
For every finding, identify the triggering input or state, the current observable behavior, the desired behavior, and a focused way to verify the change. Do not assume a line is wrong merely because it could be written differently.
### Constraints & Assumptions
- The actual controller, its service dependencies, and existing tests are supplied during the exercise.
- A controller should remain a thin HTTP adapter; business rules and persistence transactions belong behind a service boundary.
- Reusing an object is safe only if it is immutable or documented as thread-safe.
- Performance claims must distinguish measured hot-path work from stylistic preferences.
### Clarifying Questions to Ask
- What request and response contract, status codes, and error body format must this endpoint preserve?
- Which exceptions represent client errors, missing resources, conflicts, dependency failures, or unexpected faults?
- Are utility instances immutable and thread-safe, and is their construction measurably expensive?
- What authentication, authorization, idempotency, or transaction rules apply?
- Which integration or controller tests already define behavior?
### What a Strong Answer Covers
- Request validation, authorization placement, service delegation, and explicit mapping from domain outcomes to HTTP responses.
- Centralized exception handling with safe client messages, stable error shapes, correlation information, and complete server-side logs.
- Correct status codes and headers for creation, absence, validation failure, conflict, and unexpected failure.
- Avoidance of broad exception swallowing, success responses for failures, leaked stack details, and blocking work on inappropriate threads.
- Constructor-injected reusable dependencies and careful confirmation that a proposed singleton utility is thread-safe.
- Elimination of avoidable per-request construction or repeated parsing only when evidence shows it is safe and relevant.
- Bounded payloads, pagination or streaming where needed, timeouts, cancellation, and downstream failure behavior.
- Focused tests for valid requests, malformed input, missing resources, conflicts, service failures, and concurrency-sensitive reuse.
### Follow-up Questions
1. When should an exception be translated in the controller, and when should a global exception handler own it?
2. A utility is expensive to create but contains mutable formatting state. How would you improve performance safely?
3. How do you distinguish a client cancellation from a downstream timeout in logs and HTTP behavior?
4. Which controller test proves that a missing resource does not accidentally return a successful empty response?
5. What controller responsibility would you move into a service before optimizing anything?
Overview: Review a Spring REST controller by tracing success and failure paths, HTTP semantics, validation, and service boundaries. The answer examines centralized error handling, safe dependency reuse, per-request allocation, timeouts, pagination, concurrency, and focused controller tests.
Read the full Schwab Java Developer interview experience this question came from