zoukankan      html  css  js  c++  java
  • Java设计RestfulApi接口,实现统一格式返回

    创建返回状态码枚举

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

    返回结果集封装

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

    响应客户端结果集封装

    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("查询用户信息异常");
            }
        }
    }

    查询成功返回结果集信息

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

  • 相关阅读:
    CSUSTOJ-伊井野弥子是风纪委员(简单BFS)
    CSUSTOJ-石上优不想留级(一维坐标三分及思维解法)
    CSUSTOJ-哈希的纸团(mid思维)
    CSUSTOJ-辉夜大小姐想被猜中(简单暴力)
    CSUSTOJ-藤原书记想要探病(简单矩阵快速幂)
    CSUSTOJ-石上优想要逃离(STL+思维暴力)
    CSUSTOJ-白银御行想展示(思维题)
    CSUSTOJ-藤原书记的佩斯(简单数学)
    CSUSTOJ-白银探病篇(简单思维)
    Odoo发邮件被服务器退回
  • 原文地址:https://www.cnblogs.com/sunny1009/p/11772195.html
Copyright © 2011-2022 走看看