参考资料: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 public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
|
访问测试:http://localhost:8081/filter/test
输出:
MyFilter
测试