SpringBoot的Web开发入门案例3—异常处理
SpringBoot的Web开发入门案例3—异常处理
SpringBoot 默认404界面(由org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration类提供):
SpringBoot 默认500界面(由org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration类提供):
1. 定制自己的异常页面
在 resources/templates 目录下新建error文件夹,在此文件夹下新建4xx.html、404.html、500.html
4xx.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body><h1>error:4xx</h1>
</body>
</html>
404.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body><h1>error:404</h1>
</body>
</html>
500.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body><h1>error:500</h1>
</body>
</html>
当出现404错误时将显示自定义的404页面:
而在Postman中显示的却是json数据:
当出现以4开头的非404错误时显示自定义的4xx页面:
当出现500错误时将显示自定义的500页面:
2. 使用@ControllerAdvice注解定义全局异常
- 创建 GlobalExceptionHandler 类(用 @ControllerAdvice 标注该类,用 @ExceptionHandler 标注方法,指定处理的异常类型。)
package com.blu.util;import java.util.HashMap;
import java.util.Map;import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;/** 异常处理(自定义json响应)*/@ControllerAdvice
public class GlobalExceptionHandler {@ExceptionHandler(value=Exception.class)@ResponseBodypublic Map<String,Object> globalErrorHandler(){Map<String,Object> map = new HashMap<>();map.put("errorMsg", "失败了");map.put("code", "404");return map;}
}
- 在DemoController中添加exceTest方法
@GetMapping("/exce/{id}")public String exceTest(@PathVariable("id")Integer id) {if(id==0) {throw new RuntimeException("id不能为0");}return "login";}
- 浏览器访问http://localhost:8088/exce/0,显示json数据。
- 浏览器访问http://localhost:8088/exce/1,显示login页面
- Postman访问http://localhost:8088/exce/0,显示的也是json数据
- Postman访问http://localhost:8088/exce/1,显示的是html
SpringBoot的Web开发入门案例3—异常处理相关推荐
- SpringBoot的Web开发入门案例1
SpringBoot的Web开发入门案例1-登录和页面数据遍历读取 新建maven项目:logintest pom.xml文件: <project xmlns="http://mave ...
- SpringBoot的Web开发入门案例2—国际化
SpringBoot的Web开发入门案例2-国际化 改造logintest项目:SpringBoot的Web开发入门案例1 地址:https://blog.csdn.net/BLU_111/artic ...
- SpringBoot的Web开发入门案例9—数据访问
SpringBoot的Web开发入门案例9-数据访问 创建一个springboot项目(打包方式为jar包): 勾选Spring Web选项,勾选JDBC API和MySQL Driver pom文件 ...
- SpringBoot的Web开发入门案例7—WebMvcConfigurer配置类
SpringBoot的Web开发入门案例7-WebMvcConfigurer配置类 WebMvcConfigurer接口的几个常用方法: addViewControllers:配置请求路径和页面的映射 ...
- SpringBoot的Web开发入门案例6—替换默认容器Tomcat
SpringBoot的Web开发入门案例6-替换默认容器Tomcat为Jetty Spring Boot默认是使用Tomcat作为内嵌的Servlet容器的,如需修改为Jetty,只要修改pom文件即 ...
- SpringBoot的Web开发入门案例5—注册Servlets, Filter, Listener
SpringBoot的Web开发入门案例5-注册Servlets, Filter, Listener 注册Servlet 创建MyServlet类 package com.blu.conf;impor ...
- SpringBoot的Web开发入门案例8—支持jsp
新建springboot工程:springboot_jsp,打包方式为war 导入web模块: 生成的项目结构: 包含启动类SpringbootJspApplication: package com. ...
- AOP配置开发入门案例
该AOP开发入门案例采用XML文件方式配置开发(非注解方式)共包含一个xml文件和4个Java类,创建好web工程后引入相应jar包(文末会给出),建好包(若自定义包名注意更改类中的包名),将xml文 ...
- EAS的WEB开发入门
web入门案例文档 web开发入门文档样例 web重写保存提交等方法,在保存提交时改变单据状态 在实体的ContrallerBean重写save和submit方法 如果需要改变工作流审核时的其他状态要 ...
最新文章
- ES既是搜索引擎又是数据库?真的有那么全能吗?
- Spring-Boot:写出来的网站访问不到静态资源?怎样通过url访问SpringBoot项目中的静态资源?localhost:8989/favicon.ico访问不了工程中的图标资源?
- 服务管理--systemctl命令
- 烦人的Facebook分享授权
- 八皇后问题python回溯_解决Python基于回溯法子集树模板实现8皇后问题
- (76)Verilog HDL测试激励:时钟激励3
- 九里机器人_电子科技大学九里堤校区青少年科教基地为农民工留守子女开展科技讲学公益活动...
- 7-1 字符串的冒泡排序 (20 分)
- 计算机类教育部科目分类,教育部学科门类及一的级学科目录表.doc
- android 2k 屏幕 字体模糊,2k显示器上的字体模糊
- 中兴ZXV10 H608B无线MODEM开路由自动拨号上网设置
- Mac操作指南:Mac显示器颜色不正如何调节?
- 【无2022年聚合工艺考试模拟100题模拟考试平台操作
- Kettle的“增加序列”(自增字段)的使用
- ubuntu 如何放大终端窗口字体
- JQuery处理json数据实例(map和数组)
- 面向对象的特征之一:封装性
- 《哈佛商学院最受欢迎的领导课》读书笔记
- dsy 1002 轮状病毒
- ERP中物料删除与停用