How to Dynamically Register Spring MVC Handlers in Spring Boot 2.2
This guide demonstrates how to create a custom request‑handling method, register it programmatically with Spring MVC's RequestMappingHandlerMapping, and explains the underlying handler detection and mapping process in Spring Boot 2.2.
Environment: Spring Boot 2.2.10.RELEASE
Write a method prepared for handling requests
<code>@Service
public class UserHandler {
@ResponseBody
public Object getUsers(@PathVariable("id") String id, HttpServletRequest request) {
System.out.println(request);
return "查询用户ID为:" + id;
}
}
</code>The handler does not need to be a container‑managed bean; the Spring MVC annotations work the same as in a @Controller.
Register the interface handler
<code>@Configuration
public class MappingConfig {
@Autowired
public void setHandlerMapping(RequestMappingHandlerMapping mapping, UserHandler handler) throws NoSuchMethodException {
RequestMappingInfo info = RequestMappingInfo.paths("/users/{id}").methods(RequestMethod.GET).build();
Method method = UserHandler.class.getMethod("getUsers", String.class, HttpServletRequest.class);
mapping.registerMapping(info, handler, method);
}
}
</code>Create a RequestMappingInfo object containing basic request metadata.
Obtain the Method object of the handler.
Register the mapping with RequestMappingHandlerMapping.
During container startup, Spring wraps all @Controller methods into RequestMappingInfo objects and adds them to the RequestMappingHandlerMapping collection.
The container holds many HandlerMapping beans; whether a class is considered a handler is decided by AbstractHandlerMethodMapping#isHandler, which subclasses implement.
<code>public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMapping implements InitializingBean {
protected abstract boolean isHandler(Class<?> beanType);
}
</code>RequestMappingHandlerMapping overrides isHandler to check for @Controller or @RequestMapping annotations.
<code>public class RequestMappingHandlerMapping {
@Override
protected boolean isHandler(Class<?> beanType) {
return (AnnotatedElementUtils.hasAnnotation(beanType, Controller.class) ||
AnnotatedElementUtils.hasAnnotation(beanType, RequestMapping.class));
}
}
</code>Thus any bean annotated with @Controller or @RequestMapping is automatically registered as a RequestMappingInfo after the container starts.
When Spring MVC processes a request, it first obtains the appropriate HandlerMapping.
Method parameter handling
Supported parameters include JDK 8 java.util.Optional combined with annotations such as @RequestParam, @RequestHeader, etc., which behave as if required=false.
Method return value handling
End of tutorial.
Public: Spring Boot practical case collection
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.