在SpringBoot中使用 拦截器
来源:互联网 发布:少儿编程前景 编辑:程序博客网 时间:2024/06/05 12:44
在 SpringBoot中使用拦截器
拦截器接口 HandlerInterceptor
public interface HandlerInterceptor { boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception; void postHandle( HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception; void afterCompletion( HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception;}
实现自定义的拦截器需要实现HandlerInterceptor接口
例如:LogHandlerInteceptor
public class LogHandlerInterceptor implements HandlerInterceptor { /** * controller 执行之前调用 */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("------preHandle-----"); return true; } /** * controller 执行之后,且页面渲染之前调用 */ @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("------postHandle-----"); } /** * 页面渲染之后调用,一般用于资源清理操作 */ @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("------afterCompletion-----"); }}
注册拦截器
@Configurationpublic class MyWebConfig extends WebMvcConfigurerAdapter{ /** * 注册 拦截器 */ @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new LogHandlerInterceptor()); }}
测试:
UserController
@RestControllerpublic class UserController { @GetMapping("/user/home") public String home(){ System.out.println("--- user home ---"); return "user home"; }}
阅读全文
0 0
- 在SpringBoot中使用 拦截器
- 在SpringBoot中使用 拦截器
- springboot 中拦截器的使用
- springboot使用拦截器
- SpringBoot拦截器的使用
- SpringBoot拦截器的使用
- 使用springBoot的拦截器功能介绍
- springboot 配置使用拦截器 interceptor
- SpringBoot学习笔记之拦截器使用
- 在struts1.2中使用拦截器
- 在DefaultAnnotationHandlerMapping中使用拦截器
- springboot 拦截器配置
- SpringBoot--实现拦截器
- springboot添加拦截器
- springboot添加拦截器
- SpringBoot拦截器
- SpringBoot 自定义拦截器
- SpringBoot-拦截器
- iptables规则的查看、添加、删除和修改
- web开发第四课数据库操作
- C#编写的计算器程序(仿windows自带的计算器)
- AndroidUtils(六)SP工具类
- 371. Sum of Two Integers, 不用加减乘除运算符计算两数之和
- 在SpringBoot中使用 拦截器
- 字典的数据采集模块
- 阿里四面面经
- springmvc整合freemarker
- Quartz延时任务实现
- ubuntu 16.04 mysql 安装问题1045
- Android Studio 自定义设置注释模板
- 举例理解Hibernate的三种状态:瞬时态、持久态、托管(即游离态)态及互相转化
- VTK + QT + VS 编译安装配置