04_SpringBoot 过滤器+监听器

image-20200708160944615

参考资料:https://www.springcloud.cc/spring-boot.html

中文文档2:https://felord.cn/_doc/_springboot/2.1.5.RELEASE/_book/index.html

SpringBoot 中 Filter 过滤器、Listener 监听器用法相同

1. 创建过滤器

1
2
3
4
5
6
7
8
9
10
11
12
13
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

@WebFilter("/filter/*")
public class MyFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("MyFilter");
filterChain.doFilter(servletRequest, servletResponse);
}
}

2. 测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/filter")
public class FilterController {

@RequestMapping("/test")
public String test() {
System.out.println("测试");
return "success";
}
}

3. 启动类添加注解

  • @ServletComponentScan 包含 Servlet、Filter、Listener 可以直接通过@WebServlet、@WebFilter、@WebListener注解自动注册
1
2
3
4
5
6
7
8
9
10
11
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@SpringBootApplication
@ServletComponentScan // SpringBoot 中扫描 @WebServlet、@WebFilter、@WebListener注解
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

访问测试:http://localhost:8081/filter/test

image-20200708224200807

输出:

MyFilter
测试


04_SpringBoot 过滤器+监听器
https://janycode.github.io/2018/06/20/08_框架技术/04_SpringBoot/04_SpringBoot 过滤器+监听器/
作者
Jerry(姜源)
发布于
2018年6月20日
许可协议