Java Interceptor中使用HttpServletRequest

本文详细介绍了在SpringMVC中如何通过在web.xml中添加listener来处理请求,并展示了如何在interceptor中注入HttpServletRequest。同时,对于SpringBoot环境下,讲解了如何在interceptor中获取HttpServletRequest,通过RequestAttributes和RequestContextHolder实现。
  • SpringMvc中
  1. web.xml 添加listener
	<listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
  1. interceptor中注解注入request即可
    @Autowired
    HttpServletRequest request;

` SpringBoot中
intercepter中需要使用的地方直接插入

RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);
### Java Spring Boot Interceptor 使用示例 #### 创建拦截器类 为了创建一个自定义的拦截器,在Spring Boot应用程序中可以继承`HandlerInterceptorAdapter`类并重写相应的方法: ```java import org.springframework.web.servlet.HandlerInterceptor; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class MyCustomInterceptor extends HandlerInterceptorAdapter { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("Before handling the controller..."); // 自定义逻辑处理前的操作 return true; // 如果返回false,则不会继续执行后续操作 } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("After handling but before rendering view..."); // 处理完成后视图渲染之前的操作 } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("After completing the entire request processing..."); // 完成整个请求后的清理工作 } } ``` 此代码展示了如何通过覆盖三个主要方法来控制HTTP请求的不同阶段的行为[^1]。 #### 注册拦截器 为了让上述自定义拦截器生效,还需要将其注册到配置文件中。可以通过实现`WebMvcConfigurer`接口来进行设置: ```java import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class WebConfig implements WebMvcConfigurer { private final MyCustomInterceptor myCustomInterceptor; public WebConfig(MyCustomInterceptor myCustomInterceptor){ this.myCustomInterceptor = myCustomInterceptor; } @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(myCustomInterceptor).addPathPatterns("/api/**"); } } ``` 这里指定了路径匹配规则 `/api/**` ,意味着只有当访问URL以`/api/`开头时才会触发该拦截器的工作流程[^3]。 #### 测试控制器 最后编写简单的测试Controller用于验证拦截器的效果: ```java @RestController @RequestMapping("/api") public class TestController { @GetMapping("/test") public String test() { return "This is a test endpoint."; } } ``` 当调用`GET /api/test`端点时,应该可以在日志中看到由MyCustomInterceptor打印的信息,证明拦截器已经成功介入到了请求的过程中.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值