springboot 常用的异常处理方式
springboot常用的异常处理推荐:
一.创建一个异常控制器,并实现ErrorController接口:
package com.example.demo.controller; import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
public class BaseErrorController implements ErrorController { @Override
public String getErrorPath() {
return "/error/error";
} @RequestMapping("/error")
public String getError() {
return getErrorPath();
} }
当系统内发生错误后会跳转到error页面。
二.创建一个异常句柄ErrorExceperHandler
package com.example.demo.handler; import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView; @ControllerAdvice
public class ErrorExceperHandler { @ExceptionHandler
@ResponseStatus(HttpStatus.OK)
public ModelAndView processException(Exception exception) {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("exception", exception.getMessage());
modelAndView.setViewName("/error/error");
return modelAndView;
} @ExceptionHandler
@ResponseStatus(HttpStatus.OK)
public ModelAndView processException(RuntimeException exception) {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("exception", exception.getMessage());
modelAndView.setViewName("/error/error");
return modelAndView;
}
}
重载方法针对Exception和RuntimeException进行拦截,当系统发生异常后,会跳转到异常页面。
package com.example.demo.controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; import com.example.demo.entity.Student; @Controller
@RequestMapping("/student/")
public class StudentController { @RequestMapping("show")
public String show(Model model) throws Exception { Student stu = new Student();
stu.setId(1001);
stu.setName("小明");
model.addAttribute("student", stu); if (!"a".equals("")) {
throw new RuntimeException("RuntimeException....");
} if (!"a".equals("")) {
throw new Exception("Exception....");
} return "show";
} }
在做spring或springboot开发的时候推荐使用第二种。
springboot 常用的异常处理方式的更多相关文章
- SpringBoot: 15.异常处理方式5(通过实现HandlerExceptionResolver类)(转)
修改异常处理方式4中的全局异常处理controller package com.bjsxt.exception; import org.springframework.context.annotati ...
- SpringBoot中的异常处理方式
SpringBoot中有五种处理异常的方式: 一.自定义错误页面 SpringBoot默认的处理异常机制:SpringBoot默认的已经提供了一套处理异常的机制.一旦程序出现了异常SpringBoot ...
- 手把手教你Dubbo与SpringBoot常用两种方式整合
一.Dubbo整合SpringBoot的方式(1) 1)直奔主题,方式一: pom.xml中引入dubbo-starter依赖,在application.properties配置属性,使用@Servi ...
- SpringBoot: 14.异常处理方式4(使用SimpleMappingExceptionResolver处理异常)(转)
修改异常处理方法3中的全局异常处理Controller即可 package bjsxt.exception; import org.springframework.context.annotation ...
- SpringBoot: 13.异常处理方式3(使用@ControllerAdvice+@ExceptionHandle注解)(转)
问题:使用@ExceptionHandle注解需要在每一个controller代码里面都添加异常处理,会咋成代码冗余 解决方法:新建一个全局异常处理类,添加@ControllerAdvice注解即可 ...
- SpringBoot: 12.异常处理方式2(使用@ExceptionHandle注解)(转)
1.编写controller package com.bjsxt.controller; import org.springframework.stereotype.Controller; impor ...
- SpringBoot: 11.异常处理方式1(自定义异常页面)(转)
SpringBoot 默认的处理异常的机制:SpringBoot 默认的已经提供了一套处理异常的机制.一旦程序中出现了异常 SpringBoot 会向/error 的 url 发送请求.在 sprin ...
- SpringBoot介绍,快速入门小例子,目录结构,不同的启动方式,SpringBoot常用注解
SpringBoot介绍 引言 为了使用ssm框架去开发,准备ssm框架的模板配置 为了Spring整合第三方框架,单独的去编写xml文件 导致ssm项目后期xml文件特别多,维护xml文件的成本也是 ...
- SpringBoot学习15:springboot异常处理方式5(通过实现HandlerExceptionResolver类)
修改异常处理方式4中的全局异常处理controller package com.bjsxt.exception; import org.springframework.context.annotati ...
随机推荐
- leetcode_Power of Two_easy
Given an integer, write a function to determine if it is a power of two. 题目意思:推断某个数是否是2的幂. 方法:直接进行bi ...
- python学习之函数返回值
python中函数返回值的方式有2种: 1.return语句 说明:return语句执行完后,函数后续的代码将不会被执行 2.yield语句 说明:yield语句返回的是一个迭代器对象,可以通过nex ...
- code::blocks怎么设置命令行参数
Project->Set Program's Arguments...然后在相应的输入框中输入命令行参数
- 参数化--每个vuser使用不同的参数值
使用controller并发时,每个vuser从参数文件中取一个值,设置如下图:
- js中级系列:模块化(转载 学习中。。。。)
从 CommonJS 到 Sea.js https://github.com/seajs/seajs/issues/269 CMD(Common Module Definition) 模块定义规范 h ...
- Modification of UCT with Patterns in Monte-Carlo Go(论文阅读)
摘要:用于解决多臂赌博机UCB1算法已经被扩展成了解决极大极小树搜索的UCT算法.我们开发了一套Monte-Carlo围棋程序,MoGo,这是第一个使用UCT算法实现的计算机围棋程序.我们解释了为了围 ...
- 日志系统之扩展Flume-LineDeserializer
本人博客文章如未特别注明皆为原创.如有转载请注明出处:http://blog.csdn.net/yanghua_kobe/article/details/46595401 继续闲聊日志系统,在之前的博 ...
- cocos2dx-3.0(前言)
说了好久,告诉自己要開始学cocos2dx(在心理里告诉了好久),然后养成良好习惯,记录自己学习cocos2dx的过程.一个是怕自己忘记.还有一个是更加让自己理解透彻(或许哪天我写的好了,组合一下出一 ...
- ASP.NET动态网站制作(26)-- Ajax
前言:这节课讲解关于Ajax的相关内容. 内容: 1.当点击页面中的一个按钮提交数据或请求数据的时候,整个页面的信息都会提交(不管信息是否是提交或者请求的数据,页面中所有的数据都提交),这样耗用的时间 ...
- MPEG 编解码相关资料收集
以下是我搜集的关于MPEG1/2的编解码相关的资料: (注:mpge帧内编码是基于jpeg编码的,所以请务必先理解jpeg的编解码原理.) 1:Introduction to MPEG 2 Video ...