Java / Java web · 2020年8月12日 0

Spring boot 自定义处理404 500等

Spring boot 自定义处理404 500等

有时候我们会遇到一些问题,需要我们自定义一些异常,例如404、403、502、500等,我们这里就说说Springboot 修改自定义这些。

处理方式一

我们重写ErrorController接口,重写handleError方法

import javax.servlet.http.HttpServletRequest;

import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class MainsiteErrorController implements ErrorController{


  @RequestMapping("/error")
    public ModelAndView handleError(HttpServletRequest request,HttpServletResponse response){
        //获取statusCode:401,404,500
        //Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
      Integer statusCode =response.getStatus();
        ModelAndView model = new ModelAndView();
        if(statusCode == 401){
          model.setViewName("401.html");
        }else if(statusCode == 404){
          model.setViewName("404.html");
        }else if(statusCode == 403){
          model.setViewName("403.html");
        }else{
          model.setViewName("500.html");
        }
        return model;
    }
    @Override
    public String getErrorPath() {
      return "/error";
    }

}

这里可以使用

request.getAttribute("javax.servlet.error.status_code")或者response.getStatus()

使用哪个取决于你个人处理

如果你在你自己的业务逻辑中自己通过 response.sendError(404)(或者用request的转发处理)来自定义处理,就有用response.getStatus()处理

至于request.getAttribute("javax.servlet.error.status_code")笔者没有使用(请小伙伴们自己试试)

方式二

img

这种笔者也没有试试,小伙伴们可以试试

方式三

springboot 在 BasicErrorController 类里实现了默认的错误处理。只需要将对应的错误提示文件放到 resources/static/error 目录,支持模糊匹配,如

static/error/4xx.html
static/error/5xx.html
static/error/404.html
static/error/error.html

在这里插入图片描述

这这完全交由系统处理

方式四

这种方式原理就是我们通过ErrorPage自定义错误界面,这个配置的实际上就是我们的访问路径,这个路径需要我们自己通过Controller配置(和配置普通的方法一样,只是这个方法的名字是404或者其他)

  1. 添加AppErrorPagesConfiguration

    @Configuration
    public class AppErrorPagesConfiguration {
    
        @Bean
        public MyErrorPageRegistrar errorPageRegistrar(){
            return new MyErrorPageRegistrar();
        }
    
        private class MyErrorPageRegistrar implements ErrorPageRegistrar {
            @Override
            public void registerErrorPages(ErrorPageRegistry errorPageRegistry) {
                ErrorPage page404 = new ErrorPage(HttpStatus.NOT_FOUND, "/404");
                ErrorPage page500 = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500");
    
                errorPageRegistry.addErrorPages(page404, page500);
            }
        }
    

    这里面的404 和500 实际上就是我们自定义的访问路径

    HttpStatus是系统提供给我们的错误码,我们可以根据自己项目需求,使用里面的错误码

  2. 添加AppErrorController

    @Controller
    public class AppErrorController{
    
        @RequestMapping(value = "/404",produces = {"text/html"})
        public ModelAndView errorPage404(){
            return new ModelAndView("/404");
        }
    
        @RequestMapping("/404")
        @ResponseBody
        public ResponseEntity error404(){
            return ResponseEntity.status(404).build();
        }
    
        @RequestMapping(value="/500",produces = {"text/html"})
        public ModelAndView errorPage500(){
            return new ModelAndView("/500");
        }
    
        @RequestMapping(value = "/500")
        @ResponseBody
        public ResponseEntity error500(){
            return ResponseEntity.status(500).build();
        }
    

    这个和普通的Controller没有区别

Share this: