SpringBoot統(tǒng)一數(shù)據(jù)返回的方法實現(xiàn)
一. 概念
其實統(tǒng)一數(shù)據(jù)返回是運用了AOP(對某一類事情的集中處理)的思維,簡單概括就是在我們進行前后端數(shù)據(jù)交互的時候,能夠讓后端的數(shù)據(jù)進行統(tǒng)一的打包封裝,返回給前端,便于前后端的數(shù)據(jù)處理。
二.實現(xiàn)統(tǒng)一數(shù)據(jù)返回
其實統(tǒng)一數(shù)據(jù)返回非常簡單,在實現(xiàn)時要加入類注解@ControllerAdvice(這是一個表示控制通知的注解,在接下來的統(tǒng)一異常處理也要運用到),在添加類注解后還要在實現(xiàn)ResponseBodyAdvice接口,然后重寫接口的responseAdvice接口和beforeBodyWriter方法即可實現(xiàn)。
- 添加類注解@ControllerAdvice,實現(xiàn)接口ResponseBodyAdvice
- 重寫responseAdvice方法
- 重寫beforeBodyWriter方法
- 特殊數(shù)據(jù)類型特殊處理(String)
2.1 重寫responseAdvice方法
responseAdvice方法如下:
@Override public boolean supports(MethodParameter returnType, Class converterType) { return true; }
可以看到,重寫該方法主要返回的是Boolean數(shù)據(jù)類型,返回false表示不執(zhí)行統(tǒng)一返回,返回true表示對于所有的數(shù)據(jù)類型都進行數(shù)據(jù)統(tǒng)一返回。
supports方法:判斷是否要執(zhí)行beforeBodyWrite方法.true為執(zhí)行,false不執(zhí)行.通過該方法可以
選擇哪些類或哪些方法的response要進行處理,其他的不進行處理
2.2 重寫beforeBodyWriter方法
beforeBodyWriter方法如下:
beforeBodyWrite方法:對response方法進行具體操作處理
其中的參數(shù)body最為重要,如果想要對統(tǒng)一后的數(shù)據(jù)進行格式的轉(zhuǎn)換,直接轉(zhuǎn)換body的格式就行,因為這里的body代指了所有的數(shù)據(jù)格式。
三. 特殊類型-String的處理
在經(jīng)過多次不同的數(shù)據(jù)類型測試后發(fā)現(xiàn),其它類型就可以順利返回,而String類型卻返回不正確。
對于String的數(shù)據(jù)類型如果想要返回,我們要先引入ObjectMapper,通過調(diào)用ObjeceMapper的writeValueAsString()方法來把body的數(shù)據(jù)格式給轉(zhuǎn)換成Json格式,然后再次進行統(tǒng)一返回即可。
進行數(shù)據(jù)轉(zhuǎn)換的代碼如下:
private static ObjectMapper mapper = new ObjectMapper(); @Override public Object beforeBodyWrite(Object body, MethodParameter returnType,MediaType selectedContentType, Class selectedConverterType, ServerHttpRequestrequest, ServerHttpResponse response) { //如果返回結(jié)果為String類型, 使?SpringBoot內(nèi)置提供的Jackson來實現(xiàn)信息的序列化 if (body instanceof String){ return mapper.writeValueAsString(Result.success(body)); } }
四. 全部代碼
import com.example.demo.model.Result; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.springframework.core.MethodParameter; import org.springframework.http.MediaType; import org.springframework.http.server.ServerHttpRequest; import org.springframework.http.server.ServerHttpResponse; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; @Slf4j @ControllerAdvice public class ResponseAdvice implements ResponseBodyAdvice { private static ObjectMapper mapper = new ObjectMapper(); @Override public boolean supports(MethodParameter returnType, Class converterType) { return true; } @SneakyThrows @Override public Object beforeBodyWrite(Object body, MethodParameter returnType,MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) { //返回結(jié)果更加靈活 if (body instanceof Result){ return body; } //如果返回結(jié)果為String類型, 使?SpringBoot內(nèi)置提供的Jackson來實現(xiàn)信息的序列化 if (body instanceof String){ return mapper.writeValueAsString(Result.success(body)); } return Result.success(body); } }
- 方便前端程序員更好的接收和解析后端數(shù)據(jù)接口返回的數(shù)據(jù)
- 降低前端程序員和后端程序員的溝通成本,按照某個格式實現(xiàn)就可以了,因為所有接口都是這樣返回
的. - 有利于項目統(tǒng)?數(shù)據(jù)的維護和修改.
- 有利于后端技術部門的統(tǒng)?規(guī)范的標準制定,不會出現(xiàn)稀奇古怪的返回內(nèi)容.
到此這篇關于SpringBoot統(tǒng)一數(shù)據(jù)返回的方法實現(xiàn)的文章就介紹到這了,更多相關SpringBoot統(tǒng)一數(shù)據(jù)返回內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
slf4j與jul、log4j1、log4j2、logback的集成原理
這篇文章主要介紹了slf4j與jul、log4j1、log4j2、logback的集成原理,以及通用日志框架與具體日志實現(xiàn)系統(tǒng)的機制機制介紹,包括依賴的jar包,jar沖突處理等2022-03-03java Person,Student,GoodStudent 三個類的繼承、構(gòu)造函數(shù)的執(zhí)行
這篇文章主要介紹了java Person,Student,GoodStudent 三個類的繼承、構(gòu)造函數(shù)的執(zhí)行,需要的朋友可以參考下2017-02-02mall整合SpringTask實現(xiàn)定時任務的方法示例
這篇文章主要介紹了mall整合SpringTask實現(xiàn)定時任務的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-06-06Spingboot?JPA?CriteriaBuilder?如何獲取指定字段
這篇文章?主要介紹了Spingboot?JPA?CriteriaBuilder?如何獲取指定字段,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12解決mybatisplus插入報錯argument type mismatch的問題
這篇文章主要介紹了解決mybatisplus插入報錯argument type mismatch的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11