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 常用的异常处理方式的更多相关文章

  1. SpringBoot: 15.异常处理方式5(通过实现HandlerExceptionResolver类)(转)

    修改异常处理方式4中的全局异常处理controller package com.bjsxt.exception; import org.springframework.context.annotati ...

  2. SpringBoot中的异常处理方式

    SpringBoot中有五种处理异常的方式: 一.自定义错误页面 SpringBoot默认的处理异常机制:SpringBoot默认的已经提供了一套处理异常的机制.一旦程序出现了异常SpringBoot ...

  3. 手把手教你Dubbo与SpringBoot常用两种方式整合

    一.Dubbo整合SpringBoot的方式(1) 1)直奔主题,方式一: pom.xml中引入dubbo-starter依赖,在application.properties配置属性,使用@Servi ...

  4. SpringBoot: 14.异常处理方式4(使用SimpleMappingExceptionResolver处理异常)(转)

    修改异常处理方法3中的全局异常处理Controller即可 package bjsxt.exception; import org.springframework.context.annotation ...

  5. SpringBoot: 13.异常处理方式3(使用@ControllerAdvice+@ExceptionHandle注解)(转)

    问题:使用@ExceptionHandle注解需要在每一个controller代码里面都添加异常处理,会咋成代码冗余 解决方法:新建一个全局异常处理类,添加@ControllerAdvice注解即可 ...

  6. SpringBoot: 12.异常处理方式2(使用@ExceptionHandle注解)(转)

    1.编写controller package com.bjsxt.controller; import org.springframework.stereotype.Controller; impor ...

  7. SpringBoot: 11.异常处理方式1(自定义异常页面)(转)

    SpringBoot 默认的处理异常的机制:SpringBoot 默认的已经提供了一套处理异常的机制.一旦程序中出现了异常 SpringBoot 会向/error 的 url 发送请求.在 sprin ...

  8. SpringBoot介绍,快速入门小例子,目录结构,不同的启动方式,SpringBoot常用注解

    SpringBoot介绍 引言 为了使用ssm框架去开发,准备ssm框架的模板配置 为了Spring整合第三方框架,单独的去编写xml文件 导致ssm项目后期xml文件特别多,维护xml文件的成本也是 ...

  9. SpringBoot学习15:springboot异常处理方式5(通过实现HandlerExceptionResolver类)

    修改异常处理方式4中的全局异常处理controller package com.bjsxt.exception; import org.springframework.context.annotati ...

随机推荐

  1. DNS的概念,用途,DNS查询的实现算法

    1.DNS的概念,用途      DNS是由解析器以及域名服务器组成的.      域名服务器是指保存有该网络中所有主机的域名和对应IP地址,并具有将域名转换为IP地址功能的服务器.      DNS ...

  2. Error: [vuex] vuex requires a Promise polyfill in this browser. 与 babel-polyfill 的问题

    Error: [vuex] vuex requires a Promise polyfill in this browser. 与 babel-polyfill 的问题 采用最笨重的解决方案就是npm ...

  3. SpringCloud系列十一:自定义Feign

    1. 回顾 上文我们讲解了如何为服务消费者配置Feign. 在Spring Cloud中,Feign的默认配置类是FeignClientsConfiguration,该类定义了Feign默认使用的编码 ...

  4. 项目实践中--Git服务器的搭建与使用指南

    一.前言 Git是一款免费.开源的分布式版本控制系统,用以有效.高速的处理从很小到非常大的项目版本管理.在平时的项目开发中,我们会使用到Git来进行版本控制. Git的功能特性: 从一般开发者的角度来 ...

  5. 贯通tomcat --- 电子书

    http://www.educity.cn/jiaocheng/j10865.html 第1章 认识Tomcat [本章导读] Tomcat服务器是一个免费的开放源代码的Web应用服务器.它是Apac ...

  6. Mysql又一次整理笔记--woods备忘

    ==============================SQL备忘 CRUD 查询 多表 事件等=============================== ------------------ ...

  7. Android JNI和NDK学习(04)--NDK调试方法(转)

    本文转自:http://www.cnblogs.com/skywang12345/archive/2013/05/23/3092812.html 本文主要介绍在ndk中添加log的方法.然后,我们就可 ...

  8. NYOJ 诡异的电梯 && nyoj 1204 魔法少女

    诡异的电梯[Ⅰ] 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 新的宿舍楼有 N(1≤N≤100000) 层 and M(1≤M≤100000)个学生. 在新的宿舍楼里 ...

  9. jQuery EasyUI DataGrid在MVC中的运用-基本属性并实现分页

    ※ datagrid的基本属性和方法  ※ datagrid分页在前后台的实现 最终效果:    与视图显示对应的view model   public class Book public strin ...

  10. linux内核的oops

    什么是Oops?从语言学的角度说,Oops应该是一个拟声词.当出了点小事故,或者做了比较尴尬的事之后,你可以说"Oops",翻译成中国话就叫做“哎呦”.“哎呦,对不起,对不起,我真 ...