SpringBoot自定义拦截器和统一异常处理

一、自定义拦截器

1.创建Interceptor类

package com.yh.sboot6;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author youHui
 * SpringBoot自定义拦截器,先继承 HandlerInterceptor接口
 **/
public class Interceptor implements HandlerInterceptor {
    //实现HandlerInterceptor里的方法

    //在请求时还没到达Controller时调用(拦截一些请求地址)
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("在请求时还没到达Controller");
        return true;//表示需要请求到Controller
        //return false;//表示不去请求到Controller
    }

    //已经请求到Controller,在渲染页面之前调用
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("已经请求到Controller,在渲染页面之前");
    }

    //已经全部请求完成时调用
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("已经全部请求");
    }
}

2.创建WebConfig类

package com.yh.sboot6;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
 * @Author youHui
 * 拦截器管理类
 **/
@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //对哪些路径进行拦截
        //new Interceptor()拦截器类,在这里实例化并进行管理
        registry.addInterceptor(new Interceptor()).addPathPatterns("/s6/**");
        //.addPathPatterns()需要拦截的路径
    }
}

二、统一异常处理

package com.yh.sboot7;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author youHui
 **/
@ControllerAdvice //统一处理
public class HandleException {
    
    @ExceptionHandler(value = Exception.class)
    public Object exceptionHandler(
            HttpServletRequest request, 
            HttpServletResponse response,
            Exception e
            ) throws Exception{
        ModelAndView mv = new ModelAndView("error/500");//错误页面
        mv.addObject("message",e);
        mv.addObject("url",request.getRequestURL());
        return mv;
    }  
}

三、自定义定时任务

1.在启动类中加上@EnableScheduling

2.在使用类中的方法中添加@Scheduled

package com.yh.sboot8;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author youHui
 **/
@Component
public class Time {
    
    @Scheduled(fixedRate = 1000) //表示一秒执行一次
    public void testTime(){
        System.out.println("当前时间:"+new Date());
    }
}

你可能感兴趣的:(spring,boot,servlet,java)