在网上很多都把Advice翻译成增强器,其实从翻译工具上看到,这个单词翻译是忠告,通知的意思。

首先这个注解实在Spring Web包下,而Spring MVC离不开Spring Web的依赖,所以经常两个一起使用的。

题外:如果在asp.net webform下可以做这样的事情有两处地方,全部集成Base或者Globals去拦截,而在asp.net webmvc下提供了指定类进行注入,Globals同样也有,并且可以实现,但提倡这个,mvc已经有了新的类实现全局,以上是dotnet的等等。

@ControllerAdvice的做可以可以全局拦截指定的异常,并做想要的包装处理,比如跳转到别的页面,或者返回指定的数据格式等等。

下面是通过这个特定实现的简单拦截:

1、新建MyControllerAdvice类

package com.jsoft.springboottest.springboottest1.controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.NativeWebRequest; import java.util.HashMap;
import java.util.Map; /**
* Controller增强器
*
* @author jim
* @date 2017/11/23
*/
@ControllerAdvice
public class MyControllerAdvice { @ModelAttribute
public void changeModel(Model model) {
System.out.println("============应用到所有@RequestMapping注解方法,在其执行之前把返回值放入Model");
model.addAttribute("author", "Jim");
} @InitBinder
public void initBinder(WebDataBinder binder) {
System.out.println("============应用到所有@RequestMapping注解方法,在其执行之前初始化数据绑定器");
} /**
* 设置要捕获的异常,并作出处理
* 注意:这里可以返回试图,也可以放回JSON,这里就当做一个Controller使用
*
* @param request {@link NativeWebRequest}
* @param e {@link Exception}
* @return {@link Map}
*/
@ExceptionHandler(value = Exception.class)
@ResponseBody
public Map processUnauthenticatedException(NativeWebRequest request, Exception e) {
System.out.println("===========应用到所有@RequestMapping注解的方法,在其抛出Exception异常时执行");
Map map = new HashMap(5);
map.put("code", 404);
map.put("msg", e.getMessage());
return map;
}
}

2、测试异常,模拟抛出异常

    @RequestMapping("/show")
public String show() throws Exception {
throw new Exception("测试异常");
}

测试效果:

测试工程:https://github.com/easonjim/5_java_example/tree/master/springboottest/springboottest9

参考:

http://jinnianshilongnian.iteye.com/blog/1866350

https://www.cnblogs.com/magicalSam/p/7198420.html

Spring MVC中@ControllerAdvice注解实现全局异常拦截的更多相关文章

  1. Spring MVC 中 @ModelAttribute 注解的妙用

    Spring MVC 中 @ModelAttribute 注解的妙用 Spring MVC 提供的这种基于注释的编程模型,极大的简化了 web 应用的开发.其中 @Controller 和 @Rest ...

  2. Spring MVC 中采用注解方式 Action中跳转到另一个Action的写法

    Spring MVC 中采用注解方式 Action中跳转到另一个Action的写法 在Action中方法的返回值都是字符串行,一般情况是返回某个JSP,如: return "xx" ...

  3. spring 以及 spring mvc 中常用注解整理

    spring 以及 spring mvc 中常用注解整理 @RequestMapping(映射路径) @Autowired(注入 bean 对象) 例如: @Autowired private Bas ...

  4. Spring MVC中基于注解的 Controller

         终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 HandlerMapping 来映射出相应的 handler 并调用相应的方法以响 ...

  5. 0001 - Spring MVC中的注解

    1.概述 Spring MVC框架提供了功能强大的注解,大大简化了代码开发的同时也提升了程序的可扩展性 2.注解 2.1.@RequestMapping Spring MVC通过@RequestMap ...

  6. Spring MVC中@RequestMapping注解使用技巧(转)

    @RequestMapping是Spring Web应用程序中最常被用到的注解之一.这个注解会将HTTP请求映射到MVC和REST控制器的处理方法上. 在这篇文章中,你将会看到@RequestMapp ...

  7. 在Spring MVC中使用注解的方式校验RequestParams

    概述   Spring MVC支持Bean Validation,通过这个验证技术,可以通过注解方式,很方便的对输入参数进行验证,之前使用的校验方式,都是基于Bean对象的,但是在@RequestPa ...

  8. spring mvc中的注解说明

    注解扫描 context:component-scan 包扫描 <context:component-scan base-package="org.bdp"> < ...

  9. Spring MVC 中的基于注解的 Controller【转】

    原文地址:http://my.oschina.net/abian/blog/128028 终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 H ...

随机推荐

  1. poj-2524 ubiquitous religions(并查集)

    Time limit5000 ms Memory limit65536 kB There are so many different religions in the world today that ...

  2. HUD:2853-Assignment(KM算法+hash)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2853 Assignment Time Limit: 2000/1000 MS (Java/Others) ...

  3. HDU 5399 数学 Too Simple

    题意:有m个1~n的映射,而且对于任意的 i 满足 f1(f2(...fm(i))) = i 其中有些映射是知道的,有些是不知道的,问一共有多少种置换的组合. 分析: 首先这些置换一定是1~n的一个置 ...

  4. windows :Tomcat免安装版环境变量配置 + jdk配置

    1.  下载后解压,我解压的目录为:D:\Tomcat\apache-tomcat-9.0.1-windows-x64 2.  安装jdk和jre, 并配置环境变量: 2.1 用户变量新建JAVA_H ...

  5. 大数据学习——KETTLE入门学习——kettle安装

    https://blog.csdn.net/u012637358/article/details/82593492 下载的kettle是汉化的 改成英文的 工具——选项——选择英文

  6. python - 接口自动化测试 - ReadExcel - 读取测试数据封装

    # -*- coding:utf-8 -*- ''' @project: ApiAutoTest @author: Jimmy @file: read_excel.py @ide: PyCharm C ...

  7. 洛谷9月月赛II 赛后瞎写

    看错比赛时间了....结果发现的时候已经开始了半个小时,并且当时正准备睡午觉qwq 于是就水了个t1就 去睡 跑了 T2 写着写着然后看了一发评讲被辣鸡思路给绕了进去最后发现自己宛若一个智障 类似桶的 ...

  8. Good Bye 2017

    太菜了啊,一不小心就goodbye rating了 A. New Year and Counting Cards time limit per test 1 second memory limit p ...

  9. 让Android软键盘默认进入英文键盘

    今天在做一个功能的 时候,需要输入法软键盘弹出后,需要进入英文输入界面. 可以通过设置EditText的输入类型为EMAIL来实现.     //将输入法切换到英文     edit.setInput ...

  10. 将模型储存到本地-FastCoder

    // // ViewController.m // 模型转data储存 // // Created by 谭启宏 on 16/3/4. // Copyright © 2016年 tqh. All ri ...