当前位置:   article > 正文

RESTful API返回封装类_springboot结果封装状态码

springboot结果封装状态码

RESTful API返回封装类

好久没有写博客了,也不知道要和大家分享些什么,最近有点不在状态,随便写写吧。对于RESTful ,前后端的同学,应该都不默认陌生吧,今天就给搭建分享一个我最常用的,最基础的一个封装类吧

作者后端主语音是Java,给大家封装提个Java基础的返回类。
Result.java


import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@ApiModel(value = "统一数据返回对象", description = "所有数据经此包装")
public class Result<T> implements Serializable {

	private static final long serialVersionUID = 1L;

	/**
	 * 状态码
	 */
	@ApiModelProperty(required = true, value = "返回状态码", dataType = "int", example = "0", position = 0)
	private int code;

	/**
	 * msg信息
	 */
	@ApiModelProperty(required = true, value = "返回message 信息", dataType = "string", example = "操作成功", position = 1)
	private String msg;

	/**
	 * 返回数据
	 */
	@ApiModelProperty(required = true, value = "返回数据", dataType = "string", example = "", position = 3)
	private T data;

	/**
	 * 返回时间
	 */
	@ApiModelProperty(required = true, value = "返回时间", dataType = "string", example = "yyyy-MM-dd", position = 2)
	private String date;



	public Result() {
		this.code = 200;
		this.msg = "操作成功";
		this.date = dateString();
	}

	public static Result error() {
		return error(1, "操作失败");
	}

	public static Result error(String msg) {
		return error(500, msg);
	}

	public static Result error(int code, String msg) {
		Result r = new Result();
		r.setCode(code);
		r.setMsg(msg);
		r.setDate(dateStaticString());
		return r;
	}

	public static Result ok(String msg) {
		Result r = new Result();
		r.setMsg(msg);
		return r;
	}

	public static Result ok(int code,Object obj) {
		Result r = new Result();
		r.setCode(code);
		r.setData(obj);
		return r;
	}

	public static Result ok(String msg,Object obj) {
		Result r = new Result();
		r.setMsg(msg);
		r.setData(obj);
		return r;
	}

	public static Result ok(Object obj) {
		Result r = new Result();
		r.setData(obj);
		return r;
	}

	public static Result ok() {
		return new Result();
	}



	public int getCode() {
		return code;
	}

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

	public T getData() {
		return data;
	}

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

	public String getMsg() {
		return msg;
	}

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

	public String getDate() {
		return dateStaticString();
	}

	public void setDate(String date) {
		this.date = date;
	}

	private String dateString() {
		return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
	}
	public static String dateStaticString() {
		return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
	}
}
  • 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

现在Java后台,主流的SpringBoot框架,直接在controller类注解@RestController ,接口直接返回Result 类就好了。

@RestController
public class RestFulController {

    /**
     * 返回Json:
     {
         "code": 200,
         "data": null,
         "date": "2022-08-16",
         "msg": "操作成功"
     }
     */
    @RequestMapping("/index")
    public Result index(){
        return Result.ok();
    }


    /**
     * 返回Json:
     {
         "code": 1,
         "data": null,
         "date": "2022-08-16",
         "msg": "操作失败"
     }
     */
    @RequestMapping("/index1")
    public Result index1(){
        return Result.error();
    }


    /**
     * 返回Json:
     {
         "code": 500,
         "data": null,
         "date": "2022-08-16",
         "msg": "自定义错误提示"
     }
     */
    @RequestMapping("/index2")
    public Result index2(){
        return Result.error("自定义错误提示");
    }

    /**
     * 返回Json:
     {
         "code": 1,
         "data": null,
         "date": "2022-08-16",
         "msg": "自定义返回错误码与错误提示"
     }
     */
    @RequestMapping("/index3")
    public Result index3(){
        return Result.error(1,"自定义返回错误码与错误提示");
    }

}
  • 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

以上是基本的返回封装,作者在开发中也比较喜欢并且常用的基础返回封装,对于列表数据的返回,还有一个PageUtils封装类,其使用方法差不多,代码如下

@ApiModel(value = "分页数据", description = "分页数据统一返回对象")
public class  PageUtils implements Serializable {

	private static final long serialVersionUID = 1L;

	@ApiModelProperty(value = "一共查询了多少条数据", dataType = "long", name = "total", notes = "不需要传输 仅返回时展示使用")
	private long total;

	@ApiModelProperty(value = "列表数据", dataType = "String", name = "values", example = "")
	private List<?> rows;

	public PageUtils(List<?> list, long total) {
		this.rows = list;
		this.total = total;
	}

	public PageUtils(List<?> list, long total) {
		this.rows = list;
		this.total = total;
	}

	public long getTotal() {
		return total;
	}

	public void setTotal(long total) {
		this.total = total;
	}

	public List<?> getRows() {
		return rows;
	}

	public void setRows(List<?> rows) {
		this.rows = rows;
	}
}
  • 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

感谢大家的支持,您的支持就是我的动力

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号