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. VS 清除编译产生的临时文件、文件夹

    VS编译过程中会产生一些临时文件,通过以下脚本可清除 @echo off for /r %%i in (*.sdf,*.ncb,*.suo,*.exp,*.user,*.aps,*.idb,*.dep ...

  2. ROS文件系统介绍--2

    ros初级核心教程--ROS文件系统介绍(原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/) 1.ROS文件系统介绍: 1.1.预备工作:本教程中 ...

  3. Android ListView圆角

    首先来看看ListView 相关基本属性 1.单击列表后,列表的背景变成黑色了. 可通过指定android:cacheColorHint的属性来放变它,将它指定为透明. 使用以下的属性值:     a ...

  4. Java精选笔记_JSP开发模型

    JSP开发模型 JSP Model JSP Model1简单轻便,适合小型Web项目的快速开发. JSP Model2模型是在JSP Model1的基础上提出的,它提供了更清晰的代码分层,更适用于多人 ...

  5. Python 常见文件操作的函数示例(转)

    转自:http://www.cnblogs.com/txw1958/archive/2012/03/08/2385540.html # -*-coding:utf8 -*- ''''' Python常 ...

  6. leetcode -- permutation 总结

    leetcode上关于permutation有如下几题 Permutation Sequence Next Permutation Permutations Permutations II

  7. eclipse的.properties文件中文显示问题

    eclipse的.properties文件,默认的编码方式是iso-8859-1. 所以中文显示有问题. 按照下面的方式,把Default Encoding修改成UTF-8就可以了.

  8. LeetCode——Binary Tree Paths

    Description: Given a binary tree, return all root-to-leaf paths. For example, given the following bi ...

  9. Excel打开csv文件乱码问题的解决办法

    excel打开csv 出现乱码怎么解决 https://jingyan.baidu.com/article/ac6a9a5e4c681b2b653eacf1.html CSV是逗号分隔值的英文缩写,通 ...

  10. nodejs 重定向 (redirect + writeHead(Location))

    参考: Node.js实现301.302重定向服务 Express URL跳转(重定向)的实现:res.location()与res.redirect() 一 方式1 index.js var htt ...