restful返回风格

restful返回风格

创建一个Resultconfig

public enum Resultconfig {
    SUCCESS("200", "success", "成功"),
    FAIL("201", "fail", "失败"),
    TOKEN_FAIL("403", "fail", "Token无效,请重新登陆"),
    U_P_FAIL("200001", "fail", "用户名或者密码不正确"),
    SERVER_FAIL("301", "fail", "服务器报错"),
    SUCCESS_YES("200", "success", "成功"),
    SUCCESS_NO("201", "fail", "失败");


    private String code;
    private String status;
    private String msg;
    private String data;

    Resultconfig(String code, String status, String msg) {
        this.code = code;
        this.status = status;
        this.msg = msg;
    }

    Resultconfig(String code, String status, String msg, String data) {
        this.code = code;
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    public String getCode() {
        return code;
    }

    public String getStatus() {
        return status;
    }

    public String getMsg() {
        return msg;
    }

    public String getData() {
        return data;
    }

    public Resultconfig setMsg(String msg) {
        this.msg = msg;
        return this;
    }
}

再创建一个Returnrestful

import java.io.Serializable;

public class Returnrestful<T> implements Serializable {
    public String code;
    public String state;
    public String msg;
    public String status;
    public T data;

    public String getStatus() {
        return status;
    }

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

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }



    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Returnrestful(String code, String state, T data) {
        this.code = code;
        this.state = state;
        this.data = data;

    }

    public Returnrestful() {
        this.code = "201";
        this.state = "fail";
        this.data = null;
    }

    public Returnrestful(String code, String state, String msg, T data) {
        this.code = code;
        this.state = state;
        this.msg = msg;
        this.data = data;
    }
}

controller使用不表示(此处是成功与失败案例)

 @PostMapping("/deleteorder")
    public Returnrestful deleteorder(Bean bean) {
        int num = loginService.deleteorder(bean);
        System.out.println(num);
        if (num >=1) {
            return new Returnrestful(Resultconfig.SUCCESS.getCode(), Resultconfig.SUCCESS.getStatus(), Resultconfig.SUCCESS.getMsg(),"");
        } else {

            return new Returnrestful(Resultconfig.FAIL.getCode(), Resultconfig.FAIL.getStatus(), Resultconfig.FAIL.getMsg(),"");
        }

    }

返回数据格式

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值