SpringBoot第十四篇:统一异常处理

包子也沉默 2019-06-06 1.05 K阅读 0评论

温馨提示:这篇文章已超过1391天没有更新,请注意相关的内容是否还可用!

作者:追梦1819
原文:https://www.cnblogs.com/yanfei1819/p/10984081.html
版权声明:本文为博主原创文章,转载请附上博文链接!

rror(){System.out.println("自定义客户端异常");thrownewUserNotExistException();}启动项目,看到结果是:总结  异常处理


<h2>timestamp:[[${timestamp}]]</h2><h2>exception:[[${exception}]]</h2><h2

quot;+viewName;TemplateAvailabilityProviderprovider=this.templateAvailabilityProviders.getProvider(e

quest,MediaType.ALL));HttpStatusstatus=getStatus(request);returnnewResponseEntity<>(body,statu

Object>model=Collections.unmodifiableMap(getErrorAttributes(request,isIncludeStackTrace(request,M

/error";//(web.xml注册的错误页面规则)publicStringgetPath(){returnthis.path;}此时会被BasicErrorController处理:@

/error";//(web.xml注册的错误页面规则)publicStringgetPath(){returnthis.path;}此时会被BasicErrorController处理:@

/error";//(web.xml注册的错误页面规则)publicStringgetPath(){returnthis.path;}此时会被BasicErrorController处理:@

引言

  本文将谈论 SpringBoot 的默认错误处理机制,以及如何自定义错误响应。

/error";//(web.xml注册的错误页面规则)publicStringgetPath(){returnthis.path;}此时会被BasicErrorController处理:@


<h2>timestamp:[[${timestamp}]]</h2><h2>exception:[[${exception}]]</h2><h2

quot;+viewName;TemplateAvailabilityProviderprovider=this.templateAvailabilityProviders.getProvider(e

quest,MediaType.ALL));HttpStatusstatus=getStatus(request);returnnewResponseEntity<>(body,statu

Object>model=Collections.unmodifiableMap(getErrorAttributes(request,isIncludeStackTrace(request,M

/error";//(web.xml注册的错误页面规则)publicStringgetPath(){returnthis.path;}此时会被BasicErrorController处理:@

/error";//(web.xml注册的错误页面规则)publicStringgetPath(){returnthis.path;}此时会被BasicErrorController处理:@

/error";//(web.xml注册的错误页面规则)publicStringgetPath(){returnthis.path;}此时会被BasicErrorController处理:@

版本信息

  • JDK:1.8
  • SpringBoot :2.1.4.RELEASE
  • maven:3.3.9
  • Thymelaf:2.1.4.RELEASE
  • IDEA:2019.1.1


<h2>timestamp:[[${timestamp}]]</h2><h2>exception:[[${exception}]]</h2><h2

quot;+viewName;TemplateAvailabilityProviderprovider=this.templateAvailabilityProviders.getProvider(e

quest,MediaType.ALL));HttpStatusstatus=getStatus(request);returnnewResponseEntity<>(body,statu

Object>model=Collections.unmodifiableMap(getErrorAttributes(request,isIncludeStackTrace(request,M

/error";//(web.xml注册的错误页面规则)publicStringgetPath(){returnthis.path;}此时会被BasicErrorController处理:@

/error";//(web.xml注册的错误页面规则)publicStringgetPath(){returnthis.path;}此时会被BasicErrorController处理:@

/error";//(web.xml注册的错误页面规则)publicStringgetPath(){returnthis.path;}此时会被BasicErrorController处理:@

默认错误响应

  我们新建一个项目,先来看看 SpringBoot 的默认响应式什么:

/error";//(web.xml注册的错误页面规则)publicStringgetPath(){returnthis.path;}此时会被BasicErrorController处理:@

首先,引入 maven 依赖:

时候可以产出。一些名词:PM产品经理PD产品设计师PRD 需求文档 设计阶段:到了设计阶段,一般由设计组织项目成员开会,进行交互稿的处理和项目的排期,这就是交互评审会议。会议结束之

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

然后,写一个请求接口:

时候可以产出。一些名词:PM产品经理PD产品设计师PRD 需求文档 设计阶段:到了设计阶段,一般由设计组织项目成员开会,进行交互稿的处理和项目的排期,这就是交互评审会议。会议结束之

package com.yanfei1819.customizeerrordemo.web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by 追梦1819 on 2019-05-09.
 */
@Controller
public class DefaultErrorController {
    @GetMapping("/defaultViewError")
    public void defaultViewError(){
        System.out.println("默认页面异常");
    }
    @ResponseBody
    @GetMapping("/defaultDataError")
    public void defaultDataError(){
        System.out.println("默认的客户端异常");
    }
}

随意访问一个8080端口的地址,例如 http://localhost:8080/a ,如下效果:

时候可以产出。一些名词:PM产品经理PD产品设计师PRD 需求文档 设计阶段:到了设计阶段,一般由设计组织项目成员开会,进行交互稿的处理和项目的排期,这就是交互评审会议。会议结束之

  1. 浏览器访问,返回一个默认页面

    时候可以产出。一些名词:PM产品经理PD产品设计师PRD 需求文档 设计阶段:到了设计阶段,一般由设计组织项目成员开会,进行交互稿的处理和项目的排期,这就是交互评审会议。会议结束之

    Object>model=Collections.unmodifiableMap(getErrorAttributes(request,isIncludeStackTrace(request,M

  2. 其它的客户端访问,返回确定的json字符串

    Object>model=Collections.unmodifiableMap(getErrorAttributes(request,isIncludeStackTrace(request,M

Object>model=Collections.unmodifiableMap(getErrorAttributes(request,isIncludeStackTrace(request,M

  以上是SpringBoot 默认的错误响应页面和返回值。不过,在实际项目中,这种响应对用户来说并不友好。通常都是开发者自定义异常页面和返回值,使其看起来更加友好、更加舒适。

Object>model=Collections.unmodifiableMap(getErrorAttributes(request,isIncludeStackTrace(request,M


<h2>timestamp:[[${timestamp}]]</h2><h2>exception:[[${exception}]]</h2><h2

quot;+viewName;TemplateAvailabilityProviderprovider=this.templateAvailabilityProviders.getProvider(e

quest,MediaType.ALL));HttpStatusstatus=getStatus(request);returnnewResponseEntity<>(body,statu

Object>model=Collections.unmodifiableMap(getErrorAttributes(request,isIncludeStackTrace(request,M

/error";//(web.xml注册的错误页面规则)publicStringgetPath(){returnthis.path;}此时会被BasicErrorController处理:@

/error";//(web.xml注册的错误页面规则)publicStringgetPath(){returnthis.path;}此时会被BasicErrorController处理:@

/error";//(web.xml注册的错误页面规则)publicStringgetPath(){returnthis.path;}此时会被BasicErrorController处理:@

默认的错误处理机制

  在定制错误页面和错误响应数据之前,我们先来看看 SpringBoot 的错误处理机制。

环境:这个环境接触的比较少,用于一些日常的需求,或者临时改个线上的小bug使用到。一般是由开发同学和产品同学处理,小需求和小bug开发或修复后开发通知产品验收即可。上线阶段:开发修复好bug,测试同学

ErrorMvcAutoConfiguration :

环境:这个环境接触的比较少,用于一些日常的需求,或者临时改个线上的小bug使用到。一般是由开发同学和产品同学处理,小需求和小bug开发或修复后开发通知产品验收即可。上线阶段:开发修复好bug,测试同学

环境:这个环境接触的比较少,用于一些日常的需求,或者临时改个线上的小bug使用到。一般是由开发同学和产品同学处理,小需求和小bug开发或修复后开发通知产品验收即可。上线阶段:开发修复好bug,测试同学

容器中有以下组件:

环境:这个环境接触的比较少,用于一些日常的需求,或者临时改个线上的小bug使用到。一般是由开发同学和产品同学处理,小需求和小bug开发或修复后开发通知产品验收即可。上线阶段:开发修复好bug,测试同学

1、DefaultErrorAttributes
2、BasicErrorController
3、ErrorPageCustomizer
4、DefaultErrorViewResolver

quest,MediaType.ALL));HttpStatusstatus=getStatus(request);returnnewResponseEntity<>(body,statu


<h2>timestamp:[[${timestamp}]]</h2><h2>exception:[[${exception}]]</h2><h2

quot;+viewName;TemplateAvailabilityProviderprovider=this.templateAvailabilityProviders.getProvider(e

quest,MediaType.ALL));HttpStatusstatus=getStatus(request);returnnewResponseEntity<>(body,statu

Object>model=Collections.unmodifiableMap(getErrorAttributes(request,isIncludeStackTrace(request,M

/error";//(web.xml注册的错误页面规则)publicStringgetPath(){returnthis.path;}此时会被BasicErrorController处理:@

/error";//(web.xml注册的错误页面规则)publicStringgetPath(){returnthis.path;}此时会被BasicErrorController处理:@

/error";//(web.xml注册的错误页面规则)publicStringgetPath(){returnthis.path;}此时会被BasicErrorController处理:@

系统出现 4xx 或者 5xx 错误时,ErrorPageCustomizer 就会生效:

quest,MediaType.ALL));HttpStatusstatus=getStatus(request);returnnewResponseEntity<>(body,statu

    @Bean
    public ErrorPageCustomizer errorPageCustomizer() {
        return new ErrorPageCustomizer(this.serverProperties, this.dispatcherServletPath);
    }
   private static class ErrorPageCustomizer implements ErrorPageRegistrar, Ordered {
        private final ServerProperties properties;
        private final DispatcherServletPath dispatcherServletPath;
        protected ErrorPageCustomizer(ServerProperties properties,
                DispatcherServletPath dispatcherServletPath) {
            this.properties = properties;
            this.dispatcherServletPath = dispatcherServletPath;
        }
         // 注册错误页面响应规则
        @Override
        public void registerErrorPages(ErrorPageRegistry errorPageRegistry) {
            ErrorPage errorPage = new ErrorPage(this.dispatcherServletPath
                    .getRelativePath(this.properties.getError().getPath()));
            errorPageRegistry.addErrorPages(errorPage);
        }
        @Override
        public int getOrder() {
            return 0;
        }
    }

上面的注册错误页面响应规则能够的到错误页面的路径(getPath):

quest,MediaType.ALL));HttpStatusstatus=getStatus(request);returnnewResponseEntity<>(body,statu

    @Value("${error.path:/error}")
    private String path = "/error"; //(web.xml注册的错误页面规则)
    public String getPath() {
        return this.path;
    }

此时会被 BasicErrorController 处理:

,比较全面的测试用例掌握测试常用的几种方法,比如:等价类,边界值,因果图,等等 第二部分-网络知识:课件里面的有点不太清楚,请参考以下链接:http://www.ruanyifeng.com

@Controller
@RequestMapping("${server.error.path:${error.path:/error}}")
public class BasicErrorController extends AbstractErrorController {
}

BasicErrorController 中有两个请求:

,比较全面的测试用例掌握测试常用的几种方法,比如:等价类,边界值,因果图,等等 第二部分-网络知识:课件里面的有点不太清楚,请参考以下链接:http://www.ruanyifeng.com

    // //产生html类型的数据;浏览器发送的请求来到这个方法处理
    //  MediaType.TEXT_HTML_VALUE ==> "text/html"
    @RequestMapping(produces = MediaType.TEXT_HTML_VALUE)
    public ModelAndView errorHtml(HttpServletRequest request,
            HttpServletResponse response) {
        HttpStatus status = getStatus(request);
        Map<String, Object> model = Collections.unmodifiableMap(getErrorAttributes(
                request, isIncludeStackTrace(request, MediaType.TEXT_HTML)));
        response.setStatus(status.value());
        //去哪个页面作为错误页面;包含页面地址和页面内容
        ModelAndView modelAndView = resolveErrorView(request, response, status, model);
        return (modelAndView != null) ? modelAndView : new ModelAndView("error", model);
    }
    //产生json数据,其他客户端来到这个方法处理;
    @RequestMapping
    public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
        Map<String, Object> body = getErrorAttributes(request,
                isIncludeStackTrace(request, MediaType.ALL));
        HttpStatus status = getStatus(request);
        return new ResponseEntity<>(body, status);
    }

上面源码中有两个请求,分别是处理浏览器发送的请求和其它浏览器发送的请求的。是通过请求头来区分的:

,比较全面的测试用例掌握测试常用的几种方法,比如:等价类,边界值,因果图,等等 第二部分-网络知识:课件里面的有点不太清楚,请参考以下链接:http://www.ruanyifeng.com

1、浏览器请求头

,比较全面的测试用例掌握测试常用的几种方法,比如:等价类,边界值,因果图,等等 第二部分-网络知识:课件里面的有点不太清楚,请参考以下链接:http://www.ruanyifeng.com

AndView;}}returnnull;}DefaultErrorViewResolver,默认错误视图解析器,去哪个页面是由其解析得到的;@OverridepublicModelAndViewre

2、其他客户端请求头

AndView;}}returnnull;}DefaultErrorViewResolver,默认错误视图解析器,去哪个页面是由其解析得到的;@OverridepublicModelAndViewre

AndView;}}returnnull;}DefaultErrorViewResolver,默认错误视图解析器,去哪个页面是由其解析得到的;@OverridepublicModelAndViewre

resolveErrorView,获取所有的异常视图解析器 ;

AndView;}}returnnull;}DefaultErrorViewResolver,默认错误视图解析器,去哪个页面是由其解析得到的;@OverridepublicModelAndViewre

    protected ModelAndView resolveErrorView(HttpServletRequest request,
            HttpServletResponse response, HttpStatus status, Map<String, Object> model) {
         //获取所有的 ErrorViewResolver 得到 ModelAndView
        for (ErrorViewResolver resolver : this.errorViewResolvers) {
            ModelAndView modelAndView = resolver.resolveErrorView(request, status, model);
            if (modelAndView != null) {
                return modelAndView;
            }
        }
        return null;
    }

DefaultErrorViewResolver,默认错误视图解析器,去哪个页面是由其解析得到的;

楚请求URL相关参数,会涉及到接口测试。  了解掌握之后呢,就要具体的应用上,互联网工作用的浏览器一般是谷歌的。  我们打开谷歌浏览器,打开开发者工具进行使用。

    @Override
    public ModelAndView resolveErrorView(HttpServletRequest request, HttpStatus status,
            Map<String, Object> model) {
        ModelAndView modelAndView = resolve(String.valueOf(status.value()), model);
        if (modelAndView == null && SERIES_VIEWS.containsKey(status.series())) {
            modelAndView = resolve(SERIES_VIEWS.get(status.series()), model);
        }
        return modelAndView;
    }
    private ModelAndView resolve(String viewName, Map<String, Object> model) {
        // 视图名,拼接在 error/ 后面
        String errorViewName = "error/" + viewName;
        TemplateAvailabilityProvider provider = this.templateAvailabilityProviders
                .getProvider(errorViewName, this.applicationContext);
        if (provider != null) {
             // 使用模板引擎的情况
            return new ModelAndView(errorViewName, model);
        }
         // 未使用模板引擎的情况
        return resolveResource(errorViewName, model);
    }

其中 SERIES_VIEWS 是:

楚请求URL相关参数,会涉及到接口测试。  了解掌握之后呢,就要具体的应用上,互联网工作用的浏览器一般是谷歌的。  我们打开谷歌浏览器,打开开发者工具进行使用。

    private static final Map<Series, String> SERIES_VIEWS;
    static {
        Map<Series, String> views = new EnumMap<>(Series.class);
        views.put(Series.CLIENT_ERROR, "4xx");
        views.put(Series.SERVER_ERROR, "5xx");
        SERIES_VIEWS = Collections.unmodifiableMap(views);
    }

下面看看没有使用模板引擎的情况:

楚请求URL相关参数,会涉及到接口测试。  了解掌握之后呢,就要具体的应用上,互联网工作用的浏览器一般是谷歌的。  我们打开谷歌浏览器,打开开发者工具进行使用。

    private ModelAndView resolveResource(String viewName, Map<String, Object> model) {
        for (String location : this.resourceProperties.getStaticLocations()) {
            try {
                Resource resource = this.applicationContext.getResource(location);
                resource = resource.createRelative(viewName + ".html");
                if (resource.exists()) {
                    return new ModelAndView(new HtmlResourceView(resource), model);
                }
            }
            catch (Exception ex) {
            }
        }
        return null;
    }

以上代码可以总结为:

楚请求URL相关参数,会涉及到接口测试。  了解掌握之后呢,就要具体的应用上,互联网工作用的浏览器一般是谷歌的。  我们打开谷歌浏览器,打开开发者工具进行使用。

模板引擎不可用
就在静态资源文件夹下
找errorViewName对应的页面 error/4xx.html

quot;+viewName;TemplateAvailabilityProviderprovider=this.templateAvailabilityProviders.getProvider(e

如果,静态资源文件夹下存在,返回这个页面
如果,静态资源文件夹下不存在,返回null

quot;+viewName;TemplateAvailabilityProviderprovider=this.templateAvailabilityProviders.getProvider(e


<h2>timestamp:[[${timestamp}]]</h2><h2>exception:[[${exception}]]</h2><h2

quot;+viewName;TemplateAvailabilityProviderprovider=this.templateAvailabilityProviders.getProvider(e

quest,MediaType.ALL));HttpStatusstatus=getStatus(request);returnnewResponseEntity<>(body,statu

Object>model=Collections.unmodifiableMap(getErrorAttributes(request,isIncludeStackTrace(request,M

/error";//(web.xml注册的错误页面规则)publicStringgetPath(){returnthis.path;}此时会被BasicErrorController处理:@

/error";//(web.xml注册的错误页面规则)publicStringgetPath(){returnthis.path;}此时会被BasicErrorController处理:@

/error";//(web.xml注册的错误页面规则)publicStringgetPath(){returnthis.path;}此时会被BasicErrorController处理:@

定制错误响应

  按照 SpringBoot 的默认异常响应,分为默认响应页面和默认响应信息。我们也分为定制错误页面和错误信息。

quot;+viewName;TemplateAvailabilityProviderprovider=this.templateAvailabilityProviders.getProvider(e

定制错误的页面

  1. 有模板引擎的情况

    TTP知识啦,现在应该可以看得明白了。 工作用到的有:1、确定环境,在网络地址那里,一般测试环境是要绑定host的。hosts作用:就是将一些常用的网址域名与其对应的IP地址建立一个关联“数

    ​ SpringBoot 默认定位到模板引擎文件夹下面的 error/ 文件夹下。根据发生的状态码的错误寻找到响应的页面。注意一点的是,页面可以"精确匹配"和"模糊匹配"。
    ​ 精确匹配的意思是返回的状态码是什么,就找到对应的页面。例如,返回的状态码是 404,就匹配到 404.html.

    TTP知识啦,现在应该可以看得明白了。 工作用到的有:1、确定环境,在网络地址那里,一般测试环境是要绑定host的。hosts作用:就是将一些常用的网址域名与其对应的IP地址建立一个关联“数

    ​ 模糊匹配,意思是可以使用 4xx 和 5xx 作为错误页面的文件名来匹配这种类型的所有错误。不过,"精确匹配"优先。

    n/p/7068905.htmlCharles:http://blog.devtang.com/2015/11/14/charles-introduction/#%E5%B0%86-Charles-%

  2. 没有模板引擎

    rNotExistException.class)publicStringhandleException(Exceptione,HttpServletRequestrequest){Map<St

    ​ 项目如果没有使用模板引擎,则在静态资源文件夹下面查找。

    rNotExistException.class)publicStringhandleException(Exceptione,HttpServletRequestrequest){Map<St

下面自定义异常页面,并模拟异常发生。

rNotExistException.class)publicStringhandleException(Exceptione,HttpServletRequestrequest){Map<St

在以上的示例基础上,首先,自定义一个异常:

rNotExistException.class)publicStringhandleException(Exceptione,HttpServletRequestrequest){Map<St

public class UserNotExistException extends RuntimeException {
    public UserNotExistException() {
        super("用户不存在");
    }
}

然后,进行异常处理:

lt;/dependency><dependency><groupId>org.springframework.boot</groupId><artif

@ControllerAdvice
public class MyExceptionHandler {
    @ExceptionHandler(UserNotExistException.class)
    public String handleException(Exception e, HttpServletRequest request){
        Map<String,Object> map = new HashMap<>();
        // 传入我们自己的错误状态码  4xx 5xx,否则就不会进入定制错误页面的解析流程
        // Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
        request.setAttribute("javax.servlet.error.status_code",500);
        map.put("code","user.notexist");
        map.put("message","用户出错啦");
        request.setAttribute("ext",map);
        //转发到/error
        return "forward:/error";
    }
}

注意几点,一定要定制自定义的状态码,否则没有作用。

lt;/dependency><dependency><groupId>org.springframework.boot</groupId><artif

第三步,定制一个页面:

lt;/dependency><dependency><groupId>org.springframework.boot</groupId><artif

<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>Internal Server Error | 服务器错误</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
        <!--省略css代码-->
    </style>
</head>
<body>
<h1>服务器错误</h1>
<main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
    <h1>status:[[${status}]]</h1>
    <h2>timestamp:[[${timestamp}]]</h2>
    <h2>exception:[[${exception}]]</h2>
    <h2>message:[[${message}]]</h2>
    <h2>ext:[[${ext.code}]]</h2>
    <h2>ext:[[${ext.message}]]</h2>
</main>
</body>
</html>

最后,模拟一个异常:

制自定义的状态码,否则没有作用。第三步,定制一个页面:<!doctypehtml><htmllang="en"xmlns:th="http://www.

@Controller
public class CustomizeErrorController {
    @GetMapping("/customizeViewError")
    public void customizeViewError(){
        System.out.println("自定义页面异常");
        throw new UserNotExistException();
    }
}

启动项目,可以观察到以下结果:

制自定义的状态码,否则没有作用。第三步,定制一个页面:<!doctypehtml><htmllang="en"xmlns:th="http://www.

制自定义的状态码,否则没有作用。第三步,定制一个页面:<!doctypehtml><htmllang="en"xmlns:th="http://www.

定制响应的json

针对浏览器意外的其他客户端错误响应,相似的道理,我们先进行自定义异常处理:

制自定义的状态码,否则没有作用。第三步,定制一个页面:<!doctypehtml><htmllang="en"xmlns:th="http://www.

    @ResponseBody
    @ExceptionHandler(UserNotExistException.class)
    public Map<String,Object> handleException(Exception e){
        Map<String,Object> map = new HashMap<>();
        map.put("code","user.notexist");
        map.put("message",e.getMessage());
        return map;
    }

然后模拟异常的出现:

aultViewError(){System.out.println("默认页面异常");}@ResponseBody@GetMapping("/defaultDataE

    @ResponseBody
    @GetMapping("/customizeDataError")
    public void customizeDataError(){
        System.out.println("自定义客户端异常");
        throw new UserNotExistException();
    }

启动项目,看到结果是:

aultViewError(){System.out.println("默认页面异常");}@ResponseBody@GetMapping("/defaultDataE

aultViewError(){System.out.println("默认页面异常");}@ResponseBody@GetMapping("/defaultDataE


<h2>timestamp:[[${timestamp}]]</h2><h2>exception:[[${exception}]]</h2><h2

quot;+viewName;TemplateAvailabilityProviderprovider=this.templateAvailabilityProviders.getProvider(e

quest,MediaType.ALL));HttpStatusstatus=getStatus(request);returnnewResponseEntity<>(body,statu

Object>model=Collections.unmodifiableMap(getErrorAttributes(request,isIncludeStackTrace(request,M

/error";//(web.xml注册的错误页面规则)publicStringgetPath(){returnthis.path;}此时会被BasicErrorController处理:@

/error";//(web.xml注册的错误页面规则)publicStringgetPath(){returnthis.path;}此时会被BasicErrorController处理:@

/error";//(web.xml注册的错误页面规则)publicStringgetPath(){returnthis.path;}此时会被BasicErrorController处理:@

总结

  异常处理同日志一样,也属于项目的“基础设施”,它的存在,可以扩大系统的容错处理,加强系统的健壮性。在自定义的基础上,优化了错误提示,对用户更加友好。

<h2>timestamp:[[${timestamp}]]</h2><h2>exception:[[${exception}]]</h2><h2

  由于篇幅所限,以上的 SpringBoot 的内部错误处理机制也只属于“蜻蜓点水”。后期将重点分析 SpringBoot 的工作机制。

<h2>timestamp:[[${timestamp}]]</h2><h2>exception:[[${exception}]]</h2><h2

  最后,如果需要完整代码,请移步至我的GitHub

者5xx错误时,ErrorPageCustomizer就会生效:@BeanpublicErrorPageCustomizererrorPageCustomizer(){returnnewErrorPa



者5xx错误时,ErrorPageCustomizer就会生效:@BeanpublicErrorPageCustomizererrorPageCustomizer(){returnnewErrorPa

文章版权声明:除非注明,否则均为皮皮看书原创文章,转载或复制请以超链接形式并注明出处。

发表评论

快捷回复: 表情:
验证码
评论列表 (暂无评论,1045人围观)

还没有评论,来说两句吧...

目录[+]