|
| 1 | +# springboot炖interceptor |
| 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 | + |
| 40 | +@SpringBootApplication |
| 41 | +public class Application { |
| 42 | + |
| 43 | + public static void main(String[] args) { |
| 44 | + SpringApplication.run(Application.class, args); |
| 45 | + } |
| 46 | + |
| 47 | +} |
| 48 | +``` |
| 49 | +#### 2.7 开发测试接口 |
| 50 | +```java |
| 51 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 52 | +import org.springframework.web.bind.annotation.RestController; |
| 53 | + |
| 54 | +@RestController |
| 55 | +public class DemoController { |
| 56 | + |
| 57 | + @RequestMapping("/test") |
| 58 | + public Object test(String name) { |
| 59 | + return "hello " + name; |
| 60 | + } |
| 61 | + |
| 62 | + @RequestMapping("/test2") |
| 63 | + public Object test2(String name) { |
| 64 | + return "hello " + name; |
| 65 | + } |
| 66 | + |
| 67 | +} |
| 68 | +``` |
| 69 | +#### 2.8 开发拦截器 |
| 70 | +```java |
| 71 | +import org.springframework.stereotype.Component; |
| 72 | +import org.springframework.web.servlet.HandlerInterceptor; |
| 73 | +import org.springframework.web.servlet.ModelAndView; |
| 74 | + |
| 75 | +import javax.servlet.http.HttpServletRequest; |
| 76 | +import javax.servlet.http.HttpServletResponse; |
| 77 | + |
| 78 | +@Component |
| 79 | +public class MyInterceptor implements HandlerInterceptor { |
| 80 | + |
| 81 | + @Override |
| 82 | + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { |
| 83 | + System.out.println("调用接口之前。。。"); |
| 84 | + String name = request.getParameter("name"); |
| 85 | + return "Oven".equals(name); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) { |
| 90 | + System.out.println("调用接口之后。。。"); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { |
| 95 | + System.out.println("请求处理完之后。。。"); |
| 96 | + } |
| 97 | + |
| 98 | +} |
| 99 | +``` |
| 100 | +#### 2.9 开发拦截器配置 |
| 101 | +```java |
| 102 | +import com.oven.interceptor.MyInterceptor; |
| 103 | +import org.springframework.context.annotation.Configuration; |
| 104 | +import org.springframework.web.servlet.config.annotation.InterceptorRegistration; |
| 105 | +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; |
| 106 | +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
| 107 | + |
| 108 | +import javax.annotation.Resource; |
| 109 | + |
| 110 | +@Configuration |
| 111 | +public class InterceptorConfig implements WebMvcConfigurer { |
| 112 | + |
| 113 | + @Resource |
| 114 | + private MyInterceptor interceptor; |
| 115 | + |
| 116 | + @Override |
| 117 | + public void addInterceptors(InterceptorRegistry registry) { |
| 118 | + InterceptorRegistration registration = registry.addInterceptor(interceptor); |
| 119 | + registration.addPathPatterns("/test/**"); |
| 120 | + } |
| 121 | + |
| 122 | +} |
| 123 | +``` |
| 124 | +#### 2.10 编译打包运行 |
| 125 | +### 3. 应用场景 |
0 commit comments