Spring MVC (Servlet Web)
The servlet-stack web framework: DispatcherServlet, controllers and request mapping, message conversion and content negotiation, exception handling, validation, and the synchronous HTTP clients. Still the default way Spring services are written, so it dominates practical backend interviews.
part ofSpring Frameworkoverview, primer and where to startread it →on this pageshowhide
explore
- Request-Processing Lifecycle25 questions
- DispatcherServlet Front Controller5 questions
- HandlerMapping & Execution Chain5 questions
- HandlerAdapter Invocation5 questions
- HandlerExceptionResolver Chain5 questions
- View Resolution & Thymeleaf5 questions
- Controllers, Mapping & Handler Methods36 questions
- @Controller vs @RestController5 questions
- @RequestMapping & Method Shortcuts5 questions
- Path Patterns & Variables5 questions
- @RequestBody, @ModelAttribute & Data Binding5 questions
- Argument & Return-Value Handlers6 questions
- Functional Endpoints (WebMvc.fn)5 questions
- Message Conversion & Content Negotiation20 questions
- HttpMessageConverters5 questions
- Content Negotiation5 questions
- Jackson Configuration5 questions
- ResponseEntity & Response Shaping5 questions
- Exception Handling & Validation25 questions
- @ExceptionHandler (Local)5 questions
- ProblemDetail / RFC 78075 questions
- Bean Validation (@Valid / BindingResult)5 questions
- @ResponseStatus & ResponseStatusException5 questions
- Interceptors, CORS, Multipart & Async25 questions
- HandlerInterceptor vs Servlet Filter5 questions
- CORS Configuration5 questions
- Multipart & File Upload5 questions
- Async MVC (Callable / DeferredResult)5 questions
- Streaming & Server-Sent Events5 questions
- Synchronous HTTP Clients20 questions
- RestClient (Fluent Sync Client)5 questions
- RestTemplate (Legacy)5 questions
- Declarative HTTP Interfaces5 questions
- Client Error Handling5 questions
- MVC Configuration & Infrastructure20 questions
- HTTP Caching (ETag / Cache-Control)5 questions
- Static Resource Handling5 questions
- URI Building (UriComponentsBuilder)5 questions
- WebMvcConfigurer & MVC Java Config5 questions
questions
171 · 7 sectionsWhen a Spring MVC controller throws an exception, how does the framework turn it into an HTTP response?
basics
~20 sThe DispatcherServlet catches the exception and offers it to a list of HandlerExceptionResolvers. The first one that can handle it writes the response (status and body). If none handle it, the exception propagates to the servlet container.
What is a HandlerAdapter in Spring MVC and why does DispatcherServlet need one?
basics
~10 sA HandlerAdapter knows how to actually call a handler. DispatcherServlet finds the handler, then delegates to a HandlerAdapter to invoke it and get a ModelAndView, so it doesn't need to know each handler type.
What is a HandlerMapping in Spring MVC, and what does its getHandler method return for an incoming request?
basics
~10 sA HandlerMapping decides which controller method should handle an incoming HTTP request. Its getHandler method takes the request and returns a HandlerExecutionChain: the chosen handler plus any interceptors that run around it.
What is the DispatcherServlet in Spring MVC, and what design pattern does it implement?
basics
~10 sDispatcherServlet is Spring MVC's single entry point for HTTP requests. It implements the Front Controller pattern: one servlet receives every request and delegates to the right handler (controller), then renders the response.
In Spring MVC, when a controller method returns the String "home", how does that turn into an actual HTML page? Walk through logical view names, the Model, and the ViewResolver.
basics
~10 sThe returned String is a logical view name, not a file path. A ViewResolver maps it to a real template (e.g. "home" -> /WEB-INF/home.jsp or home.html). Data reaches the view through a Model object.
How does Spring MVC decide what value to pass to each parameter of a @RequestMapping controller method?
basics
~20 sFor every controller-method parameter Spring asks a list of HandlerMethodArgumentResolver strategies which one can handle it. The first that says yes resolves the value (from a request param, path variable, body, etc.) and passes it in.
How do you capture a dynamic segment from a URL in a Spring MVC controller, and how do you read its value?
basics
~10 sPut a placeholder in curly braces in the mapping, like /users/{id}, then bind it with @PathVariable on a method parameter. Spring extracts that URL segment and passes it into your method.
What is the difference between @RequestBody and @ModelAttribute in a Spring MVC controller?
basics
~10 s@RequestBody reads the raw request body (usually JSON) and deserializes it into an object using a message converter. @ModelAttribute binds request parameters (query string or HTML form fields) onto an object via its setters.
What is @RequestMapping in Spring MVC, and how do the shortcuts @GetMapping, @PostMapping, @PutMapping, @DeleteMapping, and @PatchMapping relate to it?
basics
~10 s@RequestMapping maps HTTP requests (by URL path and method) to controller methods. @GetMapping, @PostMapping, @PutMapping, @DeleteMapping, and @PatchMapping are shortcuts for @RequestMapping with the HTTP method already fixed (GET, POST, PUT, DELETE, PATCH).
What is the difference between @RequestParam and @PathVariable, and when do you use each?
basics
~20 s@PathVariable pulls a value out of the URL path itself (e.g. /users/42 -> id=42). @RequestParam reads a query-string parameter after the ? (e.g. /users?id=42) or a form field. You use @PathVariable to identify a resource, @RequestParam to filter/paginate/search.
What is content negotiation in Spring MVC, and how does Spring decide what format (JSON, XML) to send back?
basics
~20 sContent negotiation is how Spring picks the response format. The client sends an Accept header (e.g. application/json); Spring matches it against the formats a handler can produce and the available message converters, then writes that format.
What is an HttpMessageConverter in Spring MVC, and when does Spring use one?
basics
~10 sIt converts between HTTP request/response bodies (bytes) and Java objects. Spring uses it whenever you annotate a parameter with @RequestBody or a return value with @ResponseBody (including @RestController).
How does Spring Boot set up Jackson's ObjectMapper for a REST controller, and how do you tweak its behavior without writing Java?
basics
~10 sSpring Boot auto-configures a Jackson ObjectMapper bean and uses it in MappingJackson2HttpMessageConverter to turn objects into JSON. You tweak it declaratively with spring.jackson.* properties in application.yml/properties.
What is ResponseEntity<T> in Spring MVC, and why would you return it from a controller instead of returning the body object directly?
basics
~10 sResponseEntity<T> represents the full HTTP response: status code, headers, and body. You return it (instead of a plain object) when you need to control the status code or add headers, not just the body.
Explain produces vs consumes on a request mapping, and which HTTP status each drives when it fails to match.
basics
~10 sconsumes matches the request body's Content-Type; a mismatch yields 415 Unsupported Media Type. produces matches the client's Accept header (what the response can be); a mismatch yields 406 Not Acceptable.
What does @Valid on a Spring MVC controller parameter do, and what happens when validation fails?
basics
~20 s@Valid tells Spring to run Jakarta Bean Validation constraints (like @NotNull, @Size) on the argument. If a constraint fails and there is no BindingResult next to it, Spring throws an exception that becomes an HTTP 400 Bad Request.
What are @ControllerAdvice and @RestControllerAdvice, and how do @ExceptionHandler methods inside them work?
basics
~20 s@ControllerAdvice is a class whose @ExceptionHandler methods catch exceptions thrown by many controllers in one place, instead of repeating handlers in each controller. @RestControllerAdvice is the same but adds @ResponseBody, so returned objects become JSON.
What is a controller-local @ExceptionHandler method and how do you use one?
basics
~20 sA method inside a @Controller annotated with @ExceptionHandler. When a request-handling method in that same controller throws a matching exception, Spring calls the handler instead of letting the error escape, and its return value becomes the response.
What is ProblemDetail in Spring MVC, and what RFC / media type does it implement?
basics
~10 sProblemDetail is a Spring class (org.springframework.http.ProblemDetail) that models a standard error body from RFC 7807. It is serialized as application/problem+json with fields like type, title, status, detail, and instance.
What is @ResponseStatus and how do you use it to control the HTTP status code returned from a Spring MVC controller?
basics
~10 s@ResponseStatus is a Spring annotation that sets the HTTP status code of a response. You put it on a custom exception class or a controller method, e.g. @ResponseStatus(HttpStatus.NOT_FOUND) on a NotFoundException.
What happens when a Spring MVC controller method returns a Callable<T> instead of a plain value, and why would you do it?
basics
~10 sSpring runs the Callable on a separate thread pool and frees the servlet (Tomcat) thread immediately, so it can handle other requests. When the Callable finishes, Spring resumes and writes the response.
What is CORS, and how do you enable cross-origin requests for a single Spring MVC controller or handler method?
basics
~20 sCORS (Cross-Origin Resource Sharing) is a browser rule that blocks JavaScript from calling a different origin unless the server allows it. In Spring you allow it by adding @CrossOrigin on a controller class or handler method.
What is the difference between a Servlet Filter and a Spring HandlerInterceptor?
basics
~10 sA Filter runs in the Servlet container around every request, before Spring even sees it. A HandlerInterceptor runs inside Spring MVC's DispatcherServlet, only for controller requests, and knows which controller (handler) will run.
How do you receive an uploaded file in a Spring MVC controller, and what does MultipartFile give you?
basics
~10 sAdd a handler method parameter of type MultipartFile annotated with @RequestParam matching the form field name. Spring binds the uploaded part to it; then call getOriginalFilename(), getBytes()/getInputStream(), getSize(), and transferTo() to save it.
What is SseEmitter in Spring MVC, and how do you use it to stream Server-Sent Events to a browser?
basics
~20 sSseEmitter is a Spring MVC return type that lets a controller push multiple messages to the client over one open HTTP connection as Server-Sent Events (text/event-stream). You return it immediately, then call emitter.send(...) for each event and emitter.complete() when done.
What happens by default when a Spring REST client (RestTemplate or RestClient) receives a 4xx or 5xx HTTP response?
basics
~10 sBy default the client treats 4xx/5xx as errors and throws an exception: HttpClientErrorException for 4xx and HttpServerErrorException for 5xx. A successful 2xx just returns the body normally.
What is a Spring HTTP Interface (declarative HTTP client), and how do you declare one with @HttpExchange?
basics
~10 sYou define a Java interface with methods annotated like @GetExchange("/users/{id}"). Spring generates a proxy that turns each call into a real HTTP request, so you never write HTTP plumbing by hand.
What is Spring's RestClient and how does its fluent API differ from the older RestTemplate?
basics
~10 sRestClient is a synchronous HTTP client (Spring 6.1+) with a modern fluent, chained API — client.get().uri(...).retrieve().body(Type.class). RestTemplate is the older template-method client it's meant to replace for blocking calls.
What is Spring's RestTemplate, and how do you make a simple GET and POST call with it?
basics
~10 sRestTemplate is Spring's synchronous HTTP client for calling REST APIs. Use getForObject(url, Type.class) to GET and deserialize a response, and postForObject/postForEntity(url, body, Type.class) to POST a body and read the reply.
How do you customize error handling for specific status codes with RestClient's onStatus handlers?
basics
~20 sOn the retrieve() path, chain .onStatus(predicate, handler). The predicate tests the HttpStatusCode; the handler receives the request and ClientHttpResponse so you can throw your own exception or read the body. Matching handlers replace the default throw.
How do you add a Cache-Control response header to a Spring MVC controller method, and what does the CacheControl builder give you?
basics
~10 sReturn a ResponseEntity and call .cacheControl(...) with Spring's CacheControl builder, e.g. CacheControl.maxAge(Duration.ofMinutes(10)).cachePublic(). Spring writes the correct Cache-Control header (max-age=600, public) so browsers/proxies can cache the response.
How do you serve static files (CSS, JS, images) in Spring MVC, and where does Spring Boot look for them by default?
basics
~10 sPut files under src/main/resources/static (or public/resources/META-INF/resources) and Spring Boot serves them automatically at the URL root. To configure manually, override addResourceHandlers in a WebMvcConfigurer.
What is UriComponentsBuilder and why use it instead of string concatenation to build URLs?
basics
~20 sUriComponentsBuilder is a Spring helper that builds a URI piece by piece — scheme, host, path, query params — and handles proper encoding for you. It avoids bugs from manually gluing strings and forgetting to escape special characters.
What is WebMvcConfigurer and how do you use it to customize Spring MVC?
basics
~10 sWebMvcConfigurer is an interface with default (empty) callback methods. You create a @Configuration class implementing it and override only the callbacks you need, like addInterceptors or addViewControllers, to tweak MVC without replacing everything.
What does ShallowEtagHeaderFilter do, how does it generate an ETag, and what is it NOT good for?
basics
~20 sIt's a servlet filter that buffers the full response, computes an MD5 hash of the body, and sends it as the ETag header. On the next request Spring compares the client's If-None-Match; if it matches, it returns 304 Not Modified with no body — saving bandwidth, not server work.