十年網(wǎng)站開(kāi)發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無(wú)憂售后,網(wǎng)站問(wèn)題一站解決
本篇內(nèi)容主要講解“如何約定前后端對(duì)接數(shù)據(jù)格式”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“如何約定前后端對(duì)接數(shù)據(jù)格式”吧!
成都創(chuàng)新互聯(lián)公司主營(yíng)蒙山網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,重慶APP開(kāi)發(fā),蒙山h5微信小程序開(kāi)發(fā)搭建,蒙山網(wǎng)站營(yíng)銷推廣歡迎蒙山等地區(qū)企業(yè)咨詢
清單1:返回信息格式約定源碼示例
package site.syksy.qingzhou.web.response; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import io.swagger.v3.oas.annotations.media.Schema; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import java.io.Serializable; /** * @author Raspberry */ @Schema(title = "返回信息") public class ResponseMessageimplements Serializable { private static final long serialVersionUID = 1L; @Schema(title = "是否成功") private Boolean success; @Schema(title = "數(shù)據(jù)") private T data; @Schema(title = "錯(cuò)誤碼") private String errorCode; @Schema(title = "錯(cuò)誤信息") private String errorMessage; /** * error display type: 0 silent; 1 message.warn; 2 message.error; 4 notification; 9 page */ @Schema(title = "錯(cuò)誤信息顯示類型") private Integer showType; /** * Convenient for back-end Troubleshooting: unique request ID */ @Schema(title = "唯一請(qǐng)求ID") private String traceId; /** * onvenient for backend Troubleshooting: host of current access server */ @Schema(title = "當(dāng)前訪問(wèn) ResponseMessage 類中的屬性是依據(jù) ant design pro 文檔中推薦。
統(tǒng)一包裝
約定好前后端交互數(shù)據(jù)格式后,我們將在每個(gè) HTTP 接口方法中對(duì)返回結(jié)果進(jìn)行包裝,但這樣顯得冗余。這些重復(fù)冗余的代碼能否集中處理呢?當(dāng)然是可以的!只需創(chuàng)建一個(gè)類,實(shí)現(xiàn) **ResponseBodyAdvice **接口,再加上一個(gè)注解 **@RestControllerAdvice **就能實(shí)現(xiàn)統(tǒng)一對(duì)返回結(jié)果進(jìn)行包裝處理,具體請(qǐng)看清單2中的源碼。
清單2:統(tǒng)一包裝返回信息源碼示例
package site.syksy.qingzhou.web.response; 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 Raspberry */ @RestControllerAdvice public class GeneralResponseBodyAdvice implements ResponseBodyAdvice**GeneralResponseBodyAdvice **類中的兩個(gè)方法解釋如下:
supports:通過(guò)檢查 Controller 類包路徑是否匹配 site.syksy.qingzhou,返回 ture ,則會(huì)執(zhí)行 beforeBodyWrite 方法。
beforeBodyWrite:先對(duì) Controller 返回結(jié)果進(jìn)行判斷,是否已包裝過(guò)(類型屬于 ResponseMessage ),是否是 String 類型( String 類型,后續(xù)轉(zhuǎn)換器不會(huì)將其轉(zhuǎn)成 json)。如果以上判斷皆未通過(guò),將對(duì)結(jié)果包裝成 ResponseMessage 格式。
清單2示例中通過(guò)包路徑來(lái)約束,也可通過(guò)其它方式來(lái)約束。不過(guò)注意,一定要約束范圍,否則如 springdoc-openapi 的接口也將包裝,則訪問(wèn) swagger ui 時(shí)將不能正確解析。
到此,相信大家對(duì)“如何約定前后端對(duì)接數(shù)據(jù)格式”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
本文題目:如何約定前后端對(duì)接數(shù)據(jù)格式
鏈接URL:http://m.jiaotiyi.com/article/jchjed.html