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 pageshowhide
explore
- @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
questions
page 2 of 2What actually converts a @RequestParam/@PathVariable String into a LocalDate, enum, or custom value object — and how do you customize or fix a conversion failure?
basics
~20 sSpring 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.
What are matrix variables in Spring MVC, how do you bind them with @MatrixVariable, and what must be configured to use them?
basics
~20 sMatrix 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.
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?
basics
~20 sCustom 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.
When would you choose functional endpoints over annotated controllers, what are the tradeoffs, and how do you test a RouterFunction?
basics
~20 sUse 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.
Mechanically, how do the @GetMapping-style shortcuts work as composed annotations, and how would you build a custom mapping annotation like @GetJsonMapping?
basics
~20 sThe 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.
As an architect, how do you decide between @Controller (server-rendered) and @RestController (API) styles, and what are the consequences of that choice?
basics
~20 sUse @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.
showing 31–36 of 36