skip to content

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 pageshow

explore

questions

171 · 7 sections

When a Spring MVC controller throws an exception, how does the framework turn it into an HTTP response?

level: juniorimportance: must knowfreq 45%
basics
~20 s

The 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.

open as a page

What is a HandlerAdapter in Spring MVC and why does DispatcherServlet need one?

level: juniorimportance: must knowfreq 55%
basics
~10 s

A 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.

open as a page

What is a HandlerMapping in Spring MVC, and what does its getHandler method return for an incoming request?

level: juniorimportance: must knowfreq 70%
basics
~10 s

A 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.

open as a page

What is the DispatcherServlet in Spring MVC, and what design pattern does it implement?

level: juniorimportance: must knowfreq 80%
basics
~10 s

DispatcherServlet 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.

open as a page

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.

level: juniorimportance: must knowfreq 70%
basics
~10 s

The 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.

open as a page

How does Spring MVC decide what value to pass to each parameter of a @RequestMapping controller method?

level: juniorimportance: must knowfreq 70%
basics
~20 s

For 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.

open as a page

How do you capture a dynamic segment from a URL in a Spring MVC controller, and how do you read its value?

level: juniorimportance: must knowfreq 85%
basics
~10 s

Put 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.

open as a page

What is the difference between @RequestBody and @ModelAttribute in a Spring MVC controller?

level: juniorimportance: must knowfreq 78%
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.

open as a page

What is @RequestMapping in Spring MVC, and how do the shortcuts @GetMapping, @PostMapping, @PutMapping, @DeleteMapping, and @PatchMapping relate to it?

level: juniorimportance: must knowfreq 90%
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).

open as a page

What is the difference between @RequestParam and @PathVariable, and when do you use each?

level: juniorimportance: must knowfreq 85%
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.

open as a page

What is content negotiation in Spring MVC, and how does Spring decide what format (JSON, XML) to send back?

level: juniorimportance: must knowfreq 62%
basics
~20 s

Content 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.

open as a page

What is an HttpMessageConverter in Spring MVC, and when does Spring use one?

level: juniorimportance: must knowfreq 70%
basics
~10 s

It 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).

open as a page

How does Spring Boot set up Jackson's ObjectMapper for a REST controller, and how do you tweak its behavior without writing Java?

level: juniorimportance: must knowfreq 70%
basics
~10 s

Spring 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.

open as a page

What is ResponseEntity<T> in Spring MVC, and why would you return it from a controller instead of returning the body object directly?

level: juniorimportance: must knowfreq 80%
basics
~10 s

ResponseEntity<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.

open as a page

Explain produces vs consumes on a request mapping, and which HTTP status each drives when it fails to match.

level: middleimportance: must knowfreq 70%
basics
~10 s

consumes 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.

open as a page

What does @Valid on a Spring MVC controller parameter do, and what happens when validation fails?

level: juniorimportance: must knowfreq 80%
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.

open as a page

What are @ControllerAdvice and @RestControllerAdvice, and how do @ExceptionHandler methods inside them work?

level: juniorimportance: must knowfreq 78%
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.

open as a page

What is a controller-local @ExceptionHandler method and how do you use one?

level: juniorimportance: must knowfreq 72%
basics
~20 s

A 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.

open as a page

What is ProblemDetail in Spring MVC, and what RFC / media type does it implement?

level: juniorimportance: must knowfreq 62%
basics
~10 s

ProblemDetail 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.

open as a page

What is @ResponseStatus and how do you use it to control the HTTP status code returned from a Spring MVC controller?

level: juniorimportance: must knowfreq 70%
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.

open as a page

What happens when a Spring MVC controller method returns a Callable<T> instead of a plain value, and why would you do it?

level: juniorimportance: must knowfreq 45%
basics
~10 s

Spring 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.

open as a page

What is CORS, and how do you enable cross-origin requests for a single Spring MVC controller or handler method?

level: juniorimportance: must knowfreq 75%
basics
~20 s

CORS (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.

open as a page

What is the difference between a Servlet Filter and a Spring HandlerInterceptor?

level: juniorimportance: must knowfreq 72%
basics
~10 s

A 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.

open as a page

How do you receive an uploaded file in a Spring MVC controller, and what does MultipartFile give you?

level: juniorimportance: must knowfreq 60%
basics
~10 s

Add 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.

open as a page

What is SseEmitter in Spring MVC, and how do you use it to stream Server-Sent Events to a browser?

level: juniorimportance: must knowfreq 55%
basics
~20 s

SseEmitter 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.

open as a page

What happens by default when a Spring REST client (RestTemplate or RestClient) receives a 4xx or 5xx HTTP response?

level: juniorimportance: must knowfreq 78%
basics
~10 s

By 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.

open as a page

What is a Spring HTTP Interface (declarative HTTP client), and how do you declare one with @HttpExchange?

level: juniorimportance: must knowfreq 55%
basics
~10 s

You 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.

open as a page

What is Spring's RestClient and how does its fluent API differ from the older RestTemplate?

level: juniorimportance: must knowfreq 70%
basics
~10 s

RestClient 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.

open as a page

What is Spring's RestTemplate, and how do you make a simple GET and POST call with it?

level: juniorimportance: must knowfreq 70%
basics
~10 s

RestTemplate 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.

open as a page

How do you customize error handling for specific status codes with RestClient's onStatus handlers?

level: middleimportance: must knowfreq 70%
basics
~20 s

On 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.

open as a page

How do you add a Cache-Control response header to a Spring MVC controller method, and what does the CacheControl builder give you?

level: juniorimportance: must knowfreq 62%
basics
~10 s

Return 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.

open as a page

How do you serve static files (CSS, JS, images) in Spring MVC, and where does Spring Boot look for them by default?

level: juniorimportance: must knowfreq 70%
basics
~10 s

Put 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.

open as a page

What is UriComponentsBuilder and why use it instead of string concatenation to build URLs?

level: juniorimportance: must knowfreq 32%
basics
~20 s

UriComponentsBuilder 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.

open as a page

What is WebMvcConfigurer and how do you use it to customize Spring MVC?

level: juniorimportance: must knowfreq 70%
basics
~10 s

WebMvcConfigurer 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.

open as a page

What does ShallowEtagHeaderFilter do, how does it generate an ETag, and what is it NOT good for?

level: middleimportance: must knowfreq 55%
basics
~20 s

It'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.

open as a page