Spring Boot 全局异常配置,处理异常控制器需要和发生异常的方法在一个类中。使用 ControllerAdvice 注解

package com.li.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
public class ExceptionHandlerController {
@RequestMapping("/exceptionTest")
@ResponseBody
public String test() throws Exception{
System.out.println("测试");
int i=1/0;
return Integer.toString(i);
}
@ControllerAdvice
class HandlerException{
@ExceptionHandler(Exception.class)
@ResponseBody
public String defultExcepitonHandler(Exception e) {
// return "{\"error\":\"error\"}";
return "error";
}
}
}

https://blog.csdn.net/qq_34083066/article/details/79424142

Spring Boot 全局异常配置的更多相关文章

  1. Spring Boot 全局异常捕获

    import javax.servlet.http.HttpServletRequest; import org.springframework.web.bind.annotation.Control ...

  2. Spring Boot 2.0 配置图文教程

    摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! 本章内容 自定义属性快速入门 外化配置 自动配置 自定义创建 ...

  3. spring设置全局异常处理器

    1.spring设置全局异常,它的原理是向上捕获 spring.xml配置 <!--自定义全局异常处理器--> <bean id="globalExceptionResol ...

  4. spring boot web相关配置

    spring boot集成了servlet容器,当我们在pom文件中增加spring-boot-starter-web的maven依赖时,不做任何web相关的配置便能提供web服务,这还得归于spri ...

  5. spring boot多数据源配置(mysql,redis,mongodb)实战

    使用Spring Boot Starter提升效率 虽然不同的starter实现起来各有差异,但是他们基本上都会使用到两个相同的内容:ConfigurationProperties和AutoConfi ...

  6. 转-spring boot web相关配置

    spring boot web相关配置 80436 spring boot集成了servlet容器,当我们在pom文件中增加spring-boot-starter-web的maven依赖时,不做任何w ...

  7. 【转】spring boot application.properties 配置参数详情

    multipart multipart.enabled 开启上传支持(默认:true) multipart.file-size-threshold: 大于该值的文件会被写到磁盘上 multipart. ...

  8. 【转】spring boot web相关配置

    spring boot集成了servlet容器,当我们在pom文件中增加spring-boot-starter-web的maven依赖时,不做任何web相关的配置便能提供web服务,这还得归于spri ...

  9. Spring MVC全局异常后返回JSON异常数据

    问题: 当前项目是作为手机APP后台支持,使用spring mvc + mybaits + shiro进行开发.后台服务与手机端交互是发送JSON数据.如果后台发生异常,会直接返回异常页面,显示异常内 ...

随机推荐

  1. 分享jQuery封装好的一些常用操作

    1. 禁止右键点击 $(document).ready(function(){   $(document).bind("contextmenu",function(e){      ...

  2. VC++ 实现窗口抖动

    RECT rect; int x, y, nWidth, nHeight; GetWindowRect(&rect); x = rect.left; y = rect.top; nWidth ...

  3. 通过xtrabackup工具对mysql数据库做全备

    需求描述: 使用xtrabackup工具对mysql实例中的所有的数据库进行备份,并且将备份的过程输出到指定的日志文件中. 操作过程: 1.使用xtrabacup工具备份mysql实例 xtrabac ...

  4. ubuntu 16.04 appstreamcli 问题

    http://blog.csdn.net/zhbpd/article/details/77508675

  5. Sublime Text2安装Package Control

    一.安装Package Control 使用Sublime Text2首先就要安装Package Control,这样就能使用丰富的插件包了 安装方法有2种: (1)访问Package Control ...

  6. 更改VS2010的[默认开发语言]

    1.菜单-->"工具"-->"导入导出设置".例如以下图: 2.选择"重置全部设置",例如以下图: 3.重置设置,例如以下图: ...

  7. Java精选笔记_IO流(字节流、InputStream、OutputStream、字节文件、缓冲区输入输出流)

    字节流 操作图片数据就要用到字节流. 字符流使用的是字符数组char[],字节流使用的是字节数组byte[]. 字节流读写文件 针对文件的读写,JDK专门提供了两个类,分别是FileInputStre ...

  8. 数据库客户端快捷键(oracle+sybase)

    PL/SQL: 选中单行:鼠标三连击某行,那么这一行即被选中. 执行脚本:F8

  9. Spring学习笔记--在SpEL中筛选集合

    要用到Spring的util(包括util:list等),xml文件中的beans中需要添加一些有关util的信息: <?xml version="1.0" encoding ...

  10. java后端接收前端传来的复杂对象(包含List对象集合)

    最近在和安卓对接口的时候发现往java后端传数据的时候,后台对象无法接收. 说明:后台对象为 类似结构 ObjectA{ private String  a; private String b; pr ...