<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ## 為什么要統一API響應結果 * 對于移動端和PC端開發而言,調用接口返回有統一的響應體,可以針對性的設計界面,代碼結構更加清晰,層次也更加分明。 * 統一根據返回的狀態碼判斷接口調用的具體情況,從而進行下一步操作 * 統一根據返回的的消息字段顯示接口調用的具體狀態 * 統一根據返回的數據承載進行數據的交互與展示 ## API 響應類代碼 ~~~ /** * 統一API響應結果封裝 * * @author Chill */ @Getter @Setter @ToString @ApiModel(description = "返回信息") @NoArgsConstructor(access = AccessLevel.PRIVATE) public class R<T> implements Serializable { private static final long serialVersionUID = 1L; @ApiModelProperty(value = "狀態碼", required = true) private int code; @ApiModelProperty(value = "是否成功", required = true) private boolean success; @ApiModelProperty(value = "承載數據") private T data; @ApiModelProperty(value = "返回消息", required = true) private String msg; private R(IResultCode resultCode) { this(resultCode, null, resultCode.getMessage()); } private R(IResultCode resultCode, String msg) { this(resultCode, null, msg); } private R(IResultCode resultCode, T data) { this(resultCode, data, resultCode.getMessage()); } private R(IResultCode resultCode, T data, String msg) { this(resultCode.getCode(), data, msg); } private R(int code, T data, String msg) { this.code = code; this.data = data; this.msg = msg; this.success = ResultCode.SUCCESS.code == code; } /** * 判斷返回是否為成功 * * @param result Result * @return 是否成功 */ public static boolean isSuccess(@Nullable R<?> result) { return Optional.ofNullable(result) .map(x -> ObjectUtil.nullSafeEquals(ResultCode.SUCCESS.code, x.code)) .orElse(Boolean.FALSE); } /** * 判斷返回是否為成功 * * @param result Result * @return 是否成功 */ public static boolean isNotSuccess(@Nullable R<?> result) { return !R.isSuccess(result); } /** * 返回R * * @param data 數據 * @param <T> T 泛型標記 * @return R */ public static <T> R<T> data(T data) { return data(data, BladeConstant.DEFAULT_SUCCESS_MESSAGE); } /** * 返回R * * @param data 數據 * @param msg 消息 * @param <T> T 泛型標記 * @return R */ public static <T> R<T> data(T data, String msg) { return data(HttpServletResponse.SC_OK, data, msg); } /** * 返回R * * @param code 狀態碼 * @param data 數據 * @param msg 消息 * @param <T> T 泛型標記 * @return R */ public static <T> R<T> data(int code, T data, String msg) { return new R<>(code, data, data == null ? BladeConstant.DEFAULT_NULL_MESSAGE : msg); } /** * 返回R * * @param msg 消息 * @param <T> T 泛型標記 * @return R */ public static <T> R<T> success(String msg) { return new R<>(ResultCode.SUCCESS, msg); } /** * 返回R * * @param resultCode 業務代碼 * @param <T> T 泛型標記 * @return R */ public static <T> R<T> success(IResultCode resultCode) { return new R<>(resultCode); } /** * 返回R * * @param resultCode 業務代碼 * @param msg 消息 * @param <T> T 泛型標記 * @return R */ public static <T> R<T> success(IResultCode resultCode, String msg) { return new R<>(resultCode, msg); } /** * 返回R * * @param msg 消息 * @param <T> T 泛型標記 * @return R */ public static <T> R<T> failure(String msg) { return new R<>(ResultCode.FAILURE, msg); } /** * 返回R * * @param code 狀態碼 * @param msg 消息 * @param <T> T 泛型標記 * @return R */ public static <T> R<T> failure(int code, String msg) { return new R<>(code, null, msg); } /** * 返回R * * @param resultCode 業務代碼 * @param <T> T 泛型標記 * @return R */ public static <T> R<T> failure(IResultCode resultCode) { return new R<>(resultCode); } /** * 返回R * * @param resultCode 業務代碼 * @param msg 消息 * @param <T> T 泛型標記 * @return R */ public static <T> R<T> failure(IResultCode resultCode, String msg) { return new R<>(resultCode, msg); } /** * 返回R * * @param flag 成功狀態 * @return R */ public static R status(boolean flag) { return flag ? success(BladeConstant.DEFAULT_SUCCESS_MESSAGE) : failure(BladeConstant.DEFAULT_FAILURE_MESSAGE); } } ~~~ <br> ## 如何使用 1. 擼起袖子來優化下上一章我們新建的 API ~~~ @GetMapping("info") @PreAuth("hasRole('administrator')") public R<String> info(String name) { return R.data("Hello, My Name Is: " + name); } @GetMapping("count") @PreAuth("permitAll()") public R<Integer> count(Integer cnt) { return R.data(cnt * 10); } ~~~ 2. 調用 API 查看具體返回 ![](https://box.kancloud.cn/30acc1362146d7b019c513663d995825_596x415.png) 3. 可以看到,返回接口變成了Json,并且字段清晰,分別代表 狀態碼、是否成功、數據承載、消息 <br> ## 注 * 每個 API 都需要規范,具有統一的響應結果,這樣才更利于后續的開發 * 移動端、PC端對接的時候,也請根據`success`來判斷接口是否成功而不是根據`code`來判斷。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看