当前位置: 当前位置:首页 >IT科技 >你会全局统一格式返回吗? 正文

你会全局统一格式返回吗?

2025-11-04 13:51:48 来源:多维IT资讯作者:IT科技类资讯 点击:183次

本文转载自微信公众号「java后端指南」,局统作者KING鹏哥  。格式转载本文请联系java后端指南公众号。局统

相信全局统一格式返回这个东西在每个项目中都非常重要的格式,前端需要一个统一的局统格式给前端,所以我们后端需要封装好结构给前端。格式

1结构

我目前接触的局统结构基本上是这样的企商汇

{ "code":"0",格式 "msg":"请求正常", "data":{} } 

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);     } } 

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(); } }  /**  * 自定义异常  */ 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;     } } 

我就创建了两个异常类。

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()));     } } 

其中状态码我们可以单独定义一个类,我为了方便就不写了。

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("失败了");     } } 

大家可以自己去看看,如果有问题欢迎来骚扰我。

视频讲解:https://www.bilibili.com/video/BV1e341117av/

云服务器提供商
作者:IT科技
------分隔线----------------------------
头条新闻
图片新闻
新闻排行榜