注解说明

  @ControllerAdvice,是Spring3.2提供的新注解,从名字上可以看出大体意思是控制器增强。让我们先看看@ControllerAdvice的实现:

/**
* Specialization of {@link Component @Component} for classes that declare
* {@link ExceptionHandler @ExceptionHandler}, {@link InitBinder @InitBinder}, or
* {@link ModelAttribute @ModelAttribute} methods to be shared across
* multiple {@code @Controller} classes.
*
* <p>Classes with {@code @ControllerAdvice} can be declared explicitly as Spring
* beans or auto-detected via classpath scanning. All such beans are sorted via
* {@link org.springframework.core.annotation.AnnotationAwareOrderComparator
* AnnotationAwareOrderComparator}, i.e. based on
* {@link org.springframework.core.annotation.Order @Order} and
* {@link org.springframework.core.Ordered Ordered}, and applied in that order
* at runtime. For handling exceptions, an {@code @ExceptionHandler} will be
* picked on the first advice with a matching exception handler method. For
* model attributes and {@code InitBinder} initialization, {@code @ModelAttribute}
* and {@code @InitBinder} methods will also follow {@code @ControllerAdvice} order.
*
* <p>Note: For {@code @ExceptionHandler} methods, a root exception match will be
* preferred to just matching a cause of the current exception, among the handler
* methods of a particular advice bean. However, a cause match on a higher-priority
* advice will still be preferred to a any match (whether root or cause level)
* on a lower-priority advice bean. As a consequence, please declare your primary
* root exception mappings on a prioritized advice bean with a corresponding order!
*
* <p>By default the methods in an {@code @ControllerAdvice} apply globally to
* all Controllers. Use selectors {@link #annotations()},
* {@link #basePackageClasses()}, and {@link #basePackages()} (or its alias
* {@link #value()}) to define a more narrow subset of targeted Controllers.
* If multiple selectors are declared, OR logic is applied, meaning selected
* Controllers should match at least one selector. Note that selector checks
* are performed at runtime and so adding many selectors may negatively impact
* performance and add complexity.
*
* @author Rossen Stoyanchev
* @author Brian Clozel
* @author Sam Brannen
* @since 3.2
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface ControllerAdvice { /**
* Alias for the {@link #basePackages} attribute.
* <p>Allows for more concise annotation declarations e.g.:
* {@code @ControllerAdvice("org.my.pkg")} is equivalent to
* {@code @ControllerAdvice(basePackages="org.my.pkg")}.
* @since 4.0
* @see #basePackages()
*/
@AliasFor("basePackages")
String[] value() default {}; /**
* Array of base packages.
* <p>Controllers that belong to those base packages or sub-packages thereof
* will be included, e.g.: {@code @ControllerAdvice(basePackages="org.my.pkg")}
* or {@code @ControllerAdvice(basePackages={"org.my.pkg", "org.my.other.pkg"})}.
* <p>{@link #value} is an alias for this attribute, simply allowing for
* more concise use of the annotation.
* <p>Also consider using {@link #basePackageClasses()} as a type-safe
* alternative to String-based package names.
* @since 4.0
*/
@AliasFor("value")
String[] basePackages() default {}; /**
* Type-safe alternative to {@link #value()} for specifying the packages
* to select Controllers to be assisted by the {@code @ControllerAdvice}
* annotated class.
* <p>Consider creating a special no-op marker class or interface in each package
* that serves no purpose other than being referenced by this attribute.
* @since 4.0
*/
Class<?>[] basePackageClasses() default {}; /**
* Array of classes.
* <p>Controllers that are assignable to at least one of the given types
* will be assisted by the {@code @ControllerAdvice} annotated class.
* @since 4.0
*/
Class<?>[] assignableTypes() default {}; /**
* Array of annotations.
* <p>Controllers that are annotated with this/one of those annotation(s)
* will be assisted by the {@code @ControllerAdvice} annotated class.
* <p>Consider creating a special annotation or use a predefined one,
* like {@link RestController @RestController}.
* @since 4.0
*/
Class<? extends Annotation>[] annotations() default {}; }

该注解使用@Component注解,这样的话当我们使用<context:component-scan>扫描时也能扫描到。

通过类型的描述,可以得知几点:

  1. @ControllerAdvice是一个@Component,用于定义@ExceptionHandler,@InitBinder和@ModelAttribute方法,适用于所有使用@RequestMapping方法。
  2. Spring4之前,@ControllerAdvice在同一调度的Servlet中协助所有控制器。Spring4已经改变:@ControllerAdvice支持配置控制器的子集,而默认的行为仍然可以利用。
  3. 在Spring4中, @ControllerAdvice通过annotations(), basePackageClasses(), basePackages()方法定制用于选择控制器子集

不过据经验之谈,只有配合@ExceptionHandler最有用,其它两个不常用。

如果单使用@ExceptionHandler,只能在当前Controller中处理异常。但当配合@ControllerAdvice一起使用的时候,就可以摆脱那个限制了。

实现方式

一、统一异常处理中心(Handler)

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody; import com.alibaba.fastjson.JSONObject; /**
* 统一异常处理
* @ClassName: GlobalExceptionHandler
* @Description: TODO
* @author OnlyMate
* @Date 2018年8月29日 下午2:19:42
*
*/
@ControllerAdvice
public class GlobalExceptionHandler {
private Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class); /**
* 处理所有不可知的异常
* @Title: handleException
* @Description: TODO
* @Date 2018年8月29日 下午2:21:10
* @author OnlyMate
* @param e
* @return
*/
@ExceptionHandler(Exception.class)
@ResponseBody
public JSONObject handleException(HttpServletRequest req, Exception e){
JSONObject jsonObject = new JSONObject();
Throwable throwable = e.getCause();
        logger.error(throwable.getMessage(), e);
        if (throwable instanceof DuplicateSubmitException) {
         jsonObject.put("resultCode", ((DuplicateSubmitException) throwable).getCode());
jsonObject.put("resultMsg", ((DuplicateSubmitException) throwable).getMsg());
        } else {
            jsonObject.put("resultCode", "9999");
jsonObject.put("resultMsg", "系统异常");
        }
return jsonObject;
} /**
* 处理自定义异常(这里处理的异常是继承RunTimeException的自定义的异常)
* @Title: handleBusinessException
* @Description: TODO
* @Date 2018年8月29日 下午2:24:55
* @author OnlyMate
* @param e
* @return
*/
@ResponseBody
@ExceptionHandler(CustomBizRuntimeException.class)
public JSONObject handleBusinessException(CustomBizRuntimeException e){
logger.error("handleBusinessException ==> "+ e.getErrorMessage(), e);
JSONObject jsonObject = new JSONObject();
jsonObject.put("resultCode", "9999");
jsonObject.put("resultMsg", "CustomBizRuntimeException 自定义业务异常类 ==> 系统异常");
return jsonObject;
} /**
* 处理自定义异常(这里处理的异常是继承Exception的自定义的异常)
* @Title: handleCommonException
* @Description: TODO
* @Date 2018年8月29日 下午2:24:55
* @author OnlyMate
* @param e
* @return
*/
@ResponseBody
@ExceptionHandler(CustomCommonException.class)
public JSONObject handleCommonException(CustomCommonException e){
logger.error("handleCommonException ==> "+ e.getErrorMessage(), e);
JSONObject jsonObject = new JSONObject();
jsonObject.put("resultCode", "9999");
jsonObject.put("resultMsg", "CustomCommonException 自定义普通异常类 ==> 系统异常");
return jsonObject;
}
}

二、自定义异常

CustomBizRuntimeException,继承RuntimeException,可用于涉及事务操作时抛该异常回滚事务

/**
* 自定义业务异常类
* @ClassName: CustomBizRuntimeException
* @Description: TODO
* @author OnlyMate
* @Date 2018年9月1日 下午4:17:49
*
*/
public class CustomBizRuntimeException extends RuntimeException {
private static final long serialVersionUID = -8021190492897458166L;
private String errorMessage = "CustomBizRuntimeException 自定义业务异常类 "; public String getErrorMessage() {
return this.errorMessage;
}
}

CustomCommonException,普通自定义异常

/**
* 自定义普通异常类
* @ClassName: CustomCommonException
* @Description: TODO
* @author OnlyMate
* @Date 2018年9月1日 下午4:17:49
*
*/
public class CustomCommonException extends Exception {
private static final long serialVersionUID = -8021190492897458166L;
private String errorMessage = "CustomCommonException 自定义普通异常类"; public String getErrorMessage() {
return this.errorMessage;
}
}

三、编写抛出对应异常的Controller

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import com.onlymate.springboot.exception.CustomBizRuntimeException;
import com.onlymate.springboot.exception.CustomCommonException; @Controller
@RequestMapping(value="/exception")
public class ExceptionController {
private Logger logger = LoggerFactory.getLogger(HelloWorldController.class); /**
* 抛出CustomCommonException异常,然后处理该异常
* @Title: index1
* @Description: TODO
* @Date 2018年9月1日 下午4:42:58
* @author OnlyMate
* @return
* @throws CustomCommonException
*/
@ResponseBody
@RequestMapping("/index1")
public String index1() throws CustomCommonException {
try {
String str = null;
logger.info(str.toString());
} catch (Exception e) {
throw new CustomCommonException();
}
return "Success";
} /**
* 抛出CustomBizRuntimeException异常,然后处理该异常
* @Title: index2
* @Description: TODO
* @Date 2018年9月1日 下午4:43:25
* @author OnlyMate
* @return
*/
@ResponseBody
@RequestMapping("/index2")
public String index2() {
try {
String str = null;
logger.info(str.toString());
} catch (Exception e) {
throw new CustomBizRuntimeException();
}
return "Success";
} /**
* 抛出Exception异常,然后处理该异常
* @Title: index3
* @Description: TODO
* @Date 2018年9月1日 下午4:43:49
* @author OnlyMate
* @return
*/
@ResponseBody
@RequestMapping("/index3")
public String index3() {
String str = null;
logger.info(str.toString());
return "Success";
}
}

效果图

一、抛出CustomCommonException异常效果图

页面访问:http://localhost:8088/springboot/exception/index1

二、抛出CustomBizRuntimeException异常效果图

页面访问:http://localhost:8088/springboot/exception/index2

三、抛出Exception异常效果图

页面访问:http://localhost:8088/springboot/exception/index3

Spring Boot实践——统一异常处理的更多相关文章

  1. 基于Spring Boot的统一异常处理设计

    基于Spring Boot的统一异常处理设计 作者: Grey 原文地址:https://www.cnblogs.com/greyzeng/p/11733327.html Spring Boot中,支 ...

  2. spring boot配置统一异常处理

    基于@ControllerAdvice的统一异常处理 >.这里ServerException是我自定义的异常,和普通Exception分开处理 >.这里的RequestResult是我自定 ...

  3. spring boot 中统一异常处理

    基于 spring boot 对异常处理的不友好,现在通过其他的方式来统一处理异常 步骤一:自定义异常类 public class UserNotExistException extends Runt ...

  4. 基于spring boot的统一异常处理

    一.springboot的默认异常处理 Spring Boot提供了一个默认的映射:/error,当处理中抛出异常之后,会转到该请求中处理,并且该请求有一个全局的错误页面用来展示异常内容. 例如这里我 ...

  5. Spring Boot学习——统一异常处理

    本随笔记录使用Spring Boot统一处理异常. 本文实例是从数据库中根据ID查询学生信息,要求学生的年龄在14——20岁之间.小于14岁,提示“你可能在上初中”:大于20岁,提示“呢可能在上大学” ...

  6. 【Spring Boot】Spring Boot之统一异常处理

    一.统一异常处理的作用 在web应用中,请求处理时,出现异常是非常常见的.所以当应用出现各类异常时,进行异常的统一捕获或者二次处理(比如空指针异常或sql异常正常是不能外抛)是非常必要的,然后右统一异 ...

  7. spring boot 2 统一异常处理

    spring mvc 针对controller层异常统一处理非常简单,使用 @RestControllerAdvice 或 @RestControllerAdvice 注解就可以轻@RestContr ...

  8. Spring Boot实践——AOP实现

    借鉴:http://www.cnblogs.com/xrq730/p/4919025.html     https://blog.csdn.net/zhaokejin521/article/detai ...

  9. Spring Boot API 统一返回格式封装

    今天给大家带来的是Spring Boot API 统一返回格式封装,我们在做项目的时候API 接口返回是需要统一格式的,只有这样前端的同学才可对接口返回的数据做统一处理,也可以使前后端分离 模式的开发 ...

随机推荐

  1. 个人知识管理系统Version1.0开发记录(07)

    模 块 复 用 原本还要测试一会的,突然出现一连串诡异的问题,比如,编译少加载个类啊,输入地址少个字母啊,改几行代码一改就是半小时啊.这是在提醒我们大脑疲倦了,所以果断小结,下次继续.这一次简单完成了 ...

  2. [转载]SQL语句练习

    .查询“生物”课程比“物理”课程成绩高的所有学生的学号: 思路: 获取所有有生物课程的人(学号,成绩) - 临时表 获取所有有物理课程的人(学号,成绩) - 临时表 根据[学号]连接两个临时表: 学号 ...

  3. HDU 4687 Boke and Tsukkomi 一般图匹配,带花树,思路,输出注意空行 难度:4

    http://acm.hdu.edu.cn/showproblem.php?pid=4687 此题求哪些边在任何一般图极大匹配中都无用,对于任意一条边i,设i的两个端点分别为si,ti, 则任意一个极 ...

  4. linux下informatica服务安装和配置

    本文中将会用infa简称代替informatica 1.安装前准备 介质名称 版本信息 描述 Informatica Powercenter 9.5.1 for Linux 64 bit 必须 Jav ...

  5. 9.2 Zynq嵌入式系统调试方法

    陆佳华书<嵌入式系统软硬件协同设计实战指南 第2版>这本书中的实例着实浪费了我不少时间.从本书第一个实例我就碰了一鼻子灰.当然显然是自己时新手的原因.首先第一个实验其实真的特别简单,为什么 ...

  6. 接口测试工具Soapui5.1.2参数化之Properties20150924

    上次用天气预报的来给大家演示了下如何创建项目.测试套件.测试用例的操作,今天演示下如何参数化,废话不多说,跟着操作即可: 1.在一个用例中有两个步骤,我们想将第一个步骤中的响应中的值,传入第二个步骤中 ...

  7. 【java基础】java关键字final

    谈到final关键字,想必很多人都不陌生,在使用匿名内部类的时候可能会经常用到final关键字.另外,Java中的String类就是一个final类,那么今天我们就来了解final这个关键字的用法.下 ...

  8. 使用promise方式来获取网络数据

    获取网络数据 let data = []; new Promise(function(resolve,reject){ axios.post('api.php').then(function(resp ...

  9. 文档内容类似项处理-Shingling

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/jituotianxia2009/article/details/25339807 集合的Jaccar ...

  10. emacs之配置5,窗口位置和大小

    emacsConfig/window-setting.el ;设置窗口位置 ( ) ;设置宽和高 () (if (eq system-type 'darwin) ()) (if (eq system- ...