spring boot: GlobalDefaultExceptionHandler方法内的友好错误提示,全局异常捕获

Posted 穆晟铭

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 应用程序 pom 同时需要 spring-boot-starter-parent 和 spring-boot-starter-web?

《02.Spring Boot连载:Spring Boot实战.Spring Boot核心原理剖析》

spring-boot-quartz, 依赖spring-boot-parent

spring-boot系列:初试spring-boot

Spring-Boot Banner

Spring Boot:Spring Boot启动原理分析