本文转载自微信公众号「java后端指南」,作者KING鹏哥 。转载本文请联系java后端指南公众号。
相信全局统一格式返回这个东西在每个项目中都非常重要的,前端需要一个统一的格式给前端,所以我们后端需要封装好结构给前端。
1结构
我目前接触的结构基本上是这样的。
{
"code":"0",
"msg":"请求正常",
"data":{}
}
- 1.
- 2.
- 3.
- 4.
- 5.
2实现
创建一个统一返回前端的实体类
public class R extends HashMap<String, Object> {
private static final long serialVersionUID = 1L;
/**
* 状态码
*/
public static final String CODE_TAG = "code";
/**
* 返回内容
*/
public static final String MSG_TAG = "msg";
/**
* 数据对象
*/
public static final String DATA_TAG = "data";
/**
* 初始化一个新创建的 AjaxResult 对象,使其表示一个空消息。
*/
public R() {
}
/**
* 初始化一个新创建的 AjaxResult 对象
*
* @param code 状态码
* @param msg 返回内容
*/
public R(String code, String msg) {
super.put(CODE_TAG, code);
super.put(MSG_TAG, msg);
}
/**
* 初始化一个新创建的 AjaxResult 对象
*
* @param code 状态码
* @param msg 返回内容
* @param data 数据对象
*/
public R(String code, String msg, Object data) {
super.put(CODE_TAG, code);
super.put(MSG_TAG, msg);
if (null!=data) {
super.put(DATA_TAG, data);
}
}
/**
* 方便链式调用
*
* @param key
* @param value
* @return
*/
@Override
public R put(String key, Object value) {
super.put(key, value);
return this;
}
/**
* 返回成功消息
*
* @return 成功消息
*/
public static R success() {
return R.success("操作成功");
}
/**
* 返回成功数据
*
* @return 成功消息
*/
public static R success(Object data) {
return R.success("操作成功", data);
}
/**
* 返回成功消息
*
* @param msg 返回内容
* @return 成功消息
*/
public static R success(String msg) {
return R.success(msg, null);
}
/**
* 返回成功消息
*
* @param msg 返回内容
* @param data 数据对象
* @return 成功消息
*/
public static R success(String msg, Object data) {
return new R("0", msg, data);
}
/**
* 返回错误消息
*
* @return
*/
public static R error() {
return R.error("操作失败");
}
/**
* 返回错误消息
*
* @param msg 返回内容
* @return 警告消息
*/
public static R error(String msg) {
return R.error("-1", msg);
}
/**
* 返回错误消息
*
* @param msg 返回内容
* @param data 数据对象
* @return 警告消息
*/
public static R error(String msg, Object data) {
return new R("-1", msg, data);
}
/**
* 返回错误消息
*
* @param code 状态码
* @param msg 返回内容
* @return 警告消息
*/
public static R error(String code, String msg) {
return new R(code, msg, null);
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
3异常处理
正常情况下的返回结构已经弄好了,异常情况下我们也要做处理的。
首先新建几个异常类,根据实际需要创建。
/**
* 基本异常
*/
@Getter
public class BaseException extends RuntimeException {
private static final long serialVersionUID = 1L;
/**
* 所属模块
*/
private final String module;
/**
* 错误码
*/
private final String code;
/**
* 错误码对应的参数
*/
private final Object[] args;
/**
* 错误消息
*/
private final String message;
public BaseException(String module, String code, Object[] args, String message) {
this.module = module;
this.code = code;
this.args = args;
this.message = message;
}
public BaseException(String module, String code, Object[] args) {
this(module, code, args, null);
}
public BaseException(String module, String defaultMessage) {
this(module, null, null, defaultMessage);
}
public BaseException(String code, Object[] args) {
this(null, code, args, null);
}
public BaseException(String module, String code, String message) {
this(null, code, null, message);
}
public BaseException(String message) {
this(null, null, null, message);
}
public String getCode() {
return code;
}
public String getMsg() { return message; }
public Object[] getArgs() {
return args;
}
public String getDefaultMessage() { return getMessage(); }
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
/**
* 自定义异常
*/
public class CustomException extends RuntimeException {
private static final long serialVersionUID = 1L;
private String code;
private final String message;
public CustomException(String message) {
this.message = message;
}
public CustomException(String message, String code) {
this.message = message;
this.code = code;
}
public CustomException(String message, Throwable e) {
super(message, e);
this.message = message;
}
@Override
public String getMessage() {
return message;
}
public String getCode() {
return code;
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
我就创建了两个异常类。
4创建全局异常拦截器
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
/**
* 基础异常
*/
@ExceptionHandler(BaseException.class)
public R baseException(BaseException e) {
return R.error(e.getDefaultMessage());
}
/**
* 业务异常
*/
@ExceptionHandler(CustomException.class)
public R businessException(CustomException e) {
if (StringUtils.isNotBlank(e.getCode())) {
return R.error(e.getMessage());
}
return R.error("-1", e.getMessage());
}
@ExceptionHandler(Exception.class)
public R handleException(Exception e) {
log.error(e.getMessage(), e);
return R.error(String.format("未知错误%s",e.getMessage()));
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
其中状态码我们可以单独定义一个类,我为了方便就不写了。
5测试
@RestController
public class TestAction {
@RequestMapping("/test")
public R test(){
return R.success("请求成功",null);
}
@RequestMapping("/test2")
public R test2(){
Student student=new Student();
student.setName("king");
student.setPassword("123456");
return R.success(student);
}
@RequestMapping("/test3")
public R test3(){
return R.error("请求失败");
}
@RequestMapping("/test4")
public R test4(){
throw new CustomException("失败了");
}
@RequestMapping("/test5")
public R test5() throws Exception {
throw new Exception("失败了");
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
大家可以自己去看看,如果有问题欢迎来骚扰我。
视频讲解:https://www.bilibili.com/video/BV1e341117av/