目录
拦截器
拦截器是Spring框架提供的核心功能之一,主要用来拦截用户的请求,通常用于处理在请求到达最终目的地之前或响应返回给客户端之前的预处理和后处理任务。
拦截器的使用分为两步:1、定义拦截器,2、注册拦截器
1、定义拦截器
需要实现HandlerInterceptor接口,重写preHandle等方法
例如:
java
@Component
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if(......){
return false;
}
return true;//true 放行, false 拦截
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}
2、注册拦截器
需要实现WebMvcConfigurer接口,重写addInterceptors方法,通过registry.addInterceptor进行注册
java
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Autowired
private LoginInterceptor loginInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(loginInterceptor)
.addPathPatterns("/test/**")//生效的路径,对test下所有的路径生效
.excludePathPatterns("/user/login");//要排除的路径(不拦截该路径)
}
}
拦截路径设置:
拦截路径 | 含义 | 举例 |
---|---|---|
/* | 一级路径 | 能匹配/user、/book、/login,不能匹配user/login |
/** | 任意级路径 | 能匹配/user、/user/login、/uesr/reg |
/book/* | /book下的一级路径 | 能匹配/book/addBook,不能匹配/book/addBook/aaa、/book |
/book/** | /book下的任意路径 | 能匹配/book、/book/addBook、/book/addBook/aaa、不能匹 配/user/login |
统一数据返回格式
实现ResponseBodyAdvice接口,重写supports、beforeBodyWrite方法,并且需要加上@ControllerAdvice注解
java
@ControllerAdvice
public class ResponseAdvice implements ResponseBodyAdvice {
@Override
public boolean supports(MethodParameter returnType, Class converterType) {
return true;//是否支持对返回结果进行统一处理,true表示支持
}
@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
//如何重写
return null;
}
}
例如:
对返回结果封装成Result类
java
@Data
public class Result<T> {
private int code; //200-成功 -1 用户未登录 -2 程序出错 业务状态码, 非http状态码
private String errMsg;
private T data;
public static <T> Result success(T data) {
Result result = new Result();
result.setCode(200);
result.setErrMsg("");
result.setData(data);
return result;
}
public static <T> Result unlogin() {
Result result = new Result();
result.setCode(-1);
result.setErrMsg("用户未登录");
return result;
}
public static <T> Result fail(T data) {
Result result = new Result();
result.setCode(-2);
result.setErrMsg("程序发生错误!");
return result;
}
}
统一数据返回格式:
java
@ControllerAdvice
public class ResponseAdvice implements ResponseBodyAdvice {
@Autowired
private ObjectMapper objectMapper;
@Override
public boolean supports(MethodParameter returnType, Class converterType) {
return true;
}
@SneakyThrows//自动抛出检查型异常
@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
if (body instanceof Result<?>) {
return body;
}
if (body instanceof String) {
//将字符串转json
return objectMapper.writeValueAsString(Result.success(body));
}
return Result.success(body);
}
}
统一异常处理
需要加上@ControllerAdvice注解,处理异常的方法加上@ExceptionHandler注解
java
@ControllerAdvice
public class ExceptionAdvice {
@ExceptionHandler
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)//设置HTTP状态码
public Object handler(Exception e) {
log.error("发生异常,e", e);
return Result.fail();
}
}
可以存在多个方法,处理多个异常
例如:
java
@Slf4j
@ControllerAdvice
@ResponseBody//返回数据,Result.fail()是数据
public class ExceptionAdvice {
@ExceptionHandler
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public Object handler(Exception e) {
log.error("发生异常,e", e);
return Result.fail();
}
@ExceptionHandler
public Object handler(ArrayIndexOutOfBoundsException e){
log.error("发生异常, e:{} ", e.getMessage());
return Result.fail("发生数组越界异常");
}
@ExceptionHandler
public Object handler(NullPointerException e){
log.error("发生异常, e: ", e);
return Result.fail("发生NullPointerException 异常");
}
@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler
public Object handler(NoResourceFoundException e){
log.error("发生异常, e: {}, path:{}", e.getDetailMessageCode(), e.getResourcePath());
return Result.fail("发生NoResourceFoundException 异常");
}
}