接口统一返回工具类

一、接口统一返回类

import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.Data;
import java.io.Serializable;

@Data
public class ResponseMessage<T> implements Serializable {

    // 返回状态【0-成功,1-业务失败,999-表示系统异常】
    @JSONField(ordinal = 1)
    private int code;
    // 返回信息
    @JSONField(ordinal = 2)
    private String message;
    // 返回数据实体;
    @JSONField(ordinal = 3,serialzeFeatures = {SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullStringAsEmpty})
    private T data;


    private ResponseMessage(){}


    public static<T> ResponseMessage success(T m){
        ResponseMessage r = new ResponseMessage();
        r.setCode(0);
        r.setMessage("success");
        r.setData(m);
        return r;
    }



    public static<T> ResponseMessage success(){
        ResponseMessage r = new ResponseMessage();
        r.setCode(0);
        r.setMessage("success");
        return r;
    }

    public static<T> ResponseMessage success(String msg){
        ResponseMessage r = new ResponseMessage();
        r.setCode(0);
        r.setMessage("success");
        r.setData(msg);
        return r;
    }

    public static<T> ResponseMessage serviceFail(String msg){
        ResponseMessage r = new ResponseMessage();
        r.setCode(1);
        r.setMessage("error");
        r.setData(msg);
        return r;
    }

    public static<T> ResponseMessage appFail(String msg){
        ResponseMessage r = new ResponseMessage();
        r.setCode(999);
        r.setMessage(msg);

        return r;
    }

    public static<T> ResponseMessage error(int code, String msg){
        ResponseMessage r = new ResponseMessage();
        r.setCode(code);
        r.setMessage(msg);

        return r;
    }

    public static<T> ResponseMessage serviceFail(){
        ResponseMessage r = new ResponseMessage();
        r.setCode(1);
        r.setMessage("error");
        r.setData("系统异常");
        return r;
    }
    
}

二、使用

  @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = FundConfig.class)
    })
    @ApiOperation(value = "增加基金配置", notes = "", httpMethod = "POST")
    @RequestMapping(value = "/addFundConfig", method = RequestMethod.POST)
    public ResponseMessage addFundConfig(@RequestBody FundConfig fundConfig) throws Exception {
        try {
            //先查询是否存在
            FundConfig fundcode = tFundConfigService.getOne(new QueryWrapper<FundConfig>().eq("fundCode", fundConfig.getFundCode()));
            if (null != fundcode) {
                return ResponseMessage.serviceFail("基金" + fundcode.getFundCode() + "已存在!");
            }
            //基金的排序字段sort不能重复,测试提出的。其实排序字段并列是正常 的
            FundConfig sort = tFundConfigService.getOne(new QueryWrapper<FundConfig>().eq("sort", fundConfig.getSort()));
            if (null != sort) {
                return ResponseMessage.serviceFail("排序" + fundConfig.getSort() + "已存在!");
            }
            tFundConfigService.save(fundConfig);
            redisService.delByprefix("getDirectList");
            return ResponseMessage.success("增加成功!");
        } catch (Exception e) {
            log.error("异常", e);
            return ResponseMessage.serviceFail("接口异常!");
        }
    }

 

posted @ 2023-05-17 22:18  别动我的猫  阅读(64)  评论(0编辑  收藏  举报