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. retinex相关代码汇总

    混合方法 SSR.m matlab代码,本来是RGB,改成了处理灰度图像的. %%%%%%%%%%%%%%%RGB normalisation%%%%%%%%%%%%%%%%%%%%%% %its c ...

  2. XML高速入门

    XML是什么 Extensible Markup Language 自己定义标签: 用来数据传输: 可扩展标记语言,是一种类似超文本标记语言的标记语言. 与HTML的比較: 1.不是用来替代HTML的 ...

  3. Javascript中的感叹号和函数function

    js函数前加分号和感叹号是什么意思?有什么用?:http://www.cnblogs.com/mq0036/p/4605255.html function与感叹号:https://swordair.c ...

  4. Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'

    MySQL在linux安装之后,连接到服务,报了下面的错误: ERROR 2002 (HY000): Can't connect to local MySQL server through socke ...

  5. C++ 在继承中使用virtual

    使用virtual:如果方法是通过引用类型或指针而不是对象调用的,它将确定使用哪一种方法.如果没有使用关键字irtual,程序将根据引用类型或指针类型选择方法:如果使用了irtual,程序将根据引用或 ...

  6. Mybatis返回map集合

    <resultMap id="pieMap" type="HashMap"> <result property="value&quo ...

  7. golang 小知识-持续更新中

    Golang 中的指针 - Pointer Go 的原生数据类型可以分为基本类型和高级类型,基本类型主要包含 string, bool, int 及 float 系列,高级类型包含 struct,ar ...

  8. ubuntu 信使(iptux) 创建桌面快捷方式

    $ sudo ln -s /usr/bin/iptux ~/桌面/iptux.ln

  9. maven项目使用SOLR时报 previously initiated loading for a different type with name "javax/servlet/http/HttpServletRequest" 错的解决方法

    环境:Apache solr4.8,maven3,IntellijIDEA 想在项目中使用solr 在pom.xml文件中添加了solr的依赖 solr-core,solrj 和solr-dataim ...

  10. 【BZOJ3470】Freda’s Walk 概率与期望

    [BZOJ3470]Freda’s Walk Description 雨后的Poetic Island空气格外清新,于是Freda和Rainbow出来散步. Poetic Island的交通可以看作一 ...