封装一个返回类

       背景:后端写接口给前端返回数据时,往往需要统一的返回一些字段,比如说:错误码,错误提示什么的,这里为了简化后端的工作,特意封装一个返回类,给前端返回数据.

package com.pactera.common;


import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel(value = "返回信息", description = "返回响应信息")
public class ReturnValue extends BaseResponse {
    public static final String STATUS_FAIL = "500";
    public static final String STATUS_OK = "200";
    public static final String MESSAGE_SUCCESS = "操作成功";
    public static final String MESSAGE_ERROR = "操作失敗";
    public static final String ERRCODE_NOERR = "0000";
    public static final String ERRCODE_ERR = "9999";
    //未配置简易流程
    public static final String ERR_SIMPLE_PROCESS = "0001";
    //子目录与主目录的基本编码不匹配
    public static final String ERR_BASIC_CODE = "0002";
    @ApiModelProperty(value = "操作成功或失败标志", name = "status", example = "200")
    protected String status = STATUS_OK;
    @ApiModelProperty(value = "操作错误代码", name = "errcode", example = "5399")
    protected String errcode = "";
    @ApiModelProperty(value = "操作状态信息", name = "message", example = "操作成功")
    protected String message = MESSAGE_SUCCESS;
    @ApiModelProperty(value = "错误信息", name = "errmsg", example = "上送数据错误")
    protected String errmsg = "";

    public ReturnValue(String status, String message) {
        this.status = status;
        this.message = message;
    }

    public ReturnValue() {
    }

    public ReturnValue(String status, String message, JSONObject data, String errcode, String errmsg) {
        super.setData(data);
        this.status = status;
        this.message = message;
    }

    public String getErrmsg() {
        return errmsg;
    }

    public ReturnValue setErrmsg(String errmsg) {
        this.errmsg = errmsg;
        return this;
    }

    public String getMessage() {
        return message;
    }

    public ReturnValue setMessage(String message) {
        this.message = message;
        return this;
    }

    public String getStatus() {
        return status;
    }

    public ReturnValue setStatus(String status) {
        this.status = status;
        return this;
    }

    /**
     * description: 返回成功报文无需返回业务数据
     */
    public static ReturnValue ok() {
        ReturnValue returnValue = new ReturnValue();
        returnValue.setData(new JSONObject());
        returnValue.errcode = ERRCODE_NOERR;
        return returnValue;
    }

    /**
     * description: 返回成功报文包含返回数据
     */
    public static ReturnValue ok(Object data) {
        ReturnValue response = new ReturnValue();
        response.setStatus(STATUS_OK);
        response.setMessage(MESSAGE_SUCCESS);
        response.setData(data);
        response.setErrmsg("");
        response.setErrcode(ERRCODE_NOERR);
        return response;
    }


    /**
     * description: 返回成功报文包含返回数据,自定义返回错误信息和错误码
     */
    public static ReturnValue ok(Object data, String errMsg, String errCode) {
        ReturnValue response = new ReturnValue();
        response.setStatus(STATUS_OK);
        response.setMessage(MESSAGE_SUCCESS);
        response.setData(data);
        response.setErrmsg(errMsg);
        response.setErrcode(errCode);
        return response;
    }

    /**
     * description: 返回失败报文,自定义错误信息
     */
    public static ReturnValue fail(String errmsg) {
        return fail(errmsg, ERRCODE_ERR);
    }

    /**
     * description:返回失败报文,自定义错误代码和错误信息
     */
    public static ReturnValue fail(String errmsg, String errcode) {
        ReturnValue response = new ReturnValue();
        response.setStatus(STATUS_FAIL);
        response.setMessage(MESSAGE_ERROR);
        response.setData(new JSONObject());
        response.setErrmsg(errmsg);
        response.setErrcode(errcode);
        return response;
    }

    public String getErrcode() {
        return errcode;
    }

    public ReturnValue setErrcode(String errcode) {
        this.errcode = errcode;
        return this;
    }
}

这里继承的BaseResponse 代码如下:

package com.pactera.common;

import io.swagger.annotations.ApiModelProperty;

@Data
public class BaseResponse {

    @ApiModelProperty(value = "返回数据", name = "data", example = "")
    private Object data;

}

如何使用: 查出来的json数据直接丢进去即可,用法很方便简单

    @GetMapping("/getItemNotes")
    @ApiOperation(value = "获取事项各要素的数量")
    public ReturnValue getItemNotes(@RequestParam String itemCode) {
        JSONObject json;
        try {
            json = this.projectConfigService.getItemNotes(itemCode);
        } catch (Exception e) {
            log.error("获取事项各要素的数量=====异常", e);
            return ReturnValue.fail("获取事项各要素的数量=====异常");
        }
        log.info("获取事项各要素的数量=====成功");
        return ReturnValue.ok(json);
    }

前端返回效果: 除了返回的数据外,自动会加上状态和错误码什么的,大大减少后端开发的工作量

{
  "data": {
    "complain": 0,
    "consultWay": 0,
    "condition": 0,
    "material": 0,
    "productSamples": 0,
    "window": 0,
    "front": 0,
    "onlineconduct": 0
  },
  "status": "200",
  "errcode": "0000",
  "message": "操作成功",
  "errmsg": ""
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

希文先森

您的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值