背景

@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

最新文章

  1. SQL性能优化
  2. SqlServer性能优化 即席查询(十三)
  3. JS转码
  4. attachEvent和addEventListener区别
  5. poj 2887 Big String
  6. [转] 详解http和https的作用与区别
  7. Android(java)学习笔记79:java中InetAddress类概述和使用
  8. winfrom 截屏、抓屏 分类: WinForm 2014-08-01 13:02 198人阅读 评论(0) 收藏
  9. javaWeb项目(SSH框架+AJAX+百度地图API+Oracle数据库+MyEclipse+Tomcat)之一 基础Struts框架搭建篇
  10. C#对SQLite、Access数据库操作的封装,很好用的~
  11. javascript之this
  12. emWin万年历,含uCOS-III和FreeRTOS两个版本
  13. YII2 console中引用其他模块(子项目)的model时出现model找不到命名空间的问题解决
  14. C# Collection 排序
  15. PHP生成当前月份包括最近12个月内的月份
  16. WEB漏洞 XSS(一)
  17. 【NOI 2015】品酒大会
  18. etcd和redis的比较和日常使用场景
  19. try、catch、finally都有return语句时执行哪个
  20. java网络编程ServerSocket类 和Socket类的常用构造方法及其方法

热门文章

  1. SpEL表达式总结(转)
  2. Codeforces Round #707 Editorial Div2 题解
  3. nacos集群部署
  4. SpringBoot自动装配-自定义Start
  5. 【LeetCode】27.移除元素
  6. 图像处理算法的仿真平台之VGA时序
  7. 数据分析学习1-----matplotlib
  8. 在Windows7/8/10 64位操作系统下安装并注册ocx控件
  9. Springboot中mybatis执行逻辑源码分析
  10. mysql中的with rollup得到group by的汇总信息