springboot中添加拦截器(记录)
Posted 囧囧x鑫
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot中添加拦截器(记录)相关的知识,希望对你有一定的参考价值。
参考:
If you want to take complete control of Spring MVC, you can add your own @Configuration
annotated with @EnableWebMvc
如果你想要完全自定义SpringMVC,不保留SpringBoot提供的一切特征,你可以自己定义类并且添加@Configuration
注解和@EnableWebMvc
@Component public class MyInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("preHandle method is running!"); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("postHandle method is running!"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("afterCompletion method is running!"); } }
2.定义配置类,注册拦截器
@Configuration public class MvcConfiguration implements WebMvcConfigurer { @Autowired private HandlerInterceptor myInterceptor; /** * 重写接口中的addInterceptors方法,添加自定义拦截器 * @param registry */ @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(myInterceptor).addPathPatterns("/**"); } }
3.修改下日志级别
# 设置org.springframework包的日志级别为debug
logging.level.org.springframework=debug
以上是关于springboot中添加拦截器(记录)的主要内容,如果未能解决你的问题,请参考以下文章