赞
踩
好久没有写博客了,也不知道要和大家分享些什么,最近有点不在状态,随便写写吧。对于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()); } }
现在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,"自定义返回错误码与错误提示"); } }
以上是基本的返回封装,作者在开发中也比较喜欢并且常用的基础返回封装,对于列表数据的返回,还有一个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; } }
感谢大家的支持,您的支持就是我的动力
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。