Java Spring MVC and REST
How a request becomes a method call and a response. The failure modes — validation, error handling, serialization — are what production actually tests.
5 concepts · 12 interview questions
What this topic covers
Every concept in spring mvc and rest, and the questions each one gets asked as. Where a question links, it has a full write-up.
The request lifecycle
DispatcherServlet routes to a handler through mappings, adapters, argument resolvers and message converters.
- Walk me through what happens when a request hits a Spring MVC application.
- What does the DispatcherServlet do?
- How is the request body converted into an object?
Controllers and mapping
@RestController combines @Controller and @ResponseBody; mappings bind path, method, headers and content type.
- @Controller versus @RestController?
- @RequestParam versus @PathVariable versus @RequestBody?
- How do you version a REST API?
Validation
Bean Validation annotations enforced at the boundary, producing a structured failure rather than a 500.
- How does @Valid work, and where does the validation happen?
- Why does @Valid do nothing on a nested object sometimes?
Error handling
@ExceptionHandler and @ControllerAdvice turn exceptions into responses, centrally rather than per controller.
- How do you handle exceptions across all controllers?
- How do you return a consistent error body?
- What is ProblemDetail?
Filters versus interceptors
Filters are servlet-level and see every request; interceptors are Spring-level and know the handler.
- What is the difference between a Filter and a HandlerInterceptor?