zoukankan      html  css  js  c++  java
  • 014-Spring Boot web【三】拦截器HandlerInterceptor、异常处理页面,全局异常处理ControllerAdvice

    一、拦截器HandlerInterceptor

    1.1、HandlerInterceptor接口说明

      preHandle,congtroller执行前,如果返回false请求终端

      postHandle,controller执行之后,页面渲染前

      afterCompletion,整个请求结束后,页面也渲染完毕,一般是资源清理操作

      同时提供异步拦截器AsyncHandlerInterceptor

    1.2、拦截器使用步骤

      1》写一个拦截器,实现HandlerInterceptor 接口

      2》写一个类,继承WebMvcConfigurerAdapter抽象类,然后重写addInterceptors方法,把上一步的拦截器加入registry.addInterceptor(new LogHandlerInterceptor());

    1.3、示例

      启动类

    package com.lhx.spring.springboot_web;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.ConfigurableApplicationContext;
    
    @SpringBootApplication
    public class App {
        public static void main(String[] args) {
            SpringApplication.run(App.class, args);
        }
    }
    View Code

      建立UserController

    package com.lhx.spring.springboot_web;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    
    @Controller
    public class UserController {
    
        @GetMapping(value = "/user/home")
        public String home() {
            System.out.println("--------user home--------");
            return "user home";
        }    
    }
    View Code

      增加拦截器LogHandlerInterceptor 

    package com.lhx.spring.springboot_web;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;
    
    public class LogHandlerInterceptor implements HandlerInterceptor {
    
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
                throws Exception {
            System.out.println("----------preHandle----------"+handler.getClass());
            
            
            return true;
        }
    
        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                ModelAndView modelAndView) throws Exception {
            System.out.println("----------postHandle----------");
    
        }
    
        @Override
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
                throws Exception {
            System.out.println("----------afterCompletion----------");
    
        }
    
    }
    View Code

      增加配置类WebConfiguration 

    package com.lhx.spring.springboot_web;
    
    import org.springframework.boot.SpringBootConfiguration;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    
    @SpringBootConfiguration
    public class WebConfiguration extends WebMvcConfigurerAdapter {
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(new LogHandlerInterceptor());
        }
    
    }
    View Code

    二、异常处理

    2.1、编码抛出异常代码

        @GetMapping(value = "/user/help")
        @ResponseBody
        public String help() {
            throw new IllegalArgumentException("args is empty");
        }    
    View Code

    2.2、分析代码

      查看:org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration,发现默认已经被springboot定制。

      去除springboot默认异常显示,只需在启动类添加:@SpringBootApplication(exclude=ErrorMvcAutoConfiguration.class)即可。则返回错误就会变成Tomcat提供的

    2.3、添加自定义错误页

      1》去除springboot默认的:@SpringBootApplication(exclude=ErrorMvcAutoConfiguration.class)

      2》在src/main/resources下增加public,在pulic中增加404.html,500.html

      3》增加CommonErrorPageRegistry实现ErrorPageRegistrar增加@Component注解,可以针对错误码处理或者具体异常处理

    package com.lhx.spring.springboot_web;
    
    import org.springframework.boot.web.servlet.ErrorPage;
    import org.springframework.boot.web.servlet.ErrorPageRegistrar;
    import org.springframework.boot.web.servlet.ErrorPageRegistry;
    import org.springframework.http.HttpStatus;
    import org.springframework.stereotype.Component;
    
    @Component
    public class CommonErrorPageRegistry implements ErrorPageRegistrar {
    
        @Override
        public void registerErrorPages(ErrorPageRegistry registry) {
            ErrorPage e404 = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html");
            ErrorPage e500 = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html");
            ErrorPage args = new ErrorPage(IllegalArgumentException.class, "/args.html");
            registry.addErrorPages(e404);
            registry.addErrorPages(e500);
            registry.addErrorPages(args);
        }
    
    }
    View Code

        以上将404,500逻辑添加

     三、全局异常处理

    3.1、局部异常处理

      当前Controller使用,可以在当前Controller中使用ExceptionHandler注解

    如下代码

    package com.lhx.spring.springboot_web;
    
    import java.io.FileNotFoundException;
    
    import org.springframework.web.bind.annotation.ExceptionHandler;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class BookController {
        
        @ExceptionHandler(value = Exception.class)
        public String error(Exception e) {
            return "fount exception:"+e.getMessage();
        }
        
        @GetMapping("book/error1")
        public String error1() throws FileNotFoundException {
            throw new FileNotFoundException("book not found");
        }
        @GetMapping("book/error2")
        public String error2() throws ClassNotFoundException {
            throw new ClassNotFoundException("class not found");
        }
    }
    View Code

      其中:@ExceptionHandler(value = Exception.class) 代表所有;也可指代具体异常,如文件没有发现@ExceptionHandler(value = FileNotException.class)

    3.2、全局异常处理

      1》写一个GlobalExceptionHandler异常处理类,并且使用@ControllerAdvice注解

      2》写一个方法,需要添加@ExceptionHandler(value = Exception.class)注解,在方法中编写具体代码逻辑

    package com.lhx.spring.springboot_web;
    
    import org.springframework.web.bind.annotation.ControllerAdvice;
    import org.springframework.web.bind.annotation.ExceptionHandler;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @ControllerAdvice
    public class GlobalExceptionHandler {
    
        @ExceptionHandler(value = Exception.class)
        @ResponseBody
        public String errorHandler(Exception e) {
            return "global error:" + e.getClass().getName();
        }
    }
    View Code

      可以有多个不同的异常处理。

    3.3、就近原则

      局部异常优先级高于全局异常处理

    一个比较通用的写法

    @ControllerAdvice
    @ResponseBody
    public class ResponseExceptionHandler {
        Logger logger=LoggerFactory.getLogger(ResponseExceptionHandler.class);
    
        @ExceptionHandler(ResponseException.class)
        public ResponseEntity<ResponseResult> handleException(Exception e) {
            logger.error("请求异常信息",e);
            ResponseException cex = (ResponseException) e;
            return new ResponseEntity(ResponseResult.error(cex.getResponseCode()), HttpStatus.OK);
        }
    
    }
  • 相关阅读:
    VSTO不能创建OFFICE 文档项目的原因
    vs2016 创建 vsto excel 文件项目的一个问题
    一个开发原则:永远不要返回NULL
    客户为什么习惯变更需求
    从实际项目中的一个改进细节谈程序的易用性优化
    第三方系统打开EAFC的实现
    功能间(两个form)数据交互的编程方法
    关于行军模式大批量数据的审批的实现
    程序的升级发布管理
    转:从如何判断浮点数是否等于0说起——浮点数的机器级表示 献给依然 if ( double i ==0.00)的菜鸟们
  • 原文地址:https://www.cnblogs.com/bjlhx/p/8666653.html
Copyright © 2011-2022 走看看