spring boot: GlobalDefaultExceptionHandler方法内的友好错误提示,全局异常捕获
spring boot: GlobalDefaultExceptionHandler方法内的友好错误提示,全局异常捕获
当你的某个控制器内的某个方法报错,基本上回显示出java错误代码,非常不友好,这个时候可以通过新建GlobalDefaultExceptionHandler.java文件,
1.加上@ControllerAdvice注解,
2. 然后复写defaultExceptionHandler方法,在方法上添加@ResponseBody输出注解,
以及@ExceptionHandler(Exception.class)注解,就能友好的已文字的信息显示错误信息l
package com.muyang.boot22.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; @ControllerAdvice
public class GlobalDefaultExceptionHandler { @ExceptionHandler(Exception.class)
@ResponseBody
public String defaultExceptionHandler(HttpServletRequest req,Exception e){
//是返回的String. //ModelAndView -- 介绍 模板引擎...?
// ModelAndView mv = new ModelAndView();
// mv.setViewName(viewName); return "对不起,服务器繁忙,请稍后再试!";
} }
参考项目:

pom.xml参考代码
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties> <dependencies> <!-- spring mvc,aop,restful,fastjson -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <!-- jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency> <!-- tomcat的支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency> <!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency> <!-- 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency> </dependencies> <build>
<plugins> <!-- 热部署 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!--fork : 如果没有该项配置,呢个devtools不会起作用,即应用不会restart -->
<fork>true</fork>
</configuration>
</plugin> </plugins>
</build>
App.java参考代码
package com.muyang.boot22; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.HttpMessageConverter; import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; /**
* Hello world!
*
*/
@SpringBootApplication
public class App
{ //fastJson配置
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters()
{ FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); fastConverter.setFastJsonConfig(fastJsonConfig); HttpMessageConverter<?> converter = fastConverter;
return new HttpMessageConverters(converter); } public static void main( String[] args )
{
//System.out.println( "Hello World!" );
SpringApplication.run(App.class, args);
}
}
HelloController.java样例
package com.muyang.boot22.controller; import java.util.Map; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
public class HelloController
{ @RequestMapping("/hello")
public void index(Map<String, Object>map)
{
//map.put("name", "张三");
//return "hello";
int i = 1024/0;
} }
运行App.java
访问 http://localhost:8080/hello时,报错。能友好提示
spring boot: GlobalDefaultExceptionHandler方法内的友好错误提示,全局异常捕获的更多相关文章
- Spring Boot部署方法
Spring Boot部署方法 网上搜到的部署方法无非是打成jar包,然后shell执行nohup java调用jar命令,或者是打成war包然后部署到tomcat或者jetty容器上面. S ...
- SpringBoot图文教程15—项目异常怎么办?「跳转404错误页面」「全局异常捕获」
有天上飞的概念,就要有落地的实现 概念十遍不如代码一遍,朋友,希望你把文中所有的代码案例都敲一遍 先赞后看,养成习惯 SpringBoot 图文教程系列文章目录 SpringBoot图文教程1-Spr ...
- 使用spring利用HandlerExceptionResolver实现全局异常捕获
最近一直没有时间更新是因为一直在更新自己使用的框架. 之后会慢慢带来对之前使用的spring+mvc+mybatis的优化. 会使用一些新的特性,实现一些新的功能. 我会尽量分离业务,封装好再拿出来. ...
- SpringBoot 源码解析 (六)----- Spring Boot的核心能力 - 内置Servlet容器源码分析(Tomcat)
Spring Boot默认使用Tomcat作为嵌入式的Servlet容器,只要引入了spring-boot-start-web依赖,则默认是用Tomcat作为Servlet容器: <depend ...
- Spring Boot项目的内嵌容器
一.关于容器 刚才开始使用spring boot的开发者会有种很直观的感觉,servlet容器“不见了”.之前开发web项目,都是把程序写完后部署到servlet容器(比如Tomcat),但是使用sp ...
- Spring Boot提供RESTful接口时的错误处理实践
使用Spring Boot开发微服务的过程中,我们会使用别人提供的接口,也会设计接口给别人使用,这时候微服务应用之间的协作就需要有一定的规范. 基于rpc协议,我们一般有两种思路:(1)提供服务的应用 ...
- Spring Boot移除内嵌Tomcat,使用非web方式启动
前言:当我们使用Spring Boot编写了一个批处理应用程序,该程序只是用于后台跑批数据,此时不需要内嵌的tomcat,简化启动方式使用非web方式启动项目,步骤如下: 1.在pom.xml文件中去 ...
- Spring Boot run()方法剖析
Spring Boot自动配置部分重点介绍了相关注解,关于main中调用的run方法并没有阐述过.run方法的作用是什么呢?只有注解没有main里的run方法Spring Boot工程就好比身体个方面 ...
- Spring Boot 发布方法 - 原创
发布方式 构建Jar包,cmd命令行运行Spring Boot程序 第一步:在pom.xml中将packing节点值修改为jar,如下面加粗部分: <groupId>com.example ...
随机推荐
- zw版【转发·台湾nvp系列Delphi例程】HALCON HistoToThresh2
zw版[转发·台湾nvp系列Delphi例程]HALCON HistoToThresh2 procedure TForm1.Button1Click(Sender: TObject);var imag ...
- uva10167
/* 暴力 过了 要使得两半的 樱桃数目相等 去试每一个斜率 还好他这里要的是 A.B 都为正整数 这样范围就锁定在200*100 个点范围内 */ #include <cstdio> # ...
- BabelMap 12.0.0.1 汉化版(2019年3月11日更新)
软件简介 BabelMap 是一个免费的字体映射表工具,可辅助使用<汉字速查>程序. 该软件可使用系统上安装的所有字体浏览 Unicode 中的十万个字符,还带有拼音及部首检字法,适合文献 ...
- Repeater 控件使用总结
关于Repeater控件使用的一些总结,希望能对将来有机会看到这篇日志的同事有所帮助.也是为了在自己开发有所遗忘的时候能够参考一下.前言:Repeater是一个迭代控件,什么是迭代控件呢?书本上的 ...
- python 常见脚本
一登录就发现了这篇博客,非常感谢作者,有时间会静下心来一点一滴的看 https://www.cnblogs.com/ailiailan/p/10141741.html
- linux常用命令:tar 命令
通过SSH访问服务器,难免会要用到压缩,解压缩,打包,解包等,这时候tar命令就是是必不可少的一个功能强大的工具.linux中最流行的tar是麻雀虽小,五脏俱全,功能强大. tar 命令可以为linu ...
- Hive 常用优化参数
常用调优测试语句 : ①显示当前hive环境的参数值: set 参数名; 如: hive> set mapred.map.tasks;mapred.map.tasks; ②设置hi ...
- Linux配置自动时间同步
Linux配置自动时间同步时间同步命令:ntpdate -s time.windows.com自动时间同步:让linux从time.windows.com自动同步时间vi /etc/crontab加上 ...
- 利用arcgis处理遥感栅格数据,得到省平均值数据
1.准备全国省级行政区数据,需要有省级行政区信息,如下所示: 2.生成渔网数据,操作完成会生成一个面数据和一个点数据,我们主要用点数据进行后面的操作. 3.提取栅格数据的值到渔网点数据中. 4.将区域 ...
- office2007-安装程序找不到office.zh-cn\*.文件
在同时安装vs2008 和 office2007 有可能会遇到个问题(如果现在安装office2007,在安装vs2008 不会出现问题, 反正会出现下面问题) 在安装office2007时总是提示“ ...