skip to content

Controllers, Mapping & Handler Methods

Writing the handlers themselves: controller stereotypes, request mapping and path patterns, binding parameters and bodies, custom argument resolvers, and the functional WebMvc.fn alternative. This is the code interviewers ask you to write on a whiteboard.

part ofSpring Frameworkoverview, primer and where to startread it →
on this pageshow

questions

page 2 of 2

What actually converts a @RequestParam/@PathVariable String into a LocalDate, enum, or custom value object — and how do you customize or fix a conversion failure?

level: principalimportance: should knowfreq 35%

basics

~20 s

Spring runs the raw String through a ConversionService (a FormattingConversionService) using registered Converter and Formatter implementations. For dates you add @DateTimeFormat or a Formatter; for custom types you register a Converter<String, YourType>. Failures throw MethodArgumentTypeMismatchException -> 400.

open as a page

What are matrix variables in Spring MVC, how do you bind them with @MatrixVariable, and what must be configured to use them?

level: seniorimportance: nice to knowfreq 25%

basics

~20 s

Matrix variables are semicolon-separated name=value pairs attached to a path segment, like /cars;color=red;year=2020. You bind them with @MatrixVariable. In Spring MVC they're stripped by default, so you must enable them by turning off URL-path removeSemicolonContent.

open as a page

A team wants a custom resolver to intercept a parameter that Spring currently data-binds as a @ModelAttribute. Why does addArgumentResolvers work here but not for overriding @RequestParam, and how would you fully control ordering?

level: principalimportance: nice to knowfreq 22%

basics

~20 s

Custom resolvers are inserted after the specific built-ins but before the two catch-all resolvers (simple-type request param and default model-attribute). So a custom resolver beats the model-attribute catch-all, but never beats the explicit @RequestParam resolver. For total control, set the full ordered list on RequestMappingHandlerAdapter.

open as a page

When would you choose functional endpoints over annotated controllers, what are the tradeoffs, and how do you test a RouterFunction?

level: principalimportance: nice to knowfreq 18%

basics

~20 s

Use functional endpoints when you want routing centralized, composable, or generated in code, and prefer explicit request/response handling. Annotations are more concise and feature-rich for typical CRUD. Test a RouterFunction by calling handlers directly with a mock ServerRequest, or via MockMvc against the context.

open as a page

Mechanically, how do the @GetMapping-style shortcuts work as composed annotations, and how would you build a custom mapping annotation like @GetJsonMapping?

level: principalimportance: nice to knowfreq 22%

basics

~20 s

The shortcuts are annotations meta-annotated with @RequestMapping, using @AliasFor to forward attributes (path, params, produces) while fixing method. Spring's AnnotatedElementUtils / MergedAnnotations resolves this. You build your own by meta-annotating a new annotation with @RequestMapping and presetting attributes.

open as a page

As an architect, how do you decide between @Controller (server-rendered) and @RestController (API) styles, and what are the consequences of that choice?

level: principalimportance: nice to knowfreq 20%

basics

~20 s

Use @RestController for stateless JSON/XML APIs consumed by SPAs, mobile apps, or other services. Use @Controller with view templates when the server renders HTML directly. The choice drives your whole front-end architecture, state handling, and cross-cutting concerns.

open as a page

showing 31–36 of 36