说明

请先根据这个示例,创建一个可以简单使用的Restfull程序,或者通过IDE来创建一个Spring Boot的程序

这里我们仅仅演示如何简单的设置拦截器,对于拦截器的概念和使用场景不做解释,以及代码的书写,目录的设计不做规范

搭建演示项目

使用IDE创建完项目之后,会默认有个DemoApplication.java的主程序入口文件,该文件默认不做改变

DemoApplication.java

  1. package com.example.demo;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class DemoApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(DemoApplication.class, args);
  8. }
  9. }

添加两个文件,文件位置就在com.example.demo下就可以,文件分别为:GreetingController.java 和 Greeting.java。GreetingController.java 是一个Controller,Greeting.java是一个Model,仅仅做restfull简单的演示。文件代码分别如下

GreetingController.java

  1. package com.example.demo;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import org.springframework.web.bind.annotation.RequestParam;
  4. import org.springframework.web.bind.annotation.RestController;
  5. import java.util.concurrent.atomic.AtomicLong;
  6. @RestController
  7. public class GreetingController {
  8. private static final String template = "Hello, %s!";
  9. private final AtomicLong counter = new AtomicLong();
  10. @RequestMapping("/greeting")
  11. public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
  12. return new Greeting(counter.incrementAndGet(),
  13. String.format(template, name));
  14. }
  15. }

Greeting.java

  1. package com.example.demo;
  2. public class Greeting {
  3. private final long id;
  4. private final String content;
  5. public Greeting(long id, String content) {
  6. this.id = id;
  7. this.content = content;
  8. }
  9. public long getId() {
  10. return id;
  11. }
  12. public String getContent() {
  13. return content;
  14. }
  15. }

启动程序,在浏览器输入地址:http://localhost:8080/greeting,就可以在浏览器中看到如下信息:

  1. {"id":1,"content":"Hello, World!"}

点击浏览器刷新按钮,id会自增长

引入拦截器

在请求http://localhost:8080/greeting这个地址时,我们希望对请求进行拦截
参考地址:https://www.jianshu.com/p/1e8d088c2be9

添加文件AppInterceptor.java,并让该类继承HandlerInterceptorAdapter,完整代码如下

  1. package com.example.demo;
  2. import org.springframework.web.servlet.ModelAndView;
  3. import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. public class AppInterceptor extends HandlerInterceptorAdapter {
  7. @Override
  8. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  9. System.out.println("***************preHandle****************");
  10. return super.preHandle(request, response, handler);
  11. }
  12. @Override
  13. public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
  14. System.out.println("***************postHandle****************");
  15. super.postHandle(request, response, handler, modelAndView);
  16. }
  17. @Override
  18. public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
  19. System.out.println("***************afterCompletion****************");
  20. super.afterCompletion(request, response, handler, ex);
  21. }
  22. }

这里就是需要对拦截所做的事情
但是,现在还不能启用拦截器,需要做一些配置

添加文件WebAppConfig.java,该类实现WebMvcConfigurer接口,并实现addInterceptors()方法,在这个方法里注册刚刚我们的拦截器,完成代码如下:

  1. package com.example.demo;
  2. import org.springframework.context.annotation.Configuration;
  3. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
  4. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  5. @Configuration
  6. public class WebAppConfig implements WebMvcConfigurer {
  7. @Override
  8. public void addInterceptors(InterceptorRegistry registry) {
  9. registry.addInterceptor(new AppInterceptor());
  10. }
  11. }

此时,启动程序,请求http://localhost:8080/greeting就会在控制台打印如下信息,关于拦截器里面的方法如何使用,请自行查阅

  1. ***************preHandle****************
  2. ***************postHandle****************
  3. ***************afterCompletion****************