Spring?Boot如何實(shí)現(xiàn)統(tǒng)一數(shù)據(jù)返回
統(tǒng)一數(shù)據(jù)返回
一. 概念
其實(shí)統(tǒng)一數(shù)據(jù)返回是運(yùn)用了AOP(對(duì)某一類事情的集中處理)的思維,簡(jiǎn)單概括就是在我們進(jìn)行前后端數(shù)據(jù)交互的時(shí)候,能夠讓后端的數(shù)據(jù)進(jìn)行統(tǒng)一的打包封裝,返回給前端,便于前后端的數(shù)據(jù)處理。
二.實(shí)現(xiàn)統(tǒng)一數(shù)據(jù)返回
其實(shí)統(tǒng)一數(shù)據(jù)返回非常簡(jiǎn)單,在實(shí)現(xiàn)時(shí)要加入類注解@ControllerAdvice(這是一個(gè)表示控制通知的注解,在接下來(lái)的統(tǒng)一異常處理也要運(yùn)用到),在添加類注解后還要在實(shí)現(xiàn)ResponseBodyAdvice接口,然后重寫接口的responseAdvice接口和beforeBodyWriter方法即可實(shí)現(xiàn)。
- 添加類注解@ControllerAdvice,實(shí)現(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表示對(duì)于所有的數(shù)據(jù)類型都進(jìn)行數(shù)據(jù)統(tǒng)一返回。
supports方法:判斷是否要執(zhí)行beforeBodyWrite方法.true為執(zhí)行,false不執(zhí)行.通過(guò)該方法可以
選擇哪些類或哪些方法的response要進(jìn)行處理,其他的不進(jìn)行處理
2.2 重寫beforeBodyWriter方法
beforeBodyWriter方法如下:
beforeBodyWrite方法:對(duì)response方法進(jìn)行具體操作處理
其中的參數(shù)body最為重要,如果想要對(duì)統(tǒng)一后的數(shù)據(jù)進(jìn)行格式的轉(zhuǎn)換,直接轉(zhuǎn)換body的格式就行,因?yàn)檫@里的body代指了所有的數(shù)據(jù)格式。
三. 特殊類型-String的處理
在經(jīng)過(guò)多次不同的數(shù)據(jù)類型測(cè)試后發(fā)現(xiàn),其它類型就可以順利返回,而String類型卻返回不正確。
對(duì)于String的數(shù)據(jù)類型如果想要返回,我們要先引入ObjectMapper,通過(guò)調(diào)用ObjeceMapper的writeValueAsString()方法來(lái)把body的數(shù)據(jù)格式給轉(zhuǎn)換成Json格式,然后再次進(jìn)行統(tǒng)一返回即可。
進(jìn)行數(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來(lái)實(shí)現(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來(lái)實(shí)現(xiàn)信息的序列化 if (body instanceof String){ return mapper.writeValueAsString(Result.success(body)); } return Result.success(body); } }
- 方便前端程序員更好的接收和解析后端數(shù)據(jù)接口返回的數(shù)據(jù)
- 降低前端程序員和后端程序員的溝通成本,按照某個(gè)格式實(shí)現(xiàn)就可以了,因?yàn)樗薪涌诙际沁@樣返回
的. - 有利于項(xiàng)目統(tǒng)?數(shù)據(jù)的維護(hù)和修改.
- 有利于后端技術(shù)部門的統(tǒng)?規(guī)范的標(biāo)準(zhǔn)制定,不會(huì)出現(xiàn)稀奇古怪的返回內(nèi)容.
- SpringBoot統(tǒng)一數(shù)據(jù)返回格式的實(shí)現(xiàn)示例
- Spring Boot 統(tǒng)一數(shù)據(jù)返回格式的解決方案
- springboot統(tǒng)一接口返回?cái)?shù)據(jù)的實(shí)現(xiàn)
- SpringBoot統(tǒng)一返回處理出現(xiàn)cannot?be?cast?to?java.lang.String異常解決
- SpringBoot全局Controller返回值格式統(tǒng)一
- springboot配置Jackson返回統(tǒng)一默認(rèn)值的實(shí)現(xiàn)示例
- 詳解SpringBoot如何統(tǒng)一后端返回格式
相關(guān)文章
SpringBoot配置數(shù)據(jù)庫(kù)密碼加密的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot配置數(shù)據(jù)庫(kù)密碼加密的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03Java8新特性Stream流中anyMatch和allMatch和noneMatch的區(qū)別解析
這篇文章主要介紹了Java8新特性Stream流中anyMatch和allMatch和noneMatch的區(qū)別解析,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-01-01java 跳轉(zhuǎn)搜索的實(shí)現(xiàn)示例
與二分搜索一樣,跳轉(zhuǎn)搜索是一種針對(duì)排序數(shù)組的搜索算法,本文主要介紹了java 跳轉(zhuǎn)搜索的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-04-04Spring 中jdbcTemplate 實(shí)現(xiàn)執(zhí)行多條sql語(yǔ)句示例
本篇文章主要介紹了Spring 中jdbcTemplate 實(shí)現(xiàn)執(zhí)行多條sql語(yǔ)句示例,可以對(duì)多個(gè)表執(zhí)行多個(gè)sql語(yǔ)句,有興趣的可以了解一下。2017-01-01idea新建Springboot項(xiàng)目,設(shè)置默認(rèn)maven和jdk版本方式
這篇文章主要介紹了idea新建Springboot項(xiàng)目,設(shè)置默認(rèn)maven和jdk版本方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12springBoot 打war包 程序包c(diǎn)om.sun.istack.internal不存在的問(wèn)題及解決方案
這篇文章主要介紹了springBoot 打war包 程序包c(diǎn)om.sun.istack.internal不存在的問(wèn)題及解決方案,親測(cè)試過(guò)可以,需要的朋友可以參考下2018-07-07