如何定制错误页面?

(1)在有模板引擎的情况下:在template文件夹下的error/状态码;即将错误页面命名为:错误状态码.html放在template文件夹里面的error文件夹下,发生此状态码的错误会来到对应的页面。

页面可以获得的信息:

timestamp:shiajianc

status:状态码

error:错误提示

exception:异常对象

message:异常消息

errors:JSR303数据校验的错误都在这里

(2)如果没有模板引擎,就在静态资源文件static下的error文件夹下找。

(3)如果都没有,则返回系统的默认错误页面。

在错误页面可以这么获取到数据:

status:[[${status}]]

timestamp:[[${timestamp}]]

如何定制错误的json数据?

首先我们在com.gong.springbootcurd下新建一个exception文件夹,该文件夹中定义一个异常:UserNoExistException.java

packagecom.gong.springbootcurd.exception;public class UserNotExistException extendsRuntimeException {publicUserNotExistException() {super("用户不存在");

}

}

然后在com.gong.springbootcurd.component下新建一个异常处理器:MyExceptionHandler.java

packagecom.gong.springbootcurd.component;importcom.gong.springbootcurd.exception.UserNotExistException;importorg.springframework.web.bind.annotation.ControllerAdvice;importorg.springframework.web.bind.annotation.ExceptionHandler;importorg.springframework.web.bind.annotation.ResponseBody;importjavax.servlet.http.HttpServletRequest;importjava.util.HashMap;importjava.util.Map;

@ControllerAdvicepublic classMyExceptionHandler {//1、浏览器客户端返回的都是json

@ResponseBody

@ExceptionHandler(UserNotExistException.class)public MaphandleException(Exception e){

Map map = new HashMap<>();

map.put("code","user.notexist");

map.put("message",e.getMessage());returnmap;

}

}

在com.gong.springbootcurd.controller的HelloController.java中添加:

@ResponseBody

@RequestMapping("/hello")public String hello(@RequestParam("user") String user){if(!user.equals("aaa")){throw newUserNotExistException();

}return "hello world";

}

我们在服务器种模拟请求:

会显示我们自定的json错误信息。

如何设置自适应的显示错误页面?

也就是说浏览器显示的就是错误页面,而客户端显示的是json的错误信息。

修改MyExceptionHandler.java里面为:

@ExceptionHandler(UserNotExistException.class)publicString handleException(Exception e, HttpServletRequest request){

Map 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";

}

需要注意的是我们必须要设置响应的状态码,最后转发到错误error请求。

在自己定义的5xx.html中可以这么获取信息了:

status:[[${status}]]

timestamp:[[${timestamp}]]

exception:[[${exception}]]

message:[[${message}]]

ext:[[${ext.code}]]

ext:[[${ext.message}]]

当访问http://localhost:8080/curd/hello?user=aa时,会看到:

这里exception获取不到???暂时还不知道什么原因。

如何定制自己的错误信息到页面中?

向上述的ext.code和 ext.message是我们异常处理器给我们带的字段,如果我们想新增自己的字段:

在com.gong.springbootcurd.component中新建一个MyErrorAttributes.java

packagecom.gong.springbootcurd.component;importcom.gong.springbootcurd.exception.UserNotExistException;importorg.springframework.boot.web.servlet.error.DefaultErrorAttributes;importorg.springframework.stereotype.Component;importorg.springframework.web.context.request.RequestAttributes;importorg.springframework.web.context.request.WebRequest;importjava.util.Map;//给容器中加入我们自己定义的ErrorAttributes

@Componentpublic class MyErrorAttributes extendsDefaultErrorAttributes{//返回值的map就是页面和json能获取的所有字段

public Map getErrorAttributes(WebRequestrequestAttributes, booleanincludeStackTrace) {

Map map = super.getErrorAttributes(requestAttributes, includeStackTrace);//map.put("exception", UserNotExistException.class.getName());

map.put("company","gong");//我们的异常处理器携带的数据

Map ext = (Map) requestAttributes.getAttribute("ext", 0);

map.put("ext",ext);returnmap;

}

}

说明:我们先从请求域中得到与系统默认的错误相关的属性,然后再添加自己定义的属性,最后从请求域中得到自定义异常处理器中的属性,全部都传给map进行返回。对于没有打印出来的exception,我们可以这么进行处理,在自定义的异常处理器中:

map.put("exception",e.getClass().getName());

我们自己来获得异常的名字,然后传给exception,只不过最后我们在5xx.html中这么取值:

status:[[${status}]]

timestamp:[[${timestamp}]]

exception:[[${exception}]]

error:[[${error}]]

message:[[${message}]]

company:[[${company}]]

ext:[[${ext.code}]]

ext:[[${ext.message}]]

ext:[[${ext.exception}]]

最后在浏览器输入:loclahost:8080/curd/hello?user=aa

怎样用springboot开发cs_springboot开发之配置自定义的错误界面和错误信息相关推荐

  1. springboot微信公众号开发者配置 token验证失败和参数错误

    真的坑!!擦 首先参数错误 在使用ngrok的时候可能会发生,建议使用natnapp token验证失败,注意token的一致 代码奉上 AutowiredWeiXinConfig weiXinCon ...

  2. 【Java笔记+踩坑】SpringBoot基础3——开发。热部署+配置高级+整合NoSQL/缓存/任务/邮件/监控

      导航: [黑马Java笔记+踩坑汇总]JavaSE+JavaWeb+SSM+SpringBoot+瑞吉外卖+SpringCloud/SpringCloudAlibaba+黑马旅游+谷粒商城 目录 ...

  3. SpringBoot的Web开发入门案例7—WebMvcConfigurer配置类

    SpringBoot的Web开发入门案例7-WebMvcConfigurer配置类 WebMvcConfigurer接口的几个常用方法: addViewControllers:配置请求路径和页面的映射 ...

  4. 第13章 Kotlin 集成 SpringBoot 服务端开发(1)

    第13章 Kotlin 集成 SpringBoot 服务端开发 本章介绍Kotlin服务端开发的相关内容.首先,我们简单介绍一下Spring Boot服务端开发框架,快速给出一个 Restful He ...

  5. Jeecg-Boot 2.1.2版本发布,基于SpringBoot的快速开发平台

    项目介绍 JeecgBoot是一款基于代码生成器的JAVA快速开发平台,开源界"小普元"超越传统商业企业级开发平台!采用前后端分离架构:SpringBoot 2.x,Ant Des ...

  6. springboot+openFeign+nacos开发实战

    前面说了dubbo+nocas开发实战,现在来说下springboot+openFeign+nacos开发实战. 文章目录 什么是Feign Nacos环境准备 Nacos与openFegin整合 项 ...

  7. Springboot的web开发-静态资源

    1.web开发简介 SpringMVC自动配置概览 Spring Boot provides auto-configuration for Spring MVC that works well wit ...

  8. 3万字《SpringBoot微服务开发——Shiro(安全)》

    SpringBoot微服务开发--Shiro(安全) 文章目录 SpringBoot微服务开发--Shiro(安全) Shiro(安全) 1.Shiro简介 2.Shiro有哪些功能? 3.Shiro ...

  9. 项目设计-基于SpringBoot和Vue开发的宿舍管理系统

    前言 ​ 本期项目是宿舍管理系统,主要包括数据监控大盘.宿舍楼管理.宿舍管理.宿舍成员管理.借用管理.卫生管理.缴费管理.保修管理.日志管理.用户管理.角色管理以及各个模块的导出功能.以企业级的开发标 ...

最新文章

  1. 17.1.1.3 Creating a User for Replication
  2. Case When ELSE END语句
  3. 什么是mysql索引文件_数据库索引文件一般采用什么数据结构?
  4. redis和memcached缓存
  5. 如何在iOS上运行React Native应用
  6. 作者:胡晓惠(1960-),男,中国科学院软件研究所研究员,天基综合信息技术实验室常务副主任...
  7. linux下u盘病毒msdos,浅谈U盘病毒——MS-DOS.com 以及做最便民的杀毒软件
  8. Linux内核协议栈分析之网卡初始化——tcp/ip通信并不神秘(1)
  9. 多线程编程下单例模式与多例模式的使用总结
  10. Leetcode 刷题笔记(二十四) ——动态规划篇之背包问题:01背包
  11. 计算机防雷安全标语,防雷电安全标语
  12. 详解动态规划——邹博讲动态规划
  13. 【Linux】Linux的信号量集
  14. java + concat_Java中concat()方法和加号(+)运算符之间的区别
  15. QQ聊天 代码 输入表情
  16. 精益思想如何加速企业的全局价值流动?
  17. 【LeetCode-13】-罗马数字
  18. Mac终端 连接远程服务器
  19. Vue.js 组件 - 组件间的循环引用
  20. vue-cli项目局域网访问

热门文章

  1. webwork 标签 基本用法 例子
  2. rtmp推流时间戳兼容问题
  3. ELS多种方式集群部署
  4. Python求解线性方程组
  5. boost之asio异步io使用实例
  6. zabbix历史数据mysql_处理Zabbix历史数据库办法一
  7. extjs 获取id的值_extjs 获取Dom对象
  8. 怎么学python知乎_你是怎么学习Python的 ?
  9. 驱动编程中的头文件与内核源码的关系
  10. 前端规范之媒体文件规范