在咱们开发SpringBoot后端服务时,一般需求给前端一致呼应格局,方便前端调试及配置过错提示等等。这篇文章讲讲实际工作中一致呼应格局及一致反常处理是如何做的。
一、一致呼应基础类
在项目中对应东西类或Vo层来创立咱们的一致呼应类
ResponseResult
:
import com.fasterxml.jackson.annotation.JsonInclude;
import com.zhang.enums.AppHttpCodeEnum;
import java.io.Serializable;
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseResult<T> implements Serializable {
private Integer code;
private String msg;
private T data;
public ResponseResult() {
this.code = AppHttpCodeEnum.SUCCESS.getCode();
this.msg = AppHttpCodeEnum.SUCCESS.getMsg();
}
public ResponseResult(Integer code, T data) {
this.code = code;
this.data = data;
}
public ResponseResult(Integer code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public ResponseResult(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
public static ResponseResult errorResult(int code, String msg) {
ResponseResult result = new ResponseResult();
return result.error(code, msg);
}
public static ResponseResult okResult() {
ResponseResult result = new ResponseResult();
return result;
}
public static ResponseResult okResult(int code, String msg) {
ResponseResult result = new ResponseResult();
return result.ok(code, null, msg);
}
public static ResponseResult okResult(Object data) {
ResponseResult result = setAppHttpCodeEnum(AppHttpCodeEnum.SUCCESS, AppHttpCodeEnum.SUCCESS.getMsg());
if (data != null) {
result.setData(data);
}
return result;
}
public static ResponseResult errorResult(AppHttpCodeEnum enums) {
return setAppHttpCodeEnum(enums, enums.getMsg());
}
public static ResponseResult errorResult(AppHttpCodeEnum enums, String msg) {
return setAppHttpCodeEnum(enums, msg);
}
public static ResponseResult setAppHttpCodeEnum(AppHttpCodeEnum enums) {
return okResult(enums.getCode(), enums.getMsg());
}
private static ResponseResult setAppHttpCodeEnum(AppHttpCodeEnum enums, String msg) {
return okResult(enums.getCode(), msg);
}
public ResponseResult<?> error(Integer code, String msg) {
this.code = code;
this.msg = msg;
return this;
}
public ResponseResult<?> ok(Integer code, T data) {
this.code = code;
this.data = data;
return this;
}
public ResponseResult<?> ok(Integer code, T data, String msg) {
this.code = code;
this.data = data;
this.msg = msg;
return this;
}
public ResponseResult<?> ok(T data) {
this.data = data;
return this;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
里面还有用到一个呼应的枚举类AppHttpCodeEnum
,接下来咱们创立这个枚举类
二、呼应枚举类
AppHttpCodeEnum
:
public enum AppHttpCodeEnum {
// 成功
SUCCESS(200, "操作成功"),
// 登录
NEED_LOGIN(401, "需求登录后操作"),
NO_OPERATOR_AUTH(403, "无权限操作"),
SYSTEM_ERROR(500, "呈现过错"),
USERNAME_EXIST(501, "用户名已存在"),
PHONENUMBER_EXIST(502, "手机号已存在"), EMAIL_EXIST(503, "邮箱已存在"),
REQUIRE_USERNAME(504, "必需填写用户名"),
CONTENT_NOT_NULL(506, "评论内容不能为空"),
FILE_TYPE_ERROR(507, "文件类型过错"),
USERNAME_NOT_NULL(508, "用户名不能为空"),
NICKNAME_NOT_NULL(509, "昵称不能为空"),
PASSWORD_NOT_NULL(510, "密码不能为空"),
EMAIL_NOT_NULL(511, "邮箱不能为空"),
NICKNAME_EXIST(512, "昵称已存在"),
LOGIN_ERROR(505, "用户名或密码过错");
int code;
String msg;
AppHttpCodeEnum(int code, String errorMessage) {
this.code = code;
this.msg = errorMessage;
}
public int getCode() {
return code;
}
public String getMsg() {
return msg;
}
}
一般咱们在这个枚举类中办理需求呼应的过错code
及msg
三、一致呼应格局运用
在对应的controller或许service里面运用一致呼应类
- 成功:
ResponseResult.okResult()
- 失败:
ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR)
, 参数传入咱们定义的呼应枚举类
四、一致反常处理
1. 自定义反常
为什么咱们需求自定义反常?由于在某些情况下,咱们需求回来咱们自定义的呼应格局非常不方便,如在处理用户鉴权或token校验的时分,由于像这些部分咱们一般都是在独自的东西类中去处理,这时分如果报错其实就可以抛出咱们自定义的反常,交由咱们大局的反常处理去一致回来呼应。
- 在
exception
包下新建SystemException
类 -
SystemException
继承RuntimeException
详细完成代码如下
exception.SystemException:
import com.jk.enums.AppHttpCodeEnum;
public class SystemException extends RuntimeException{
private int code;
private String msg;
public int getCode() {
return code;
}
public String getMsg() {
return msg;
}
public SystemException(AppHttpCodeEnum appHttpCodeEnum) {
super(appHttpCodeEnum.getMsg());
this.code = appHttpCodeEnum.getCode();
this.msg = appHttpCodeEnum.getMsg();
}
}
目前只是自定义了反常,咱们还需求对自定义的反常进行处理,回来一致的呼应格局。
2.反常处理
- 在
handler.exception
包下新建GlobalExceptionHandler
处理类 -
@RestControllerAdvice
是组合注解,由@ControllerAdvice
、@ResponseBody
组成,标明是一个一致反常处理的类,并把回来结果封装在ResponseBody
中 -
@Slf4j
是Lombok
完成日志输出的注解 -
@ExceptionHandler
标明该方法处理哪些反常
详细代码完成如下:
handler.exception.GlobalExceptionHandler:
import com.jk.enums.AppHttpCodeEnum;
import com.jk.exception.SystemException;
import com.jk.vo.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
@ExceptionHandler(SystemException.class)
public ResponseResult systemExceptionHandler(SystemException e) {
log.error("呈现了反常! {}", e);
return ResponseResult.errorResult(e.getCode(), e.getMsg());
}
@ExceptionHandler(Exception.class)
public ResponseResult exceptionHandler(Exception e) {
log.error("呈现了反常! {}", e);
return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR.getCode(), e.getMessage());
}
}
可以看到咱们除了处理自定义反常SystemException
外,还对Exception
便是其他咱们无法预料到的反常做了一个兜底。
3.自定义反常运用
在需求抛出反常的当地:
throw new SystemException(AppHttpCodeEnum.LOGIN_ERROR);
前端接收到的呼应是:
{
"code": 505,
"msg": "用户名或密码过错"
}
这样就比接收到500过错也不知道过错原因好多了。
SpringBoot中一致呼应格局及一致反常处理便是如此简单,如果我们有什么疑问,可以评论区留言哈,拜托我们
点赞保藏
一下, ,后续还会继续出关于SpringBoot相关的系列文章哦,记得关注!