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("查詢用戶信息異常");
        }
    }
}

查詢成功返回結果集信息

 

 

服務器異常返回結果集信息


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM