HttpServletUtil
Servlet 请求工具类,提供从 HttpServletRequest 解析 HandlerMethod 的能力
HttpServletUtil
HttpServletUtil 是一个静态工具类,提供从 HttpServletRequest 解析 Spring MVC HandlerMethod 的能力。该工具类主要在过滤器层使用,用于在请求到达 Controller 之前获取目标处理方法的元信息(如注解、参数类型等)。
方法详解
字段
类型
参数说明
字段
类型
返回值
- 若请求 URL 匹配到有效的 Controller 方法,返回对应的
HandlerMethod实例 - 若无法匹配(如请求静态资源或 URL 不存在),返回
null
使用场景
在过滤器中检查注解
HttpServletUtil 的典型使用场景是在过滤器层判断目标方法是否标注了特定注解:
public class CustomFilter extends OncePerRequestFilter {
private final HandlerMapping handlerMapping;
@Override
protected void doFilterInternal(
HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain) throws ServletException, IOException {
// 解析目标 Handler 方法
HandlerMethod handlerMethod =
HttpServletUtil.getHandlerMethod(request, handlerMapping);
if (handlerMethod != null) {
// 检查方法上是否标注了 @NeedPermission
NeedPermission annotation =
handlerMethod.getMethodAnnotation(NeedPermission.class);
if (annotation != null) {
// 执行权限校验逻辑...
}
}
filterChain.doFilter(request, response);
}
}在 ContextFilter 中使用
ContextFilter 使用此工具检查 @IgnoreContext 注解:
HandlerMethod handler =
HttpServletUtil.getHandlerMethod(request, handlerMapping);
if (handler != null) {
// 检查是否标注 @IgnoreContext
IgnoreContext ignore = handler.getMethodAnnotation(IgnoreContext.class);
if (ignore != null) {
filterChain.doFilter(request, response);
return;
}
}获取方法元信息
HandlerMethod handlerMethod =
HttpServletUtil.getHandlerMethod(request, handlerMapping);
if (handlerMethod != null) {
// 获取 Controller 类名和方法名
String className = handlerMethod.getBeanType().getSimpleName();
String methodName = handlerMethod.getMethod().getName();
log.info("请求目标: {}.{}", className, methodName);
// 获取方法参数类型
Class<?>[] parameterTypes = handlerMethod.getMethod().getParameterTypes();
}注意事项
- 该方法可能返回
null,调用方必须进行空值判断 HandlerMapping实例可通过 Spring 注入获取- 在高频调用场景中,
getHandlerMethod()的性能开销主要来自HandlerMapping的内部查找过程,Spring 内部已做缓存优化 - 该工具类仅适用于 Servlet 环境(Spring MVC),不适用于响应式环境(Spring WebFlux)