背景

@ControllerAdvice 注解 通常用于定义@ExceptionHandler, @InitBinder@ModelAttribute 适用于所有@RequestMapping方法的方法。

@ExceptionHandler异常处理器

作用:

可以拦截程序抛出来的指定异常。

使用场景:

主要使用与项目统一异常处理,对于rest风格的返回统一异常格式。

/**
* 指定拦截异常的类型
*
* @param e
* @return json格式类型
*/
@ExceptionHandler({Exception.class}) //指定拦截异常的类型
@ResponseBody
public Object customExceptionHandler(Exception e) {
//打印异常日志
e.printStackTrace();
//非空验证异常
if(e instanceof BindException){
BindException bindException = (BindException) e;
String msg = bindException.getBindingResult().getFieldError().getDefaultMessage();
return msg;
}
if(e instanceof GlobalException){
GlobalException globalException = (GlobalException) e;
return globalException.getErrMsg();
}
return "系统异常";
}

@InitBinder

作用:

应用到所有@RequestMapping注解方法,在其执行之前初始化数据绑定器

使用场景:

1.可用于绑定作用于全局的请求参数验证器。

2.日期格式把请求中的日期字符串转换成Date类型。

   /**
* 应用到所有@RequestMapping注解方法,在其执行之前初始化数据绑定器
*
* @param binder
*/
@InitBinder
public void initWebBinder(WebDataBinder binder) {
//对日期的统一处理
// binder.addCustomFormatter(new DateFormatter("yyyy-MM-dd"));
//添加对数据的校验
//binder.setValidator();
}

自定义参数验证器使用传送门:https://www.jianshu.com/p/518a45c3d72f

@ModelAttribute

作用:

把值绑定到Model中,使全局@RequestMapping可以获取到该值

使用场景:

大家发挥自己那聪明的小脑袋吧,本人也没想到哪里实战使用!

/**
* 把值绑定到Model中,使全局@RequestMapping可以获取到该值
*
* @param model
*/
@ModelAttribute
public void addAttribute(Model model) {
model.addAttribute("msg", "hello");
}

获取参数:

  @GetMapping("test")
@ResponseBody
public Object test(@ModelAttribute("msg") String msg){
return msg;
}

完整的代码

package com.wzq.config.exception;

import org.springframework.ui.Model;
import org.springframework.validation.BindException;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*; /**
* @description: 全局异常处理类
* @author: Wzq
* @create: 2019-12-26 11:01
*/
@RestControllerAdvice
public class GlobalExceptionAdvice { /**
* 指定拦截异常的类型
*
* @param e
* @return json格式类型
*/
@ExceptionHandler({Exception.class}) //指定拦截异常的类型
@ResponseBody
public Object customExceptionHandler(Exception e) {
//打印异常日志
e.printStackTrace();
//非空验证异常
if(e instanceof BindException){
BindException bindException = (BindException) e;
String msg = bindException.getBindingResult().getFieldError().getDefaultMessage();
return msg;
}
if(e instanceof GlobalException){
GlobalException globalException = (GlobalException) e;
return globalException.getErrMsg();
}
return "系统异常";
} /**
* 应用到所有@RequestMapping注解方法,在其执行之前初始化数据绑定器
*
* @param binder
*/
@InitBinder
public void initWebBinder(WebDataBinder binder) {
//对日期的统一处理
// binder.addCustomFormatter(new DateFormatter("yyyy-MM-dd"));
//添加对数据的校验
//binder.setValidator();
} /**
* 把值绑定到Model中,使全局@RequestMapping可以获取到该值
*
* @param model
*/
@ModelAttribute
public void addAttribute(Model model) {
model.addAttribute("msg", "hello");
} }

@RestControllerAdvice 和 @ControllerAdvice区别在于@RestControllerAdvice不需要加@ResponseBody

@ControllerAdvice注解(全局异常捕获)的更多相关文章

  1. Spring-MVC开发之全局异常捕获全面解读

    异常,异常 我们一定要捕获一切该死的异常,宁可错杀一千也不能放过一个! 产品上线后的异常更要命,一定要屏蔽错误内容,以免暴露敏感信息! 在用Spring MVC开发WEB应用时捕获全局异常的方法基本有 ...

  2. springboot(二 如何访问静态资源和使用模板引擎,以及 全局异常捕获)

    在我们开发Web应用的时候,需要引用大量的js.css.图片等静态资源. 默认配置 Spring Boot默认提供静态资源目录位置需置于classpath下,目录名需符合如下规则: /static / ...

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

    spring boot: GlobalDefaultExceptionHandler方法内的友好错误提示,全局异常捕获 当你的某个控制器内的某个方法报错,基本上回显示出java错误代码,非常不友好,这 ...

  4. springboot编程之全局异常捕获

    springboot编程之全局异常捕获 1.创建GlobalExceptionHandler.java,在类上注解@ControllerAdvice, 在方法上注解@ExceptionHandler( ...

  5. SpringBoot图文教程15—项目异常怎么办?「跳转404错误页面」「全局异常捕获」

    有天上飞的概念,就要有落地的实现 概念十遍不如代码一遍,朋友,希望你把文中所有的代码案例都敲一遍 先赞后看,养成习惯 SpringBoot 图文教程系列文章目录 SpringBoot图文教程1-Spr ...

  6. 使用spring利用HandlerExceptionResolver实现全局异常捕获

    最近一直没有时间更新是因为一直在更新自己使用的框架. 之后会慢慢带来对之前使用的spring+mvc+mybatis的优化. 会使用一些新的特性,实现一些新的功能. 我会尽量分离业务,封装好再拿出来. ...

  7. MVC 好记星不如烂笔头之 ---> 全局异常捕获以及ACTION捕获

    public class BaseController : Controller { /// <summary> /// Called after the action method is ...

  8. atitit.js浏览器环境下的全局异常捕获

    atitit.js浏览器环境下的全局异常捕获 window.onerror = function(errorMessage, scriptURI, lineNumber) { var s= JSON. ...

  9. C#中的那些全局异常捕获

    1.WPF全局捕获异常     public partial class App : Application     {         public App()         {    // 在异 ...

随机推荐

  1. Spring boot中相关的注解

    一.相关类中使用的注解 @RestController:REST风格的控制器 @RequestMapping:配置URL和方法之间的映射 @SpringBootApplication:应用程序入口类 ...

  2. lua环境搭建

    前言: Linux & Mac上安装 Lua 安装非常简单,只需要下载源码包并在终端解压编译即可,本文介绍Linux 系统上,lua5.3.0版本安装步骤: ↓ 1. Linux 系统上安装 ...

  3. Socket 编程介绍

    Socket 编程发展 Linux Socket 编程领域,为了处理大量连接请求场景,需要使用非阻塞 I/O 和复用.select.poll 和 epoll 是 Linux API 提供的 I/O 复 ...

  4. ZSH主题

    All the current themes can be found in the themes/ directory in the oh-my-zsh distribution. See list ...

  5. CentOS 命令提示符

    命令提示符的设置就是对PS1的配置: export PS1="\[\e[35;40m\][\[\e[32;40m\]\u\[\e[37;40m\]@\[\e[36;40m\]\h  \[\e ...

  6. PYTHON 当前.PY文件名不能与引入的模块同名

    当前文件名:sqlite3.py 文件引入import sqlite3 运行会出错,因为调用sqlite3的方法首先从当前文件找方法,当然找不到,所以会报错了

  7. C语言中函数的返回值

    规则 除局部变量的内存地址不能作为函数的返回值外,其他类型的局部变量都能作为函数的返回值. 我总结出下面这些规则: int.char等数据类型的局部变量可以作为函数返回值. 在函数中声明的指针可以作为 ...

  8. 前端开发入门到进阶第三集【js进行url解析】

    https://www.cnblogs.com/yuanzhiguo/p/8241644.html

  9. airodump-ng的使用及显示

    PWR   表示所接收的信号的强度.表示为负数,数值赿大表示信号赿强.(绝对值赿大,数据赿值小) beacons  表示网卡接收到的AP发出的信号个数

  10. 电子物流中的EDI 应用

    电子物流中的EDI 应用 背景 EDI 全称是Electronic data interchange, 即电子数据交换.在传统企业里,很多流程上的操作或者通信一般是由纸质媒介完成的,比如说采购订单.发 ...