5.错误处理

均在模板引擎FreeMarker下测试

一.错误的处理

方法一:Spring Boot 将所有的错误默认映射到/error, 实现

①BaseErrorController

②创建error模板error.ftl

方法二:添加自定义的错误页面

  • 2.1 html静态页面:在resources/public/error/ 下定义

    如添加404页面: resources/public/error/404.html页面,中文注意页面编码

  • 2.2 模板引擎页面:在templates/error/下定义

    如添加5xx页面: templates/error/5xx.ftl

    注:templates/error/ 这个的优先级比较 resources/public/error/高

src\main\resources\templates\error\5xx.ftl

controller中加入

方法三:使用注解@ControllerAdvice

实例:

ErrorExceptionHandler

src\main\resources\templates\error\500.ftl

备注当Rest风格情况下返回的json,可以参考以下写法

Last updated

Was this helpful?