(五)整合统一异常和统一返回值
项目地址:https://gitee.com/springzb/admin-boot
如果觉得不错,给个 star
简介:
这是一个基础的企业级基础后端脚手架项目,主要由springboot为基础搭建,后期整合一些基础插件例如:redis、xxl-job、flowable、minioio、easyexcel、skyWalking、rabbitmq
目录
一、统一返回值
统一返回结果 R
package cn.mesmile.admin.common.result;
import com.sun.istack.internal.Nullable;
import java.io.Serializable;
import java.util.Optional;
/**
* @author zb
* @Description 公共返回信息
*/
public class R<T> implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 状态码
*/
private int code;
/**
* 是否成功
*/
private boolean success;
/**
* 承载数据
*/
private T data;
/**
* 返回消息
*/
private String msg;
private R(IResultCode resultCode) {
this(resultCode, null, resultCode.getMessage());
}
private R(IResultCode resultCode, String msg) {
this(resultCode, null, msg);
}
private R(IResultCode resultCode, T data) {
this(resultCode, data, resultCode.getMessage());
}
private R(IResultCode resultCode, T data, String msg) {
this(resultCode.getCode(), data, msg);
}
private R(int code, T data, String msg) {
this.code = code;
this.data = data;
this.msg = msg;
this.success = ResultCode.SUCCESS.code == code;
}
public static boolean isSuccess(@Nullable R<?> result) {
return (Boolean) Optional.ofNullable(result).map((x) -> {
return ResultCode.SUCCESS.code == x.code;
}).orElse(Boolean.FALSE);
}
public static boolean isNotSuccess(@Nullable R<?> result) {
return !isSuccess(result);
}
public static <T> R<T> data(T data) {
return data(data, "操作成功");
}
public static <T> R<T> data(T data, String msg) {
return data(200, data, msg);
}
public static <T> R<T> data(int code, T data, String msg) {
return new R(code, data, data == null ? "暂无承载数据" : msg);
}
public static <T> R<T> success(String msg) {
return new R(ResultCode.SUCCESS, msg);
}
public static <T> R<T> success(IResultCode resultCode) {
return new R(resultCode);
}
public static <T> R<T> success(IResultCode resultCode, String msg) {
return new R(resultCode, msg);
}
public static <T> R<T> fail(String msg) {
return new R(ResultCode.FAILURE, msg);
}
public static <T> R<T> fail(int code, String msg) {
return new R(code, (Object)null, msg);
}
public static <T> R<T> fail(IResultCode resultCode) {
return new R(resultCode);
}
public static <T> R<T> fail(IResultCode resultCode, String msg) {
return new R(resultCode, msg);
}
public static <T> R<T> status(boolean flag) {
return flag ? success("操作成功") : fail("操作失败");
}
public int getCode() {
return this.code;
}
public boolean isSuccess() {
return this.success;
}
public T getData() {
return this.data;
}
public String getMsg() {
return this.msg;
}
public void setCode(final int code) {
this.code = code;
}
public void setSuccess(final boolean success) {
this.success = success;
}
public void setData(final T data) {
this.data = data;
}
public void setMsg(final String msg) {
this.msg = msg;
}
@Override
public String toString() {
return "R(code=" + this.getCode() + ", success=" + this.isSuccess() + ", data=" + this.getData() + ", msg=" + this.getMsg() + ")";
}
public R() {
}
}
IResultCode
package cn.mesmile.admin.common.result;
import java.io.Serializable;
/**
* @author zb
* @Description
*/
public interface IResultCode extends Serializable {
/**
* 获取结果消息
* @return 结果消息
*/
String getMessage();
/**
* 获取返回状态码
* @return 结果状态码
*/
int getCode();
}
ResultCode
package cn.mesmile.admin.common.result;
/**
* @author zb
* @Description
*/
public enum ResultCode implements IResultCode {
/**
* 返回状态码,以及返回消息
*/
SUCCESS(200, "操作成功"),
FAILURE(400, "业务异常"),
UN_AUTHORIZED(401, "请求未授权"),
CLIENT_UN_AUTHORIZED(401, "客户端请求未授权"),
NOT_FOUND(404, "404 没找到请求"),
MSG_NOT_READABLE(400, "消息不能读取"),
METHOD_NOT_SUPPORTED(405, "不支持当前请求方法"),
MEDIA_TYPE_NOT_SUPPORTED(415, "不支持当前媒体类型"),
REQ_REJECT(403, "请求被拒绝"),
INTERNAL_SERVER_ERROR(500, "服务器异常"),
PARAM_MISS(400, "缺少必要的请求参数"),
PARAM_TYPE_ERROR(400, "请求参数类型错误"),
PARAM_BIND_ERROR(400, "请求参数绑定错误"),
PARAM_VALID_ERROR(400, "参数校验失败");
final int code;
final String message;
@Override
public int getCode() {
return this.code;
}
@Override
public String getMessage() {
return this.message;
}
private ResultCode(final int code, final String message) {
this.code = code;
this.message = message;
}
}
测试
package cn.mesmile.admin.modules.system.controller;
import cn.mesmile.admin.common.result.R;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author zb
* @Description
*/
@RequestMapping("/api/v1/hello")
@RestController
public class HelloController {
@GetMapping("/get")
public R hello () {
return R.data("hello world");
}
}
启动 cn.mesmile.admin.AdminApplication 启动类,访问 localhost:8080/api/v1/hello/get
看到统一返回结果如下:
{
"code": 200,
"success": true,
"data": "hello world",
"msg": "操作成功"
}
二、统一异常处理
自定义异常
package cn.mesmile.admin.common.exceptions;
import cn.mesmile.admin.common.result.IResultCode;
import cn.mesmile.admin.common.result.ResultCode;
/**
* @author zb
* @Description 自定义业务异常
*/
public class BusinessException extends RuntimeException {
private final long serialVersionUID = 1L;
private int code = ResultCode.FAILURE.getCode();
private String msg = ResultCode.FAILURE.getMessage();
public BusinessException() {
super();
}
public BusinessException(String msg) {
super(msg);
this.msg = msg;
}
public BusinessException(IResultCode resultCode, String msg) {
super(msg);
this.code = resultCode.getCode();
this.msg = msg;
}
public BusinessException(String msg, Throwable cause) {
super(msg, cause);
this.msg = msg;
}
public BusinessException(IResultCode resultCode, String msg, Throwable cause) {
super(msg, cause);
this.code = resultCode.getCode();
this.msg = msg;
}
public BusinessException(Throwable cause) {
super(cause);
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
统一异常处理
package cn.mesmile.admin.common.handler;
import cn.mesmile.admin.common.constant.AdminConstant;
import cn.mesmile.admin.common.exceptions.*;
import cn.mesmile.admin.common.result.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
* @author zb
* @Description 全局异常拦截
* <p>
* 如果我同时捕获了父类和子类,那么到底能够被那个异常处理器捕获呢?比如 Exception 和 BusinessException
* 当然是 BusinessException 的异常处理器捕获了,精确匹配,如果没有 BusinessException 的异常处理器才会轮到它的 父亲 ,
* 父亲 没有才会到 祖父 。总之一句话, 精准匹配,找那个关系最近的
* </p>
*/
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
/**
* @param businessException 业务异常
* @return @ResponseBody
* @ExceptionHandler相当于controller的@RequestMapping 如果抛出的的是BusinessException,则调用该方法
*/
@ExceptionHandler(BusinessException.class)
public R handle(BusinessException businessException) {
// 获取指定包名前缀的异常信息,减少不必要的日志
String stackTraceByPn = getStackTraceByPn(businessException, AdminConstant.basePackage);
log.error("记录业务异常信息: 消息{} 编码{} {}", businessException.getMessage(), businessException.getCode(), stackTraceByPn);
return R.fail(businessException.getCode(), businessException.getMessage());
}
/**
* 日志信息: 业务异常在全局处理的时候只记录了 指定头开始的日志,减少了堆栈信息
*/
private String getStackTraceByPn(Throwable e, String packagePrefix) {
StringBuilder append = new StringBuilder("\n").append(e);
for (StackTraceElement stackTraceElement : e.getStackTrace()) {
if (stackTraceElement.getClassName().startsWith(packagePrefix)) {
append.append("\n\tat ").append(stackTraceElement);
}
}
return append.toString();
}
/**
* 捕获空指针异常
**/
@ExceptionHandler(value = NullPointerException.class)
public R handlerBindException(NullPointerException exception) {
String message = exception.getMessage();
log.error("全局捕获null错误信息: {}", exception.toString(), exception);
return R.fail(message);
}
/**
* 捕获最大异常
**/
@ExceptionHandler(value = Exception.class)
public R handlerBindException(Exception exception) {
String message = exception.getMessage();
log.error("全局捕获错误信息: {}", exception.toString(), exception);
return R.fail(message);
}
}
测试统一异常
package cn.mesmile.admin.modules.system.controller;
import cn.mesmile.admin.common.exceptions.BusinessException;
import cn.mesmile.admin.common.result.R;
import cn.mesmile.admin.common.result.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author zb
* @Description
*/
@Slf4j
@RequestMapping("/api/v1/hello")
@RestController
public class HelloController {
@GetMapping("/get")
public R hello () {
if (true){
throw new BusinessException(ResultCode.FAILURE, "hello业务发送异常了");
}
return R.data("hello world");
}
}
启动 cn.mesmile.admin.AdminApplication 启动类,访问 localhost:8080/api/v1/hello/get
看到统一返回结果如下:
{
"code": 400,
"success": false,
"data": null,
"msg": "hello业务发送异常了"
}
日志信息: 业务异常在全局处理的时候只记录了 指定头开始的日志,减少了堆栈信息
2022-07-09 16:15:42.050 INFO 13512 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-07-09 16:15:42.051 INFO 13512 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2022-07-09 16:15:42.051 INFO 13512 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 0 ms
2022-07-09 16:15:42.074 ERROR 13512 --- [nio-8080-exec-1] c.m.a.c.handler.GlobalExceptionHandler : 记录业务异常信息: 消息hello业务发送异常了 编码400
cn.mesmile.admin.common.exceptions.BusinessException: hello业务发送异常了
at cn.mesmile.admin.modules.system.controller.HelloController.hello(HelloController.java:23)