Spring Boot 教程: 拦截器

在以下两种情况下,可以在 Spring Boot 中使用拦截器来执行操作:
-
发送请求到控制器之前
-
发送响应到客户端之前
譬如,可以使用拦截器在发送请求到控制器之前添加请求头,并在发送响应到客户端之前添加响应头。
要用拦截器,你需要创建 @Component 类,并且要实现 HandlerInterceptor 接口。
下面是使用拦截器要知道的三个方法:
-
preHandle() 方法:用于在发送请求到控制器之前执行操作。这个方法应当返回 true 以响应客户端。
-
postHandle() 方法:用于在发送响应到客户端之前执行操作。
-
afterCompletion() 方法: 用于在请求和响应完成之后执行操作。
观察以下代码以加深理解:
1 @Component 2 public class ProductServiceInterceptor implements HandlerInterceptor { 3 @Override 4 public boolean preHandle( 5 HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { 6 7 return true; 8 } 9 @Override 10 public void postHandle( 11 HttpServletRequest request, HttpServletResponse response, Object handler, 12 ModelAndView modelAndView) throws Exception {} 13 14 @Override 15 public void afterCompletion(HttpServletRequest request, HttpServletResponse response, 16 Object handler, Exception exception) throws Exception {} 17 }
还要通过 WebMvcConfigurerAdapter 用 InterceptorRegistry 注册这个拦截器:
1 @Component 2 public class ProductServiceInterceptorAppConfig extends WebMvcConfigurerAdapter { 3 @Autowired 4 ProductServiceInterceptor productServiceInterceptor; 5 6 @Override 7 public void addInterceptors(InterceptorRegistry registry) { 8 registry.addInterceptor(productServiceInterceptor); 9 } 10 }
在下面给出的示例中,我们将要使用后面给出的 GET products API:
Interceptor 类文件 ProductServiceInterceptor.java 如下所示:
1 package com.tutorialspoint.demo.interceptor; 2 3 import javax.servlet.http.HttpServletRequest; 4 import javax.servlet.http.HttpServletResponse; 5 6 import org.springframework.stereotype.Component; 7 import org.springframework.web.servlet.HandlerInterceptor; 8 import org.springframework.web.servlet.ModelAndView; 9 10 @Component 11 public class ProductServiceInterceptor implements HandlerInterceptor { 12 @Override 13 public boolean preHandle 14 (HttpServletRequest request, HttpServletResponse response, Object handler) 15 throws Exception { 16 17 System.out.println("Pre Handle method is Calling"); 18 return true; 19 } 20 @Override 21 public void postHandle(HttpServletRequest request, HttpServletResponse response, 22 Object handler, ModelAndView modelAndView) throws Exception { 23 24 System.out.println("Post Handle method is Calling"); 25 } 26 @Override 27 public void afterCompletion 28 (HttpServletRequest request, HttpServletResponse response, Object 29 handler, Exception exception) throws Exception { 30 31 System.out.println("Request and Response is completed"); 32 } 33 }
Application Configuration 类文件把拦截器注册到 Interceptor Registry中,ProductServiceInterceptorAppConfig.java 文件如下:
1 package com.tutorialspoint.demo.interceptor; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.stereotype.Component; 5 import org.springframework.web.servlet.config.annotation.InterceptorRegistry; 6 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 7 8 @Component 9 public class ProductServiceInterceptorAppConfig extends WebMvcConfigurerAdapter { 10 @Autowired 11 ProductServiceInterceptor productServiceInterceptor; 12 13 @Override 14 public void addInterceptors(InterceptorRegistry registry) { 15 registry.addInterceptor(productServiceInterceptor); 16 } 17 }
Controller 类文件 ProductServiceController.java 如下:
1 package com.tutorialspoint.demo.controller; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 import org.springframework.http.HttpStatus; 7 import org.springframework.http.ResponseEntity; 8 import org.springframework.web.bind.annotation.PathVariable; 9 import org.springframework.web.bind.annotation.RequestBody; 10 import org.springframework.web.bind.annotation.RequestMapping; 11 import org.springframework.web.bind.annotation.RequestMethod; 12 import org.springframework.web.bind.annotation.RestController; 13 import com.tutorialspoint.demo.exception.ProductNotfoundException; 14 import com.tutorialspoint.demo.model.Product; 15 16 @RestController 17 public class ProductServiceController { 18 private static Map<String, Product> productRepo = new HashMap<>(); 19 static { 20 Product honey = new Product(); 21 honey.setId("1"); 22 honey.setName("Honey"); 23 productRepo.put(honey.getId(), honey); 24 Product almond = new Product(); 25 almond.setId("2"); 26 almond.setName("Almond"); 27 productRepo.put(almond.getId(), almond); 28 } 29 @RequestMapping(value = "/products") 30 public ResponseEntity<Object> getProduct() { 31 return new ResponseEntity<>(productRepo.values(), HttpStatus.OK); 32 } 33 }
POJO 类文件 Product.java 如下:
1 package com.tutorialspoint.demo.model; 2 3 public class Product { 4 private String id; 5 private String name; 6 7 public String getId() { 8 return id; 9 } 10 public void setId(String id) { 11 this.id = id; 12 } 13 public String getName() { 14 return name; 15 } 16 public void setName(String name) { 17 this.name = name; 18 } 19 }
主 Spring Boot 应用类文件 DemoApplication.java 如下所示:
1 package com.tutorialspoint.demo; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 6 @SpringBootApplication 7 public class DemoApplication { 8 public static void main(String[] args) { 9 SpringApplication.run(DemoApplication.class, args); 10 } 11 }
Maven build – pom.xml 文件在此:
1 <?xml version = "1.0" encoding = "UTF-8"?> 2 <project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = " 3 http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 5 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 6 7 <modelVersion>4.0.0</modelVersion> 8 <groupId>com.tutorialspoint</groupId> 9 <artifactId>demo</artifactId> 10 <version>0.0.1-SNAPSHOT</version> 11 <packaging>jar</packaging> 12 <name>demo</name> 13 <description>Demo project for Spring Boot</description> 14 15 <parent> 16 <groupId>org.springframework.boot</groupId> 17 <artifactId>spring-boot-starter-parent</artifactId> 18 <version>1.5.8.RELEASE</version> 19 <relativePath/> 20 </parent> 21 22 <properties> 23 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 24 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 25 <java.version>1.8</java.version> 26 </properties> 27 28 <dependencies> 29 <dependency> 30 <groupId>org.springframework.boot</groupId> 31 <artifactId>spring-boot-starter-web</artifactId> 32 </dependency> 33 34 <dependency> 35 <groupId>org.springframework.boot</groupId> 36 <artifactId>spring-boot-starter-test</artifactId> 37 <scope>test</scope> 38 </dependency> 39 </dependencies> 40 41 <build> 42 <plugins> 43 <plugin> 44 <groupId>org.springframework.boot</groupId> 45 <artifactId>spring-boot-maven-plugin</artifactId> 46 </plugin> 47 </plugins> 48 </build> 49 50 </project>
Gradle Build build.gradle 文件在此:
1 buildscript { 2 ext { 3 springBootVersion = '1.5.8.RELEASE' 4 } 5 repositories { 6 mavenCentral() 7 } 8 dependencies { 9 classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 10 } 11 } 12 apply plugin: 'java' 13 apply plugin: 'eclipse' 14 apply plugin: 'org.springframework.boot' 15 group = 'com.tutorialspoint' 16 version = '0.0.1-SNAPSHOT' 17 sourceCompatibility = 1.8 18 19 repositories { 20 mavenCentral() 21 } 22 dependencies { 23 compile('org.springframework.boot:spring-boot-starter-web') 24 testCompile('org.springframework.boot:spring-boot-starter-test') 25 }
你可以使用下面的 Maven 或 Gradle 命令创建可执行的 JAR 文件,运行 Spring Boot 应用:
对于 Maven,使用以下命令:
mvn clean install
在 “BUILD SUCCESS” 之后,你可以在 target 目录下找到 JAR 文件:
对于 Gradle,使用以下命令:
gradle clean build
在 “BUILD SUCCESSFUL” 之后,你可以在 build/libs 目录下找到 JAR 文件:
可以使用以下命令运行 JAR 文件:
java –jar <JARFILE>
此时,应用已经在 Tomcat 8080 端口启动了,如下所示:

在 POSTMAN 应用中单击以下 URL,你可以看到下面的结果:
GET API: http://localhost:8080/products

在控制台窗口,你可以看到加在拦截器中的 System.out.println 打印出来的结果,截屏如下:


浙公网安备 33010602011771号