springboot攔截器,異常統一接收

一:攔截器

package com.liujiang.interceptor;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 *
 * @author isaac
 * @since 1.0.0
 */
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 可添加多個
        registry.addInterceptor(getTestFilter()).addPathPatterns("/**");
    }

    @Bean
    public TestInterceptor getTestFilter() {
        return new TestInterceptor();
    }

}

 

package com.liujiang.interceptor;

import com.liujiang.util.exception.InterfaceException;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author isaac
 * @since 1.0.0
 */
@Configuration
public class TestInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws InterfaceException {
        System.out.println("preHandle111111111");
        //return true;
        throw new InterfaceException(-1,"全局異常測試");
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
        System.out.println("preHandle222222222");
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
        System.out.println("preHandle333333333");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        System.out.println("preHandle444444444");
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
        System.out.println("preHandle555555555");
    }

}

二:異常統一接收

package com.liujiang.util.exception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 統一處理InterfaceException
 */
@ControllerAdvice
public class AdviceConfig{

/**
     * 應用到所有@RequestMapping註解方法,在其執行之前初始化數據綁定器
     * 在需要全局註冊時比較有用
     *
     * @param webDataBinder Web請求數據
     */
    @SuppressWarnings("unused")
    @InitBinder
    public void initBinder(WebDataBinder webDataBinder) {
        //nothing to do
    }

/**
     * 綁定KV到Model中,使全局@RequestMapping可以獲取到該值
     *
     * @param model Controller調用的入參Model
     */
    @ModelAttribute
    public void addAttributes(Model model) {
        model.addAttribute("author", "LiuJiang");
    }

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public ServiceResult<String> commonErrorHandler(Exception e) {
        logger.error("Exception:{}", ExceptionUtils.getStackTrace(e));
        ServiceResult<String> result = ServiceResult.fail(ServiceCode.DEFAULT.getCode(),e.toString(), ExceptionUtils.getStackTrace(e));
        return result;
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章