最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot返回結(jié)果統(tǒng)一處理實(shí)例詳解

 更新時(shí)間:2023年12月10日 17:33:05   作者:wx59bcc77095d22  
這篇文章主要為大家介紹了SpringBoot返回結(jié)果統(tǒng)一處理實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

一、前言

在Web開發(fā)中,我們常常需要對API接口的返回結(jié)果進(jìn)行統(tǒng)一的包裝,以方便客戶端對數(shù)據(jù)和異常情況的統(tǒng)一處理。我們可以自定義返回接口結(jié)果包裝類。

二、創(chuàng)建返回結(jié)果枚舉類

package com.example.hellodemo.enums;
/**
 * @author qx
 * @date 2023/11/30
 * @des 返回結(jié)果枚舉類
 */
public enum ResultTypeEnum {
    SUCCESS(0, "成功"), FAILURE(1, "失敗");
    private final int code;
    private final String msg;
    ResultTypeEnum(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }
    public int getCode() {
        return code;
    }
    public String getMsg() {
        return msg;
    }
}

三、定義統(tǒng)一返回結(jié)果類

package com.example.hellodemo.bean;
import com.example.hellodemo.enums.ResultTypeEnum;
import java.io.Serializable;
/**
 * @author qx
 * @date 2023/11/30
 * @des 統(tǒng)一返回結(jié)果類
 */
public class ResultResponse<T> implements Serializable {
    private int code;
    private String msg;
    private T data;
    public ResultResponse(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }
    public static <T> ResultResponse success() {
        return new ResultResponse(ResultTypeEnum.SUCCESS.getCode(), ResultTypeEnum.SUCCESS.getMsg(), null);
    }
    public static <T> ResultResponse success(T data) {
        return new ResultResponse(ResultTypeEnum.SUCCESS.getCode(), ResultTypeEnum.SUCCESS.getMsg(), data);
    }
    public static <T> ResultResponse success(String msg, T data) {
        return new ResultResponse(ResultTypeEnum.SUCCESS.getCode(), msg, data);
    }
    public static ResultResponse failure() {
        return new ResultResponse(ResultTypeEnum.FAILURE.getCode(), ResultTypeEnum.FAILURE.getMsg(), null);
    }
    public static ResultResponse failure(String msg) {
        return new ResultResponse(ResultTypeEnum.FAILURE.getCode(), msg, null);
    }
    public int getCode() {
        return code;
    }
    public void setCode(int code) {
        this.code = code;
    }
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
    public T getData() {
        return data;
    }
    public void setData(T data) {
        this.data = data;
    }
}

四、創(chuàng)建控制器

package com.example.hellodemo.controller;
import com.example.hellodemo.bean.ResultResponse;
import com.example.hellodemo.bean.one.DbOneEntity;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
/**
 * @author qx
 * @date 2023/11/30
 * @des
 */
@RestController
public class DbController {
    @PostMapping("/test")
    public ResultResponse<Map<String, Object>> testDemo(@RequestParam String name, @RequestParam Integer id) {
        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("name", name);
        hashMap.put("id", id);
        return ResultResponse.success(hashMap);
    }
}

測試:

我們也可以返回自定義的msg。

package com.example.hellodemo.controller;
import com.example.hellodemo.bean.ResultResponse;
import com.example.hellodemo.bean.one.DbOneEntity;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
/**
 * @author qx
 * @date 2023/11/30
 * @des
 */
@RestController
public class DbController {
    @PostMapping("/test")
    public ResultResponse<Map<String, Object>> testDemo(@RequestParam String name, @RequestParam Integer id) {
        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("name", name);
        hashMap.put("id", id);
        return ResultResponse.success("獲取數(shù)據(jù)成功",hashMap);
    }
}

測試:

五、全局異常統(tǒng)一返回類

package com.example.hellodemo.exception;
import com.example.hellodemo.bean.ResultResponse;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
 * @author qx
 * @date 2023/11/30
 * @des 全局異常統(tǒng)一返回處理類
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 捕獲Exceptino異常類型
     *
     * @param e
     * @return 返回異常統(tǒng)一返回處理結(jié)果
     */
    @ExceptionHandler(value = {Exception.class})
    public ResultResponse exceptionHandler(Exception e) {
        return ResultResponse.failure(e.getMessage());
    }
}

我們在控制器中自己創(chuàng)建一個(gè)異常,然后請求接口,看看返回什么?

package com.example.hellodemo.controller;
import com.example.hellodemo.bean.ResultResponse;
import com.example.hellodemo.bean.one.DbOneEntity;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
/**
 * @author qx
 * @date 2023/11/30
 * @des
 */
@RestController
public class DbController {
    @PostMapping("/test")
    public ResultResponse<Map<String, Object>> testDemo(@RequestParam String name, @RequestParam Integer id) {
        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("name", name);
        hashMap.put("id", id);
        // 自己寫一個(gè)異常
        int i = 1 / 0;
        return ResultResponse.success("獲取數(shù)據(jù)成功", hashMap);
    }
}

測試:

我們看到msg已經(jīng)返回了異常的相關(guān)信息。

六、Spring切面實(shí)現(xiàn)自動(dòng)返回統(tǒng)一結(jié)果

package com.example.hellodemo.config;
import com.example.hellodemo.bean.ResultResponse;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
/**
 * @author qx
 * @date 2023/11/30
 * @des 全局統(tǒng)一返回結(jié)果
 */
@RestControllerAdvice
public class GlobalResponseAdvice implements ResponseBodyAdvice<Object> {
    @Override
    public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
        return true;
    }
    @Override
    public Object beforeBodyWrite(Object body, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
        // 要排除的api,里面的接口不需要統(tǒng)一包裝
        String[] excludePath = {};
        for (String path : excludePath) {
            if (serverHttpRequest.getURI().getPath().startsWith(path)) {
                return body;
            }
        }
        if (body instanceof ResultResponse) {
            return body;
        }
        return ResultResponse.success(body);
    }
}

我們定義一個(gè)不帶返回格式的控制器。

package com.example.hellodemo.controller;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
/**
 * @author qx
 * @date 2023/11/30
 * @des
 */
@RestController
public class DbController {
    @PostMapping("/test")
    public Map<String, Object> testDemo(@RequestParam String name, @RequestParam Integer id) {
        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("name", name);
        hashMap.put("id", id);
        return hashMap;
    }
}

我們進(jìn)行測試:

我們發(fā)現(xiàn)我們使用切面設(shè)置統(tǒng)一返回結(jié)果的封裝成功了,這樣就統(tǒng)一了返回格式,對代碼沒有侵入。

我們注意全局異常統(tǒng)一返回和切面使用統(tǒng)一返回結(jié)果都使用了一個(gè)注解@RestControllerAdvice。

以上就是SpringBoot返回結(jié)果統(tǒng)一處理實(shí)例詳解的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot返回結(jié)果統(tǒng)一處理的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • java如何使用正則表達(dá)式限制特殊字符的個(gè)數(shù)

    java如何使用正則表達(dá)式限制特殊字符的個(gè)數(shù)

    這篇文章主要介紹了java如何使用正則表達(dá)式限制特殊字符的個(gè)數(shù),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Java字符串split使用方法代碼實(shí)例

    Java字符串split使用方法代碼實(shí)例

    這篇文章主要介紹了Java字符串split使用方法代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07
  • 一文教你如何使用原生的Feign

    一文教你如何使用原生的Feign

    Feign使得 Java HTTP 客戶端編寫更方便,Feign 靈感來源于Retrofit、JAXRS-2.0和WebSocket,這篇文章主要給大家介紹了如何使用原生的Feign的相關(guān)資料,需要的朋友可以參考下
    2021-10-10
  • java 加密之RSA算法加密與解密的實(shí)例詳解

    java 加密之RSA算法加密與解密的實(shí)例詳解

    這篇文章主要介紹了java 加密之RSA算法加解密與解密的實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • Java構(gòu)造函數(shù)與普通函數(shù)用法詳解

    Java構(gòu)造函數(shù)與普通函數(shù)用法詳解

    本篇文章給大家詳細(xì)講述了Java構(gòu)造函數(shù)與普通函數(shù)用法以及相關(guān)知識點(diǎn),對此有興趣的朋友可以參考學(xué)習(xí)下。
    2018-03-03
  • Idea中添加Maven項(xiàng)目支持scala的詳細(xì)步驟

    Idea中添加Maven項(xiàng)目支持scala的詳細(xì)步驟

    這篇文章主要介紹了Idea中添加Maven項(xiàng)目支持scala,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-03-03
  • SpringBoot整合任務(wù)系統(tǒng)quartz和SpringTask的方法

    SpringBoot整合任務(wù)系統(tǒng)quartz和SpringTask的方法

    這篇文章主要介紹了SpringBoot整合任務(wù)系統(tǒng)(quartz和SpringTask),Quartz是一個(gè)比較成熟了的定時(shí)任務(wù)框架,但是捏,它稍微的有些許繁瑣,本文先給大家講解下Quartz的一些基本概念結(jié)合實(shí)例代碼給大家詳細(xì)講解,需要的朋友可以參考下
    2022-10-10
  • 淺談SpringBoot如何自定義Starters

    淺談SpringBoot如何自定義Starters

    今天帶大家來學(xué)習(xí)SpringBoot如何自定義Starters,文中有非常詳細(xì)的圖文介紹及代碼示例,對正在學(xué)習(xí)java的小伙伴們很有幫助,需要的朋友可以參考下
    2021-05-05
  • java實(shí)現(xiàn)分布式鎖的常用三種方式

    java實(shí)現(xiàn)分布式鎖的常用三種方式

    本文主要介紹了java實(shí)現(xiàn)分布式鎖,一般有這3種方式,基于數(shù)據(jù)庫實(shí)現(xiàn)的分布式鎖、基于Redis實(shí)現(xiàn)的分布式鎖和基于Zookeeper實(shí)現(xiàn)的分布式鎖,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-08-08
  • 從入門到精通Spring Boot中WebSocket常用方法

    從入門到精通Spring Boot中WebSocket常用方法

    本文將從入門到精通,詳細(xì)介紹Spring Boot中WebSocket的常用使用方法,涵蓋協(xié)議特性、消息收發(fā)、點(diǎn)對點(diǎn)通信、認(rèn)證攔截及注解實(shí)現(xiàn)方式,,需要的朋友跟隨小編一起看看吧
    2025-08-08

最新評論

奇台县| 临桂县| 金堂县| 蓝山县| 平和县| 四子王旗| 乌拉特前旗| 玉屏| 滕州市| 天峨县| 南皮县| 寿光市| 遂平县| 乌兰县| 唐海县| 岳阳市| 万载县| 菏泽市| 梅州市| 罗源县| 进贤县| 邹平县| 江阴市| 北京市| 莱西市| 图木舒克市| 洪洞县| 南召县| 黑龙江省| 镇沅| 龙南县| 车险| 靖江市| 通州区| 登封市| 望谟县| 伊川县| 黄冈市| 龙州县| 甘谷县| 辉南县|