Quick revision
The short answer
We implement HandlerMethodArgumentResolver: supportsParameter() returns true when the parameter has @CurrentTenant and its type is Tenant, and resolveArgument() reads the X-Tenant header from the NativeWebRequest and loads the Tenant, failing with 400 when the header is missing or unknown. We register the resolver in WebMvcConfigurer.addArgumentResolvers(). Custom resolvers are consulted after Spring's built-in ones, so they can add new kinds of parameters but cannot take over types that a built-in resolver already handles.
What to remember
In this answer, we saw how RequestMappingHandlerAdapter asks argument resolvers in order to fill each controller parameter. We wrote a @CurrentTenant annotation and a resolver whose supportsParameter checks both the annotation and the type, and whose resolveArgument reads X-Tenant and loads the tenant, failing with 400 when it cannot. We registered it with WebMvcConfigurer.addArgumentResolvers, tested both paths with MockMvc, and saw that custom resolvers come after the built-in ones, so they add new parameter kinds but cannot override existing ones.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We implement HandlerMethodArgumentResolver: supportsParameter() returns true when the parameter has @CurrentTenant and its type is Tenant, and resolveArgument() reads the X-Tenant header from the NativeWebRequest and loads the Tenant, failing with 400 when the header is missing or unknown. We register the resolver in WebMvcConfigurer.addArgumentResolvers(). Custom resolvers are consulted after Spring's built-in ones, so they can add new kinds of parameters but cannot take over types that a built-in resolver already handles.
Why this question is asked
Many services need the same piece of context in almost every controller method: the current tenant, the current user, a request locale. Reading the header by hand in every method repeats code and lets mistakes slip in, such as one endpoint that forgets to check the tenant exists. An argument resolver puts that logic in one place and lets controllers simply declare what they need. Interviewers ask for one because it shows whether we know how Spring MVC fills method parameters. In this answer, we will recap how parameters are resolved, write the annotation and the resolver, register it, test it, and look at the ordering rule that limits what custom resolvers can do.
1A Quick Recap of How Controller Parameters Get Filled
When DispatcherServlet invokes a controller method, it does not call it directly. RequestMappingHandlerAdapter, the component that runs annotated handler methods, looks at each parameter and asks a list of argument resolvers, one by one, whether they support it. The first resolver that says yes produces the value. This is how @PathVariable, @RequestParam, @RequestBody, HttpServletRequest and Principal parameters all get their values.
A HandlerMethodArgumentResolver has two methods. supportsParameter(MethodParameter) answers whether this resolver handles a given parameter; a MethodParameter describes one parameter, with its type and annotations. resolveArgument(...) produces the value, and it receives a NativeWebRequest, a thin wrapper that gives access to headers, parameters and the underlying servlet request. Spring caches the answer of supportsParameter per parameter, so the check runs once, not on every request.
2Writing the Annotation and the Resolver
First the annotation. It needs runtime retention so the resolver can see it through reflection, and it targets parameters only.
Notice that supportsParameter checks both the annotation and the type. Checking only the annotation would let someone write @CurrentTenant String tenant and get a ClassCastException-style failure at invocation time. Checking only the type would capture every Tenant parameter, even ones meant to be bound some other way.
In resolveArgument we fail with exceptions that Spring already maps to sensible responses. MissingRequestHeaderException is a ServletRequestBindingException, which the default exception handling turns into 400 Bad Request, the same response a missing @RequestHeader would give. ResponseStatusException lets us choose the status directly. Loading the Tenant from a repository, rather than building it from the header text alone, also confirms that the tenant exists.
3Registering the Resolver
The resolver is not a magic bean that Spring discovers on its own. We add it through WebMvcConfigurer, the callback interface Spring Boot uses for MVC customisations.
- Adapter takes the next parametertype Tenant, annotation
@CurrentTenant - A built-in resolver supports it?Yesuse the built-in resolverNotry the custom resolvers
- CurrentTenantArgumentResolver supports it?YesresolveArgument reads X-TenantNofall through to the catch-all resolvers
- Load the Tenantmissing or unknown header gives 400
- Invoke the controller methodtenant is ready to use
5 steps in order. Play them, or step through them one at a time.
We do not add @EnableWebMvc here. In Spring Boot that annotation switches off the MVC auto-configuration, and we only want to add one resolver, not take over the whole setup.
4Testing It With MockMvc
A web slice test proves the whole chain: the resolver is registered, the header is read, and errors come out as 400.
The second test matters as much as the first. A resolver that silently returns null for a missing header would let a controller run without a tenant, which in a multi-tenant system can mean reading another customer's data.
5Why Custom Resolvers Cannot Override Built-in Types
The adapter builds its list of resolvers in a fixed order. First come the built-in annotation-based resolvers (@RequestParam, @PathVariable, @RequestBody and others), then the built-in type-based ones (HttpServletRequest, Principal, Locale and so on), then our custom resolvers, and finally a couple of catch-all resolvers that treat any remaining simple type as a request parameter and any remaining object as a model attribute.
So a custom resolver can add a new kind of parameter, as ours does, and it runs before the catch-alls, which is why our Tenant is not mistaken for a model attribute. But if we wrote a resolver for Principal or for @RequestParam, it would never be asked: a built-in resolver says yes first. Replacing a built-in behaviour requires changing the adapter's resolver list itself, for example from a BeanPostProcessor on RequestMappingHandlerAdapter, which is fragile across upgrades and rarely worth it. Using our own annotation, as here, avoids the problem completely.
6Follow-up questions
Should the tenant come from a header at all?
Only if the header is trusted or checked. A client can send any X-Tenant value, so the resolver, or a security rule, must confirm that the authenticated user belongs to that tenant. In many systems the tenant is better taken from the authentication token, and the header is used only for users who legitimately work across several tenants.
Why not use a filter that stores the tenant in a ThreadLocal?
A filter is useful when code far from the controller, such as a repository or a logging pattern, needs the tenant. But ThreadLocals hide the dependency and must be cleared carefully, and they do not follow async work to other threads. The argument resolver makes the dependency visible in the method signature and is easy to test. The two can be combined: a filter sets the context for logging, and the resolver gives controllers a typed parameter.
How would you make the parameter optional?
Add an attribute to the annotation, such as boolean required() default true, read it in resolveArgument with parameter.getParameterAnnotation(CurrentTenant.class), and return null when the header is absent and required is false. Supporting Optional<Tenant> is another option; then supportsParameter would check parameter.nestedIfOptional() for the inner type.
7Conclusion
In this answer, we saw how RequestMappingHandlerAdapter asks argument resolvers in order to fill each controller parameter. We wrote a @CurrentTenant annotation and a resolver whose supportsParameter checks both the annotation and the type, and whose resolveArgument reads X-Tenant and loads the tenant, failing with 400 when it cannot. We registered it with WebMvcConfigurer.addArgumentResolvers, tested both paths with MockMvc, and saw that custom resolvers come after the built-in ones, so they add new parameter kinds but cannot override existing ones.
Open with
I'd start with a runtime-retained @CurrentTenant annotation targeting parameters. Then I'd implement HandlerMethodArgumentResolver. In supportsParameter I return true only when the parameter has the annotation and its type is assignable to Tenant, so a misuse like @CurrentTenant String is not picked up.
Then, if they want more
In resolveArgument I get the X-Tenant header from the NativeWebRequest; if it's missing I throw MissingRequestHeaderException, which Spring maps to 400 just like a missing @RequestHeader, and otherwise I load the tenant from a repository and throw a 400 ResponseStatusException if it doesn't exist.
I register it in a @Configuration class implementing WebMvcConfigurer, in addArgumentResolvers, and I don't add @EnableWebMvc because in Boot that would turn off the auto-configuration. A controller then just declares @CurrentTenant Tenant tenant. I'd test it with @WebMvcTest and MockMvc, one test with a valid header and one without, because silently returning null in a multi-tenant system can leak data.
One thing worth knowing: RequestMappingHandlerAdapter puts custom resolvers after the built-in ones but before the catch-all model attribute resolver. So my resolver works for a new annotation, but I couldn't override how Principal or @RequestParam are resolved that way. I'd also mention that the header must be checked against the authenticated user, otherwise any client could pick any tenant.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Framework 6
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Spring Web MVC
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Write a HandlerMethodArgumentResolver so that a controller parameter annotated...": https://scrivio-interview-library.netlify.app/spring-boot/mvc-architecture/b85b6a7038/