1、实现HandlerInterceptor接口,重写拦截方法。
2、添加拦截器配置类,继承自WebMvcConfigurerAdapter,重写addInterceptors方法注册拦截器。
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new RequestInfoInteceptor()).addPathPatterns("/**");
super.addInterceptors(registry);
}
简单功能实现demo:日志打印所有请求方法路径:
@Override
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
String str = arg2.toString();
System.out.println("本次请求的绝对路径为:"+str.substring(str.indexOf("com"), str.length()));
return true;
}