Java设计RestfulApi接口,实现统一格式返回

 

 

创建返回状态码枚举

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
package com.sunny.tool.api.enums;
 
/**
 * @Author sunt
 * @Description 响应枚举状态码
 * @Date 2019/10/31
 **/
public enum ResultCode {
 
    // 成功
    SUCCESS(200),
 
    // 失败
    FAIL(400),
 
    // 未认证(签名错误)
    UNAUTHORIZED(401),
 
    // 接口不存在
    NOT_FOUND(404),
 
    // 服务器内部错误
    INTERNAL_SERVER_ERROR(500);
 
    public int code;
 
    ResultCode(int code) {
        this.code = code;
    }
}

返回结果集封装

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
package com.sunny.tool.api.entity;
 
import com.sunny.tool.api.enums.ResultCode;
 
/**
 * @ClassName: ResponseResult
 * @Description: 返回结果集封装
 * @Author: sunt
 * @Date: 2019/10/31 16:11
 * @Version 1.0
 **/
public class ResponseResult<T> {
 
    public int code; //返回状态码200成功
 
    private String msg; //返回描述信息
 
    private T data; //返回内容体
 
    public ResponseResult<T> setCode(ResultCode retCode) {
        this.code = retCode.code;
        return this;
    }
 
    public int getCode() {
        return code;
    }
 
    public ResponseResult<T> setCode(int code) {
        this.code = code;
        return this;
    }
 
    public String getMsg() {
        return msg;
    }
 
    public ResponseResult<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }
 
    public T getData() {
        return data;
    }
 
    public ResponseResult<T> setData(T data) {
        this.data = data;
        return this;
    }
 
}

响应客户端结果集封装

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
package com.sunny.tool.api.entity;
 
import com.sunny.tool.api.enums.ResultCode;
 
/**
 * @ClassName: Response
 * @Description:将结果转换为封装后的对象
 * @Author: sunt
 * @Date: 2019/10/31 16:11
 * @Version 1.0
 **/
public class Response {
 
    private final static String SUCCESS = "success";
 
    private final static String FAIL = "fail";
 
    public static <T> ResponseResult<T> makeOKRsp() {
        return new ResponseResult<T>().setCode(ResultCode.SUCCESS).setMsg(SUCCESS);
    }
 
    public static <T> ResponseResult<T> makeOKRsp(String message) {
        return new ResponseResult<T>().setCode(ResultCode.SUCCESS).setMsg(message);
    }
 
    public static <T> ResponseResult<T> makeOKRsp(T data) {
        return new ResponseResult<T>().setCode(ResultCode.SUCCESS).setMsg(SUCCESS).setData(data);
    }
 
    public static <T> ResponseResult<T> makeErrRsp(String message) {
        return new ResponseResult<T>().setCode(ResultCode.INTERNAL_SERVER_ERROR).setMsg(message);
    }
 
    public static <T> ResponseResult<T> makeRsp(int code, String msg) {
        return new ResponseResult<T>().setCode(code).setMsg(msg);
    }
 
    public static <T> ResponseResult<T> makeRsp(int code, String msg, T data) {
        return new ResponseResult<T>().setCode(code).setMsg(msg).setData(data);
    }
}

以查询用户列表为例讲解具体使用

创建查询用户的Controller 

复制代码
package com.sunny.tool.api.controller;

import com.sunny.tool.api.entity.Response;
import com.sunny.tool.api.entity.ResponseResult;
import com.sunny.tool.api.entity.UserBean;
import com.sunny.tool.api.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName: TestController
 * @Description:
 * @Author: sunt
 * @Date: 2019/10/31 16:12
 * @Version 1.0
 **/
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private UserService userService;

    @RequestMapping("/queryUserList")
    public ResponseResult<List<UserBean>> queryUserList() {
        try {
            //调用业务层返回用户列表
            List<UserBean> userList = userService.queryUserList();
            return Response.makeOKRsp(userList);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.makeErrRsp("查询用户信息异常");
        }
    }
}
复制代码

查询成功返回结果集信息

 

 

服务器异常返回结果集信息

posted @   满Sir  阅读(1039)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示