1.添加异常类

package myshop.config;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView; /*
* 1.新建一个class,这里取名为GlobalDefaultExceptionHandler
* 2.在class上添加注解,@ControllerAdvice
* 3.在class中添加一个方法
* 4.在方法上添加@ExcetionHandler拦截相应的异常
* 5.如果返回的是View -- 方法返回值是ModelAndView
* 6.如果返回的是String或者是Json数据,那么需要在方法上添加@RequestBody注解
*/
@ControllerAdvice
public class GlobalDefaultExceptionHandler { @ExceptionHandler(Exception.class)
@ResponseBody
public String defaultExceptionHandler(HttpServletRequest req, Exception e)
{
//ModelAndView mv = new ModelAndView();
//mv.setViewName("");
return "对不起,服务器繁忙...";
}
}

2.添加启动类

package myshop;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class App { public static void main(String[] args) {
// TODO Auto-generated method stub
SpringApplication.run(App.class, args);
}
}

SpringBoot------全局异常捕获的更多相关文章

  1. springboot 全局异常捕获,异常流处理业务逻辑

    前言 上一篇文章说到,参数校验,往往需要和全局的异常拦截器来配套使用,使得返回的数据结构永远是保持一致的.参数异常springboot默认的返回结构: { "timestamp": ...

  2. SpringBoot全局异常拦截

    SpringBoot全局异常捕获 使用到的技能 @RestControllerAdvice或(@ControllerAdvice+@ResponseBody) @ExceptionHandler 代码 ...

  3. springboot(二 如何访问静态资源和使用模板引擎,以及 全局异常捕获)

    在我们开发Web应用的时候,需要引用大量的js.css.图片等静态资源. 默认配置 Spring Boot默认提供静态资源目录位置需置于classpath下,目录名需符合如下规则: /static / ...

  4. springboot编程之全局异常捕获

    springboot编程之全局异常捕获 1.创建GlobalExceptionHandler.java,在类上注解@ControllerAdvice, 在方法上注解@ExceptionHandler( ...

  5. 【快学springboot】5.全局异常捕获,异常流处理业务逻辑

    前言 上一篇文章说到,参数校验,往往需要和全局的异常拦截器来配套使用,使得返回的数据结构永远是保持一致的.参数异常springboot默认的返回结构: { "timestamp": ...

  6. SpringBoot图文教程15—项目异常怎么办?「跳转404错误页面」「全局异常捕获」

    有天上飞的概念,就要有落地的实现 概念十遍不如代码一遍,朋友,希望你把文中所有的代码案例都敲一遍 先赞后看,养成习惯 SpringBoot 图文教程系列文章目录 SpringBoot图文教程1-Spr ...

  7. SpringBoot 全局异常拦截捕获处理

    一.全局异常处理 //Result定义全局数据返回对象 package com.xiaobing.demo001.domain; public class Result { private Integ ...

  8. 测试开发专题:spring-boot统一异常捕获

    java异常介绍 异常时相对于return的一种退出机制,可以由系统触发,也可由程序通过throw语句触发,异常可以通过try/catch语句进行捕获并处理,如果没有捕获,则会导致程序退出并输出异常栈 ...

  9. MVC 好记星不如烂笔头之 ---> 全局异常捕获以及ACTION捕获

    public class BaseController : Controller { /// <summary> /// Called after the action method is ...

  10. atitit.js浏览器环境下的全局异常捕获

    atitit.js浏览器环境下的全局异常捕获 window.onerror = function(errorMessage, scriptURI, lineNumber) { var s= JSON. ...

随机推荐

  1. an error has occurred while trying to access the log file. logging may not function properly

    I had this issure a couple of days ago  when  open my vs2012 on windows8,by google i find the soluth ...

  2. 初试 spring web mvc

    作为一名code需要了解更多的知识,编程方面的东西太多了,是个逐渐积累的过程.最近学习一下spring web mvc,写下我个人的一些经验. 1.准备jar包.spring mvc 已经到了版本4, ...

  3. 使用openssl模拟CA和CA证书的签发

    使用openssl模拟CA和CA证书的签发     当使用ssl/tls进行加密通信时,必须要有数字证书.若通信只限制在局域网内,可以不向第三方机构申请签发证书,可以通过openssl模拟CA(Cer ...

  4. 如何在ROS中使用PCL—数据格式(1)

    在ROS中点云的数据类型 在ROS中表示点云的数据结构有: sensor_msgs::PointCloud      sensor_msgs::PointCloud2     pcl::PointCl ...

  5. Material Design 相关资源

    Materialpalette -- Material配色工具 Materialup -- Material设计灵感: Material Design 相关好文: <超全面总结!深聊MATERI ...

  6. 请教 JTable 里的单元格如何使得双击进入单元格后,单元格的内容处于全选中状态

    http://bbs.csdn.net/topics/390195204 ———————————————————————————————————————— java 达人, 最近在开发一个 java  ...

  7. Sword pcre库函数学习一

    0.pcre_exec 原型: #include <pcre.h> int pcre_exec(const pcre *code, const pcre_extra *extra, con ...

  8. Ogre RTSS组件解析

    我们为什么要用RTSS. Ogre如计算物体位置,纹理,光照都有固定API如(glMatrixFrustumEXT, glLoadmatrix, glTexture, glLight ),使用这些AP ...

  9. c指针 --笔记2返回指针值的函数

    返回指针值的函数 一般带回指针值的函数,定义形式为: int *a (int x, int y); 看这个经典案例: #include <stdio.h> int main(int arg ...

  10. Android wifi powersave

    使用高通平台的查看power save的功能. 一般是控制WCNSS_qcom_cfg.ini文件的两个参数gEnableBmps,gEnableImps. BMPS: Beacon mode pow ...