spring boot配置统一异常处理
基于@ControllerAdvice的统一异常处理
>.这里ServerException是我自定义的异常,和普通Exception分开处理
>.这里的RequestResult是我自定义的请求返回结果对象
ExceptionResolver.class
import com.dawn.blogspot.common.exception.ServerException;
import com.dawn.blogspot.common.response.RequestResult;
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; /**
* @author TangZedong
* @apiNote 统一异常处理器
* @since 2018/5/21 11:55
*/
@ControllerAdvice
public class ExceptionResolver {
// spring boot 2.0.4版本内部集成了log4j,所以不需要额外的配置log4j
private final static Logger LOGGER = LoggerFactory.getLogger(ExceptionResolver.class); /**
* 处理自定义异常{@link ServerException},并返回错误信息
*/
@ResponseBody
@ExceptionHandler(value = ServerException.class)
public RequestResult ServerException(ServerException e) {
return RequestResult.getFailedInstance(e.getCode(), e.getMessage() == null ? "系统异常" : e.getMessage());
} /**
* 处理自定义异常{@link ServerException},并返回错误信息
*/
@ResponseBody
@ExceptionHandler(value = Exception.class)
public RequestResult ServerException() {
return RequestResult.getFailedInstance("系统异常");
} }
ServerException.class
/**
* @author TangZedong
* @apiNote 服务异常
* @since 2018/9/4 15:27
*/
public class ServerException extends RuntimeException {
// 错误代码
private short code = -1; public short getCode() {
return code;
} public ServerException(String message) {
super(message);
} public ServerException(short code, String message) {
super(message);
this.code = code;
} public ServerException(short code, String message, Throwable t) {
super(message, t);
this.code = code;
} }
RequestResult.class
/**
* @author TangZedong
* @apiNote 请求结果
* @since 2018/9/4 16:20
*/
public class RequestResult<T> {
private static final short SUCCESS_CODE = 0;
private static final short FAILED_CODE = -1; private static final boolean SUCCESS_STATUS = true;
private static final boolean FAILED_STATUS = false; private static final String NULL_MESSAGE = null;
private static final String NULL_DATA = null; private short code;
private String message;
private boolean success;
private T data; // 构造方法
public RequestResult(short code, String message, boolean success, T data) {
this.code = code;
this.message = message;
this.success = success;
this.data = data;
} // get方法
public short getCode() {
return code;
} public String getMessage() {
return message;
} public T getData() {
return data;
} public boolean isSuccess() {
return success;
} // 获取实例
public static <T> RequestResult getFailedInstance(short code, String message, T data) {
return new RequestResult(code, message, FAILED_STATUS, data);
} public static <T> RequestResult getFailedInstance(short code, String message) {
return new RequestResult(code, message, FAILED_STATUS, NULL_DATA);
} public static <T> RequestResult getFailedInstance(String message) {
return new RequestResult(FAILED_CODE, message, FAILED_STATUS, NULL_DATA);
} public static <T> RequestResult getSuccessInstance(short code, String message, T data) {
return new RequestResult(code, message, SUCCESS_STATUS, data);
} public static <T> RequestResult getSuccessInstance(short code, T data) {
return new RequestResult(code, NULL_MESSAGE, SUCCESS_STATUS, data);
} public static <T> RequestResult getSuccessInstance(T data) {
return new RequestResult(SUCCESS_CODE, NULL_MESSAGE, SUCCESS_STATUS, data);
} }
spring boot配置统一异常处理的更多相关文章
- 基于Spring Boot的统一异常处理设计
基于Spring Boot的统一异常处理设计 作者: Grey 原文地址:https://www.cnblogs.com/greyzeng/p/11733327.html Spring Boot中,支 ...
- spring boot 中统一异常处理
基于 spring boot 对异常处理的不友好,现在通过其他的方式来统一处理异常 步骤一:自定义异常类 public class UserNotExistException extends Runt ...
- 基于spring boot的统一异常处理
一.springboot的默认异常处理 Spring Boot提供了一个默认的映射:/error,当处理中抛出异常之后,会转到该请求中处理,并且该请求有一个全局的错误页面用来展示异常内容. 例如这里我 ...
- Spring Boot实践——统一异常处理
注解说明 @ControllerAdvice,是Spring3.2提供的新注解,从名字上可以看出大体意思是控制器增强.让我们先看看@ControllerAdvice的实现: /** * Special ...
- Spring Boot学习——统一异常处理
本随笔记录使用Spring Boot统一处理异常. 本文实例是从数据库中根据ID查询学生信息,要求学生的年龄在14——20岁之间.小于14岁,提示“你可能在上初中”:大于20岁,提示“呢可能在上大学” ...
- 【Spring Boot】Spring Boot之统一异常处理
一.统一异常处理的作用 在web应用中,请求处理时,出现异常是非常常见的.所以当应用出现各类异常时,进行异常的统一捕获或者二次处理(比如空指针异常或sql异常正常是不能外抛)是非常必要的,然后右统一异 ...
- spring boot 2 统一异常处理
spring mvc 针对controller层异常统一处理非常简单,使用 @RestControllerAdvice 或 @RestControllerAdvice 注解就可以轻@RestContr ...
- Spring Boot配置过滤器的两种方式
过滤器(Filter)是Servlet中常用的技术,可以实现用户在访问某个目标资源之前,对访问的请求和响应进行拦截,常用的场景有登录校验.权限控制.敏感词过滤等,下面介绍下Spring Boot配置过 ...
- Spring Boot API 统一返回格式封装
今天给大家带来的是Spring Boot API 统一返回格式封装,我们在做项目的时候API 接口返回是需要统一格式的,只有这样前端的同学才可对接口返回的数据做统一处理,也可以使前后端分离 模式的开发 ...
随机推荐
- Loadrunner 脚本开发-从文件读取数据并参数化
脚本开发-从文件读取数据并参数化 by:授客 QQ:1033553122 直接上代码: char* testfn() { int count, total = 0; //char buffer[1 ...
- vue axios 发送post请求,后端接收参数为null
1首先检查自己的传参方式是否正确,我是传一个对象,没有问题,接口也触发了 2查了下资料说是 Content-Type的问题,设置为 'application/x-www-form-urlencod ...
- Universal-ImageLoader,Picasso,Fresco,Glide对比
Universal-ImageLoader:(估计由于HttpClient被Google放弃,作者就放弃维护这个框架)优点:1.支持下载进度监听2.可以在 View 滚动中暂停图片加载,通过 Paus ...
- redis介绍 (8) window 下redis的集群(cluster命令)
前言: 前段时间我在centos上搭建过一次redis集群,那是借助ruby搭建,这次我介绍一种纯redis集群命令的方式去搭建[最后我会简单介绍ruby搭建]. redis集群搭建(三主三备): 准 ...
- Spring Boot 中配置文件application.properties使用
一.配置文档配置项的调用(application.properties可放在resources,或者resources下的config文件夹里) package com.my.study.contro ...
- python第六天 函数 python标准库实例大全
今天学习第一模块的最后一课课程--函数: python的第一个函数: 1 def func1(): 2 print('第一个函数') 3 return 0 4 func1() 1 同时返回多种类型时, ...
- JavaScript高级特性-数组
1. JavaScript中的数组 在C++.Java中,数组是一种高效的数据结构,随机访问性能特别好,但是局限性也特别明显,就是数组中存放的数据必须是同一类型的,而在JavaScript中,数组中的 ...
- Android 用webService产生java.lang.ClassCastException: org.ksoap2.serialization.SoapPrimitive错误的解决(转)
在做android Webservice开发的时候一般情况下大家接受webservice服务器返回值的时候都是使用 SoapObject soapObject = (SoapObject) enve ...
- IO流(字节流,字符流,缓冲流)
一:IO流的分类(组织架构) 根据处理数据类型的不同分为:字节流和字符流 根据数据流向不同分为:输入流和输出流 这么庞大的体系里面,常用的就那么几个,我们把它们抽取出来,如下图: 二:字符字节 ...
- golang xorm框架的使用
1.创建engine engine, err := xorm.NewEngine(driverName, dataSourceName) 上述代码创建了一个数据库引擎,可以在一个程序中创建多个engi ...