Spring Boot 拦截器中 handler 的几种类型
Spring Boot About 1,274 words描述
之前文章有说到:使用HandlerInterceptor中提供的Object handler参数,获取Controller的方法上的注解,并实现逻辑。
但Object handler不一定都是HandlerMethod类型,也有可能是ResourceHttpRequestHandler。
之前文章
https://www.zhangbj.com/p/1388.html
代码
@Slf4j
public class RateLimiterInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
log.info("request url#{}, method#{}", request.getRequestURI(), request.getMethod());
if (handler instanceof HandlerMethod handlerMethod) {
log.info("this is handlerMethod#{}", handlerMethod);
} else if (handler instanceof ResourceHttpRequestHandler resourceHttpRequestHandler) {
log.info("this is resourceHttpRequestHandler#{}", resourceHttpRequestHandler);
} else {
log.info("this is {}", handler);
}
return HandlerInterceptor.super.preHandle(request, response, handler);
}
}
HandlerMethod
Spring中提供了HandlerMethod类,封装了Controller的方法的相关参数信息,包括:方法名、参数、注解等元信息。
自定义的Controller、RestController通过拦截器时Object handler都会是HandlerMethod类型。
ResourceHttpRequestHandler
静态资源通过Spring请求获取时,拦截器中的handler类型就是ResourceHttpRequestHandler,而且默认的/error接口通过拦截器时Object handler类型也是ResourceHttpRequestHandler。
Views: 3,240 · Posted: 2023-02-11
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb/LiteNote扫描下方二维码关注公众号和小程序↓↓↓
Loading...