使用@ControllerAdvice实现发生异常时返回json错误信息
来源:互联网 发布:台湾谈大陆2016网络 编辑:程序博客网 时间:2024/04/30 13:51
前言
本文介绍spring MVC的自定义异常处理,即在Controller中抛出自定义的异常时,客户端收到更友好的JSON格式的提示,而不是常见的报错页面,方便移动端的处理。
注解介绍
@ControllerAdvice注解,是spring3.2提供的新注解,从名字上可以看出大体意思是控制器增强,可以用来通过结合注解@ExceptionHanlder, 来定义具体的异常处理, 以及返回的结果。
@ControllerAdvice
使@ControllerAdvice注解生效有两种方式:
- 1.额外添加@EnableWebMvc注解
@EnableWebMvc @ControllerAdvice public class BaseController { @ExceptionHandler() public void handleException(Exception e) { System.out.println("rrrrrrrrrrrrr"); } }
- 2.spring-mvc.xml文件中添加注解驱动器
<mvc:annotation-driven />
使用@ExceptionHandler注解可以指定spring捕获到所有抛出的CustomException异常,由这个被注解的方法处理
@ExceptionHandler(value = CustomException.class)
使用@ResponseBody注解可以使Spring自动把要返回的对象转化成json文本写入到响应体中
@ResponseBody
Sample展示
创建一个GlobalExceptionHandler.java类实现全局异常处理,该类会自动拦截异常然后做相应的处理
这边需要注意的是, @ExceptionHandler可定义多个, 当多个匹配时, 按编写最早的处理函数优先处理. 这边就像try/catch那种常见的情况了, 既顺序敏感。最佳实践, 需要把处于顶层的异常类搁置到代码最尾端
@ControllerAdvicepublic class GlobalExceptionHandler { @ExceptionHandler(value = CustomException.class) @ResponseBody public ResponseWrapper handle(CustomException ex) { String message = ex.getMessage(); return ex.getErrorWrapper(0, message); } @ExceptionHandler(value = Exception.class) @ResponseBody public ResponseWrapper handle(Exception ex) { String message = ex.getMessage(); System.out.println("GlobalExceptionHandler: " + message); return ResponseWrapper.occurException(0, message); }}
返回值封装类,该类就是最后传到用户面前的最后一个类,正常时返回正常json文本,错误时返回相应的code和message异常信息
public class ResponseWrapper<T> implements Serializable { private Integer code = 1; private String errorMsg = "成功"; private T requestData; public ResponseWrapper() { } private ResponseWrapper(Integer code, String errorMsg) { this.code = code; this.errorMsg = errorMsg; } public static ResponseWrapper occurException(Integer code, String errorMsg) { return new ResponseWrapper(code, errorMsg); } public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public String getErrorMsg() { return errorMsg; } public void setErrorMsg(String errorMsg) { this.errorMsg = errorMsg; } public T getRequestData() { return requestData; } public void setRequestData(T requestData) { this.requestData = requestData; }}
CustomException类起到一个桥梁作用,遇到异常时传不同的code和message进去,然后封装成ResponseWrapper对象,剩下的就是如何优雅实现的问题了,如下是CustomException的实现:
public class CustomException extends Exception { //异常信息 private String message; private Integer code; public CustomException(String message) { super(message); this.message = message; } public ResponseWrapper getErrorWrapper(Integer code, String message) { return ResponseWrapper.occurException(code, message); } @Override public String getMessage() { return message; } public void setMessage(String message) { this.message = message; }}
0 0
- 使用@ControllerAdvice实现发生异常时返回json错误信息
- Spring Boot @ControllerAdvice 处理全局异常,返回固定格式Json
- springMVC使用@ControllerAdvice实现异常处理
- springMVC使用@ControllerAdvice实现异常处理
- Spring中的@ControllerAdvice注解配合@ExceptionHandler使用实现异常处理
- 使用Spring MVC的@ControllerAdvice注解做Json的异常处理
- 使用Spring MVC的@ControllerAdvice注解做Json的异常处理
- 使用Spring MVC的@ControllerAdvice注解做Json的异常处理
- 使用Spring MVC的@ControllerAdvice注解做Json的异常处理
- 使用Spring MVC的@ControllerAdvice注解做Json的异常处理
- 使用Spring MVC的@ControllerAdvice注解做Json的异常处理
- spring使用@ExceptionHandler、@ControllerAdvice统一异常处理
- spring mvc 使用 @ControllerAdvice 捕获404异常
- SpringMVC异常处理使用@ControllerAdvice注释
- SpringBoot 统一异常处理--- @ControllerAdvice 使用
- fastJson json转对象时发生异常
- Spring Boot实战之全局异常捕获 实现参数异常检查返回统一错误信息
- 使用ControllerAdvice获取controller接口@ResponseBody返回值
- Prism4 文档翻译系列---第3章 管理组件间的依赖关系
- Eclipse修改控制台背景色
- CSS引入样式
- 如何改造现有文件为 CMD 模块
- cocos Creator
- 使用@ControllerAdvice实现发生异常时返回json错误信息
- LeetCode 482. License Key Formatting
- 为什么说coarseGrainedExecutorBackend要通信的对象driverUrl是driverEndpoint而不是ClientEndpoint
- Print binary tree
- 南邮数据结构实验三---图的基本运算及飞机换乘次数最少问题
- Java Web项目中编写定时任务
- 采访:关于Go语言和《Go Web编程》
- 每个人,都孤独
- SDWebImage源码解读 之UIImage+GIF