|
| 1 | +# springboot炖filter |
| 2 | +### 1. 先睹为快 |
| 3 | +### 2. 实现原理 |
| 4 | +#### 2.1 新建项目 |
| 5 | +#### 2.2 创建maven目录结构,以及pom.xml文件 |
| 6 | +#### 2.3 pom.xml文件中加入依赖 |
| 7 | +```xml |
| 8 | +<parent> |
| 9 | + <groupId>org.springframework.boot</groupId> |
| 10 | + <artifactId>spring-boot-starter-parent</artifactId> |
| 11 | + <version>2.0.5.RELEASE</version> |
| 12 | + <relativePath/> |
| 13 | +</parent> |
| 14 | +``` |
| 15 | +#### 2.4 pom.xml文件中加入springboot-starter依赖 |
| 16 | +```xml |
| 17 | +<dependencies> |
| 18 | + <dependency> |
| 19 | + <groupId>org.springframework.boot</groupId> |
| 20 | + <artifactId>spring-boot-starter-web</artifactId> |
| 21 | + </dependency> |
| 22 | +</dependencies> |
| 23 | +``` |
| 24 | +#### 2.5 pom.xml文件中加入maven-springboot打包插件 |
| 25 | +```xml |
| 26 | +<build> |
| 27 | + <plugins> |
| 28 | + <plugin> |
| 29 | + <groupId>org.springframework.boot</groupId> |
| 30 | + <artifactId>spring-boot-maven-plugin</artifactId> |
| 31 | + </plugin> |
| 32 | + </plugins> |
| 33 | +</build> |
| 34 | +``` |
| 35 | +#### 2.6 开发启动类 |
| 36 | +```java |
| 37 | +import org.springframework.boot.SpringApplication; |
| 38 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 39 | +import org.springframework.boot.web.servlet.ServletComponentScan; |
| 40 | + |
| 41 | +@ServletComponentScan |
| 42 | +@SpringBootApplication |
| 43 | +public class Application { |
| 44 | + |
| 45 | + public static void main(String[] args) { |
| 46 | + SpringApplication.run(Application.class, args); |
| 47 | + } |
| 48 | + |
| 49 | +} |
| 50 | +``` |
| 51 | +#### 2.7 开发测试接口 |
| 52 | +```java |
| 53 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 54 | +import org.springframework.web.bind.annotation.RestController; |
| 55 | + |
| 56 | +@RestController |
| 57 | +public class DemoController { |
| 58 | + |
| 59 | + @RequestMapping("/test") |
| 60 | + public Object test(String name) { |
| 61 | + return "hello " + name; |
| 62 | + } |
| 63 | + |
| 64 | + @RequestMapping("/test2") |
| 65 | + public Object test2(String name) { |
| 66 | + return "hello " + name; |
| 67 | + } |
| 68 | + |
| 69 | +} |
| 70 | +``` |
| 71 | +#### 2.8 开发过滤器 |
| 72 | +```java |
| 73 | +import javax.servlet.*; |
| 74 | +import javax.servlet.annotation.WebFilter; |
| 75 | +import java.io.IOException; |
| 76 | + |
| 77 | +@WebFilter(filterName = "myFilter", urlPatterns = "/test/*") |
| 78 | +public class MyFilter implements Filter { |
| 79 | + |
| 80 | + @Override |
| 81 | + public void init(FilterConfig filterConfig) { |
| 82 | + System.out.println("初始化过滤器。。。"); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { |
| 87 | + System.out.println("进入到过滤器。。。"); |
| 88 | + String name = servletRequest.getParameter("name"); |
| 89 | + if ("Oven".equals(name)) { |
| 90 | + filterChain.doFilter(servletRequest, servletResponse); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void destroy() { |
| 96 | + System.out.println("过滤器销毁。。。"); |
| 97 | + } |
| 98 | + |
| 99 | +} |
| 100 | +``` |
| 101 | +#### 2.9 编译打包运行 |
| 102 | +### 3. 应用场景 |
0 commit comments