说明
请先根据这个示例,创建一个可以简单使用的Restfull程序,或者通过IDE来创建一个Spring Boot的程序
这里我们仅仅演示如何简单的设置拦截器,对于拦截器的概念和使用场景不做解释,以及代码的书写,目录的设计不做规范
搭建演示项目
使用IDE创建完项目之后,会默认有个DemoApplication.java的主程序入口文件,该文件默认不做改变
DemoApplication.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
添加两个文件,文件位置就在com.example.demo下就可以,文件分别为:GreetingController.java 和 Greeting.java。GreetingController.java 是一个Controller,Greeting.java是一个Model,仅仅做restfull简单的演示。文件代码分别如下
GreetingController.java
package com.example.demo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.atomic.AtomicLong;
@RestController
public class GreetingController {
private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();
@RequestMapping("/greeting")
public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
return new Greeting(counter.incrementAndGet(),
String.format(template, name));
}
}
Greeting.java
package com.example.demo;
public class Greeting {
private final long id;
private final String content;
public Greeting(long id, String content) {
this.id = id;
this.content = content;
}
public long getId() {
return id;
}
public String getContent() {
return content;
}
}
启动程序,在浏览器输入地址:http://localhost:8080/greeting,就可以在浏览器中看到如下信息:
{"id":1,"content":"Hello, World!"}
点击浏览器刷新按钮,id会自增长
引入拦截器
在请求http://localhost:8080/greeting这个地址时,我们希望对请求进行拦截
参考地址:https://www.jianshu.com/p/1e8d088c2be9
添加文件AppInterceptor.java,并让该类继承HandlerInterceptorAdapter,完整代码如下
package com.example.demo;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AppInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("***************preHandle****************");
return super.preHandle(request, response, handler);
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("***************postHandle****************");
super.postHandle(request, response, handler, modelAndView);
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("***************afterCompletion****************");
super.afterCompletion(request, response, handler, ex);
}
}
这里就是需要对拦截所做的事情
但是,现在还不能启用拦截器,需要做一些配置
添加文件WebAppConfig.java,该类实现WebMvcConfigurer接口,并实现addInterceptors()方法,在这个方法里注册刚刚我们的拦截器,完成代码如下:
package com.example.demo;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebAppConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new AppInterceptor());
}
}
此时,启动程序,请求http://localhost:8080/greeting就会在控制台打印如下信息,关于拦截器里面的方法如何使用,请自行查阅
***************preHandle****************
***************postHandle****************
***************afterCompletion****************