一、背景
作为一名java程序猿,我们通常在工作中要开发接口给前端调用,因此一个统一规范的返回结果是必然的,下面我们就统一返回结果和统一异常处理进行个封装。
二、实现
2.1 枚举状态码
首先返回结果我们需要一个统一的状态码及信息提示,这里我们使用枚举类型,当然也可以使用静态变量形式,个人比较推荐枚举,主要就是看着简洁。
package com.example.demo.response;
public enum ResultEnum {
// 枚举定义
SUCCESS(200, "success"),
NO_PERMISSION(403,"无查看权限"),
INTERNAL_SERVER_ERROR(500, "服务器异常请联系管理员"),
;
/** 错误码 */
private Integer code;
/** 错误信息 */
private String message;
ResultEnum(Integer code, String message) {
this.code = code;
this.message = message;
}
public Integer getCode() {
return code;
}
public String getMessage() {
return message;
}
}
2.2 自定义异常类
package com.example.demo.response;
import lombok.Data;
@Data
public class BusinessException extends RuntimeException {
/**
* 错误状态码
*/
protected Integer errorCode;
/**
* 错误提示
*/
protected String errorMsg;
public BusinessException(){
}
public BusinessException(Integer errorCode, String errorMsg) {
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}
}
2.3 统一返回数据类型
我们将返回结果信息规范封装一下。
package com.example.demo.response;
import lombok.Data;
@Data
public class ResultResponse {
//是否成功
private Boolean success;
//状态码
private Integer code;
//提示信息
private String msg;
//数据
private Object data;
public ResultResponse() {}
// 自定义构造方法
public ResultResponse(Boolean success,Integer code, String msg,Object data) {
this.success = success;
this.code = code;
this.msg = msg;
this.data = data;
}
// 自定义异常
public static ResultResponse customException(BusinessException be){
ResultResponse result = new ResultResponse();
result.setSuccess(false);
result.setCode(be.getErrorCode());
result.setMsg(be.getErrorMsg());
result.setData(null);
return result;
}
// 其他异常
public static ResultResponse otherException(ResultEnum resultEnum){
ResultResponse result = new ResultResponse();
result.setSuccess(false);
result.setCode(resultEnum.getCode());
result.setMsg(resultEnum.getMessage());
result.setData(null);
return result;
}
}
2.4 全局异常处理器
全局异常处理器就是在你需要报错的地方,直接throws出去,写入自定义的异常信息。
package com.example.demo.response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
* 全局异常处理器
*/
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
/**
* 处理自定义异常
*
*/
@ExceptionHandler(value = BusinessException.class)
public ResultResponse customExceptionHandler(BusinessException e) {
log.error(e.getMessage(), e);
return ResultResponse.customException(e);
}
/**
*处理其他异常
*
*/
@ExceptionHandler(value = Exception.class)
public ResultResponse otherExceptionHandler(Exception e) {
log.error(e.getMessage(), e);
return ResultResponse.otherException(ResultEnum.INTERNAL_SERVER_ERROR);
}
}
2.5 测试controller
我们编写一个controller类型,写一个方法测试一下自定义异常和返回封装结果体。
package com.example.demo.response;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@RequestMapping("exception")
@Controller
public class ExceptionController {
@RequestMapping("query")
@ResponseBody
public Object exceptionTest() {
throw new BusinessException(ResultEnum.NO_PERMISSION.getCode(), ResultEnum.NO_PERMISSION.getMessage());
}
}
2.6 测试结果
启动项目后,在浏览器里输入请求链接,可得下结果:
三、总结
这是工作学习中总结出来的,感觉应该大多数场景下都够用了,欢迎建议指教啊。