Restful 通用返回格式類設

Restful 通用返回格式類設計


restful 接口返回遵循統一格式, 設計參考類 Optional 遵循函數式編程規範java

普通格式編程

{
  "data": "waht",
  "msg": null,
  "state": 1,
  "is_redirect": true,
  "redirect_url": "http://baidu.com",
  "token": null
}

帶頁碼的格式json

{
  "data": {
    "list": null,
    "pagebar": {
      "page": 1,
      "total": 2,
      "limit": 10
    }
  },
  "msg": "error",
  "state": 0,
  "is_redirect": true,
  "redirect_url": "http://qq.com",
  "token": null
}

樣例代碼restful

@RequestMapping("/get")
    public Result<String> get(@RequestParam(value = "msg", required = false) String msg) {
        return Result.success(msg).orFail("error")
                .redirect("http://baidu.com").orFailRedirect("http://qq.com");
    }

    @RequestMapping("/page")
    public PageResult<String> page() {
        List<String> list = new ArrayList<>();
        list.add("a");
        list.add("b");
        return PageResult.success(list).pageBar(1, 10).total(2)
                .orFail("error").orFailRedirect("http://qq.com");
    }

字段解釋app

/**
     * 錯誤信息
     */
    private String msg;
    /**
     * 狀態 1成功 其它失敗,失敗須要返回msg
     */
    private int state;

    /**
     * 是否跳轉
     */
    private boolean is_redirect = false;

    /**
     * 跳轉地址
     */
    private String redirect_url;

    /**
     * token
     */
    private String token;

Result 普通返回類設計ide

package org.vergil.pojo;

import java.util.Objects;

/**
 * @author: yuantao
 * @Date: 2018/4/20
 * @Description:
 */
public class Result<T> {

    private final static Result<?> EMPTY = new Result<>();

    private T data;

    /**
     * 錯誤信息
     */
    private String msg;
    /**
     * 狀態 1成功 其它失敗,失敗須要返回msg
     */
    private int state;

    /**
     * 是否跳轉
     */
    private boolean is_redirect = false;

    /**
     * 跳轉地址
     */
    private String redirect_url;

    /**
     * token
     */
    private String token;

    public T getData() {
        return data;
    }

    public String getMsg() {
        return msg;
    }

    public int getState() {
        return state;
    }

    public boolean isIs_redirect() {
        return is_redirect;
    }

    public String getRedirect_url() {
        return redirect_url;
    }

    public String getToken() {
        return token;
    }

    private Result() {
        this.data = null;
    }

    private Result(String message, int state) {
        this.msg = message;
        this.state = state;
    }

    private Result(T data, int state) {
        this.data = data;
        this.state = state;
    }

    /**
     *
     * 功能描述: 建立一個空Result類
     * @auther: yuantao
     * @date: 2018/4/21
     */
    public static <T> Result<T> empty() {
        @SuppressWarnings("unchecked")
        Result<T> t = (Result<T>) EMPTY;
        return t;
    }

    /**
     *
     * 功能描述: 生成一個成功狀態Result類
     * @auther: yuantao
     * @param: Data
     * @return: Result<T>
     * @date: 2018/4/21
     */
    public static <T> Result<T> success(T data) {
        return new Result<>(data, 1);
    }

    /**
     *
     * 功能描述: 生成一個成功狀態Result類
     * @auther: yuantao
     * @param: 返回的錯誤信息
     * @return: Result<T>
     * @date: 2018/4/21
     */
    public static <T> Result<T> fail(String message) {
        return new Result<>(message, 0);
    }

    /**
     *
     * 功能描述: 設置跳轉地址
     * @auther: yuantao
     * @param: 跳轉地址
     * @return: Result<T>
     * @date: 2018/4/21
     */
    public <T> Result<T> redirect(String url) {
        this.is_redirect = true;
        this.redirect_url = url;
        return (Result<T>) this;
    }

    /**
     *
     * 功能描述: 設置失敗狀態跳轉地址
     * @auther: yuantao
     * @param: 跳轉地址
     * @return: Result<T>
     * @date: 2018/4/21
     */
    public <T> Result<T> orFailRedirect(String url) {
        if (state == 1) {
            return (Result<T>) this;
        }
        this.is_redirect = true;
        this.redirect_url = url;
        return (Result<T>) this;
    }

    /**
     *
     * 功能描述: 判斷是否傳入值是否爲空,非空則返回值,爲空則返回失敗信息
     * @auther: yuantao
     * @param: 返回的錯誤信息
     * @return: Result<T>
     * @date: 2018/4/21
     */
    public <T> Result<T> orFail(String message) {
        if (null != data) {
            return (Result<T>) this;
        } else {
            this.msg = message;
            this.state = 0;
        }
        return (Result<T>) this;
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(data);
    }


    @Override
    public String toString() {
        return data != null
                ? String.format("result[%s]", data)
                : "result.empty";
    }
}

帶頁碼類設計函數式編程

package org.vergil.pojo;

import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.function.Supplier;

/**
 * @author: yuantao
 * @Date: 2018/4/20
 * @Description:
 */
public class PageResult<T> {

    private final static PageResult<?> EMPTY = new PageResult<>();

    private Data<T> data;

    /**
     * 錯誤信息
     */
    private String msg;
    /**
     * 狀態 1成功 其它失敗,失敗須要返回msg
     */
    private int state;

    /**
     * 是否跳轉
     */
    private boolean is_redirect = false;

    /**
     * 跳轉地址
     */
    private String redirect_url;

    /**
     * token
     */
    private String token;

    public Data<T> getData() {
        return data;
    }

    public String getMsg() {
        return msg;
    }

    public int getState() {
        return state;
    }

    public boolean isIs_redirect() {
        return is_redirect;
    }

    public String getRedirect_url() {
        return redirect_url;
    }

    public String getToken() {
        return token;
    }

    private PageResult() {
        this.data = null;
    }

    private PageResult(String message, int state) {
        this.msg = message;
        this.state = state;
    }

    private PageResult(List<T> list, int state) {
        Data<T> data = new Data<>(list);
        this.data = data;
        this.state = state;
    }

    /**
     * 功能描述: 建立一個空PageResult類
     *
     * @auther: yuantao
     * @date: 2018/4/21
     */
    public static <T> PageResult<T> empty() {
        @SuppressWarnings("unchecked")
        PageResult<T> t = (PageResult<T>) EMPTY;
        return t;
    }

    /**
     * 功能描述: 生成一個成功狀態PageResult類
     *
     * @auther: yuantao
     * @param: List<T>
     * @return: PageResult<T>
     * @date: 2018/4/21
     */
    public static <T> PageResult<T> success(List<T> data) {
        return new PageResult<>(data, 1);
    }

    /**
     * 功能描述: 生成一個失敗狀態PageResult類
     *
     * @auther: yuantao
     * @param: 錯誤信息
     * @return: PageResult<T>
     * @date: 2018/4/21
     */
    public static <T> PageResult<T> fail(String message) {
        return new PageResult<>(message, 0);
    }

    /**
     * 功能描述: 設置頁碼信息
     *
     * @auther: yuantao
     * @param: pageNum, pageSize
     * @return: PageResult<T>
     * @date: 2018/4/21
     */
    public <T> PageResult<T> pageBar(int pageNum, int pageSize) {
        if (state == 1) {
            data.pageBar(pageNum, pageSize);
        }
        return (PageResult<T>) this;
    }

    /**
     * 功能描述: 設置總數
     *
     * @auther: yuantao
     * @param: total
     * @return: PageResult<T>
     * @date: 2018/4/21
     */
    public <T> PageResult<T> total(int total) {
        if (data.pagebar != null) {
            data.pagebar.total(total);
        }
        return (PageResult<T>) this;
    }

    /**
     * 功能描述: 設置跳轉地址
     *
     * @auther: yuantao
     * @param: 跳轉地址
     * @return: PageResult<T>
     * @date: 2018/4/21
     */
    public <T> PageResult<T> redirect(String url) {
        this.is_redirect = true;
        this.redirect_url = url;
        return (PageResult<T>) this;
    }

    /**
     * 功能描述: 設置失敗跳轉地址
     *
     * @auther: yuantao
     * @param: 跳轉地址
     * @return: PageResult<T>
     * @date: 2018/4/21
     */
    public <T> PageResult<T> orFailRedirect(String url) {
        if (state == 1) {
            return (PageResult<T>) this;
        }
        this.is_redirect = true;
        this.redirect_url = url;
        return (PageResult<T>) this;
    }

    /**
     * 功能描述: 判斷是否傳入值是否爲空,非空則返回值,爲空則返回失敗信息
     *
     * @auther: yuantao
     * @param: 返回的錯誤信息
     * @return: PageResult<T>
     * @date: 2018/4/21
     */
    public <T> PageResult<T> orFail(String message) {
        if (null != data.list) {
            return (PageResult<T>) this;
        } else {
            this.msg = message;
            this.state = 0;
        }
        return (PageResult<T>) this;
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(data);
    }


    @Override
    public String toString() {
        return data != null
                ? String.format("result[%s]", data)
                : "result.empty";
    }

    public static class Data<T> {
        /**
         * 接口數據
         */
        private List<T> list;

        /**
         * 分頁信息
         */
        private PageBar pagebar;

        private Data(List<T> list) {
            this.list = list;
        }

        public List<T> getList() {
            return list;
        }

        public PageBar getPagebar() {
            return pagebar;
        }

        public void pageBar(int pageNum, int pageSize) {
            PageBar pageBar = new PageBar(pageNum, pageSize);
            this.pagebar = pageBar;
        }

        public static class PageBar {
            private int page;
            private int total;
            private int limit;

            private PageBar(int page, int limit) {
                this.page = page;
                this.limit = limit;
            }

            private void total(int total) {
                this.total = total;
            }

            public int getPage() {
                return page;
            }

            public int getTotal() {
                return total;
            }

            public int getLimit() {
                return limit;
            }
        }
    }
}
相關文章
相關標籤/搜索