Restful API返回统一响应体 :restful api接口,返回统一响应体

先建一个工具类如

1
RestResponse.java
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
package com.ibaiqi.news.sqgov.tool;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
 
import java.io.Serializable;
import java.util.Date;
 
/**
 *
 * @author wangyushuai@fang.com
 * @date 2018/9/26
 * REST请求响应工具类 ,在controller控制器中,做为接口返回值  return RestResponse.buildSuccess(articles);
 */
public class RestResponse implements Serializable {
 
    private final static long serialVersionUID = 1L;
    /**
     * 成功
     */
    private final static int STATUS_SUCCESS = 200;
 
    /**
     * 代码错误
     */
    private final static int STATUS_ERROR_INTERNAL_SERVER_ERROR = 500;
 
    /**
     * 服务不可用(针对熔断&服务降级的情况)
     */
    private final static int STATUS_ERROR_SERVICE_UNAVAILIABLE = 503;
 
 
    private int status;
    //@JsonInclude(JsonInclude.Include.NON_NULL)//不为空时,返回
    private Object data;
    private String message;
 
    /**
     * 时间戳并格式化
     */
    @JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss",locale = "lz",timezone = "GMT+8")
    private Date timestamp;
 
    /**
     * 程序耗时
     */
    //@JsonIgnore//不返回注解
    private long time;
 
    public RestResponse(int code, String message, Object data) {
        super();
        this.status = code;
        this.message = message;
        this.data = data;
    }
 
    /**
     * 请求成功
     * @param data
     * @return
     */
    public static RestResponse buildSuccess(Object data) {
        return new RestResponse(STATUS_SUCCESS, "success", data);
    }
 
 
    /**
     * 代码错误
     * @param data
     * @return
     */
    public static RestResponse buildError_InternalServerError(Object data) {
        return new RestResponse(STATUS_ERROR_INTERNAL_SERVER_ERROR, "error", data);
    }
 
    /**
     * 返回错误码(服务不可用时,返回此方法),但推荐直接使用异常类
     * @param data
     * @return
     */
    public static RestResponse buildError_ServiceUnavailable(Object data) {
        return new RestResponse(STATUS_ERROR_SERVICE_UNAVAILIABLE, "error", data);
    }
 
//    public static RestResponse buildError(Object data) {
//        return new RestResponse(STATUS_SUCCESS, "success", data);
//    }
 
    public int getStatus() {
        return status;
    }
 
    public void setStatus(int status) {
        this.status = status;
    }
 
    public Object getData() {
        return data;
    }
 
    public void setData(Object data) {
        this.data = data;
    }
 
    public String getMessage() {
        return message;
    }
 
    public void setMessage(String message) {
        this.message = message;
    }
 
    public Date getTimestamp() {
        return new Date();
    }
 
    public void setTimestamp(Date timestamp) {
        this.timestamp = timestamp;
    }
 
    public String getTime() {
        return time + "ms";
    }
 
    public void setTime(long time) {
        this.time = time;
    }
 
}

 2:控制器类controller 类中方法引用

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
@RestController
@RequestMapping("/api")
@Api("政务要闻文章Controller接口")
@CrossOrigin(origins = "*", maxAge = 3600)
public class ArticlesController {
    @Resource
    ArticlesService articlesService;
 
     
    /**
     * 列出Articles且带上分页信息
     *
     * @return
     */
    @GetMapping("/V1/articlesResResult/{pageSize}")
    @ResponseBody
    @ApiOperation(value = "列出文章,且带分页信息", notes = "返回Articles对象的集合")
    public RestResponse articlesResResult(
            @ApiParam(name = "pageSize", value = "输入每页显示页数", defaultValue = "3", required = true) @PathVariable Integer pageSize
    ) {
//        Map<String, Object> map = new HashMap<>();
        PageInfo<Articles> articles = articlesService.listArticles(1, pageSize);
//        map.put("data",articles);
//        map.put("msg","查询所有用户及分页信息成功");
        //        return  RestResponse.buildSuccess(map);
        return RestResponse.buildSuccess(articles);
 
    }
}

 

posted @   码哥之旅  阅读(2542)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示