java遞歸實(shí)現(xiàn)拼裝多個api的結(jié)果操作方法
工作需要,經(jīng)常需要實(shí)現(xiàn)api接口,但每次都是大同小異,我就考慮是否可以將這種重復(fù)性的工作配置化。
我就寫一個模板api,然后所有的HTTP請求過來,根據(jù)不同的配置返回不同結(jié)果。
最開始考慮的是比較簡單的,來一個api需求,我就去MySQL查一條這個api對應(yīng)的SQL,然后拿SQL去取結(jié)果,返回。
這個不難。
關(guān)鍵是實(shí)際需求中,有很多api返回的數(shù)據(jù)很復(fù)雜,比如渲染地圖的接口,一般一條SQL搞不定。
那我就想,那我能不能實(shí)現(xiàn)api的拼裝呢,你看到我只是調(diào)用了一個API,但是我給你返回的結(jié)果,其實(shí)是好幾個API結(jié)果拼裝成的。
經(jīng)過研究,是可以實(shí)現(xiàn)的。
首先我們定義一個ApiConfig的模型
@Data @Table(name = "api_config") @AllArgsConstructor public class ApiConfig implements Serializable { @ApiModelProperty("api名稱") private String apiName; @ApiModelProperty("數(shù)據(jù)源名稱") private String dsName; @ApiModelProperty("SQL") private String querySql; @ApiModelProperty("結(jié)果類型") private String resultType; @ApiModelProperty("結(jié)果描述") private String resultDesc; @ApiModelProperty("依賴api") private String dependApiName; }
接下來就是我們的實(shí)現(xiàn)類,因?yàn)槭钦宫F(xiàn)可行性,所以我們不分層,在一個Test類中把所有邏輯實(shí)現(xiàn)
{"key1":"x/y/1",
"key2":"x/y/2"}
接下來就是我們的實(shí)現(xiàn)類,因?yàn)槭钦宫F(xiàn)可行性,所以我們不分層,在一個Test類中把所有邏輯實(shí)現(xiàn)
@Slf4j public class Test { //測試數(shù)據(jù)的初始化 public static List<ApiConfig> apiConfigList = new ArrayList<>(); public static Map<String, String> sqlResultMap = ImmutableMap.of("sql1", "{\"a\":\"1\"}", "sql2", "{\"b\":\"2\"}", "sql3", "{\"c\":\"3\"}"); static { ApiConfig api1 = new ApiConfig("p1", "d1", "sql1", "map", "", "{\"b\":\"p1/x1\"}"); ApiConfig api2 = new ApiConfig("p1/x1", "d1", "sql2", "map", "", "{\"c\":\"p1/x2\"}"); ApiConfig api3 = new ApiConfig("p1/x2", "d1", "sql3", "map", "", null); apiConfigList.add(api1); apiConfigList.add(api2); apiConfigList.add(api3); } /** * 我要進(jìn)行http:ip:port/p1這個請求,請返回我相關(guān)數(shù)據(jù) * @param args */ public static void main(String[] args) { //根據(jù)api名稱獲取結(jié)果 String apiName = "p1"; JSONObject json = doGetResult(apiName); //result必須初始化,而且在方法內(nèi)部不能重新new,以保證遞歸方法內(nèi)更新的是同一個對象,否則拿不到更新數(shù)據(jù)后的result JSONVO result = null; if (json != null) { result = new JSONVO(json.toJSONString()); } else { result = new JSONVO("{}"); } //如有需要,遞歸獲取子api的結(jié)果,并存入result getApiResult(apiName, null, result); System.out.println(result); } /** * 從子api查詢結(jié)果,并更新到主result * @param apiName * @param dataKey * @param result */ public static void getApiResult(String apiName, String dataKey, JSONVO result) { //dataKey在進(jìn)入方法時是等于null的,第二次進(jìn)入肯定不應(yīng)該為null,這個地方是更新result的關(guān)鍵位置 if (dataKey != null) { JSONObject json = doGetResult(apiName); result.set(dataKey, json); } //進(jìn)入遞歸的入口 String dependApiName = getApiConfig(apiName).getDependApiName(); if (dependApiName != null) { JSONObject dependApi = JSONObject.parseObject(dependApiName); Set<String> keySet = dependApi.keySet(); for (String key : keySet) { String subApi = dependApi.getString(key); getApiResult(subApi, key, result); } } } public static JSONObject doGetResult(String apiName) { String querySql = getApiConfig(apiName).getQuerySql(); return doQuery(querySql); } /** * 根據(jù)api名稱獲取apiConfig * * @param api * @return */ public static ApiConfig getApiConfig(String api) { for (ApiConfig apiConfig : apiConfigList) { if (apiConfig.getApiName().equals(api)) { return apiConfig; } } log.error("api not exists!"); return null; } /** * 根據(jù)查詢SQL獲取結(jié)果 * * @param sql * @return */ public static JSONObject doQuery(String sql) { String s = sqlResultMap.get(sql); JSONObject jsonObject = JSONObject.parseObject(s); return jsonObject; } }
輸出結(jié)果:
{"a":"1","b":{"b":"2"},"c":{"c":"3"}}
可以看到,兩層遞歸的子api的數(shù)據(jù)都查出來了。
從數(shù)據(jù)庫返回的結(jié)果,可能也不一定是JsonObject,這個在實(shí)現(xiàn)項(xiàng)目中需要在具體分析。
到此這篇關(guān)于java遞歸實(shí)現(xiàn)拼裝多個api的結(jié)果的文章就介紹到這了,更多相關(guān)java遞歸拼裝內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring mvc 和ajax異步交互完整實(shí)例代碼
本篇文章主要介紹了spring mvc 和ajax異步交互完整實(shí)例代碼,簡單的AJAX+SpringMVC的異步交互小例子,有興趣的可以了解一下。2017-02-02java中fastjson生成和解析json數(shù)據(jù)(序列化和反序列化數(shù)據(jù))
本篇文章主要介紹了java中fastjson生成和解析json數(shù)據(jù)(序列化和反序列化數(shù)據(jù)),具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-02-02Spring Boot中的WebSocketMessageBrokerConfigurer接口使用
在SpringBoot中,我們可以使用 WebSocketMessageBrokerConfigurer接口來配置WebSocket消息代理,以實(shí)現(xiàn)實(shí)時通信,具有一定的參考價值,感興趣的可以了解一下2023-11-11java?WebSocket?服務(wù)端實(shí)現(xiàn)代碼
WebSocket協(xié)議是基于TCP的一種新的網(wǎng)絡(luò)協(xié)議。它實(shí)現(xiàn)了瀏覽器與服務(wù)器全雙工(full-duplex)通信——允許服務(wù)器主動發(fā)送信息給客戶端,這篇文章主要介紹了java?WebSocket?服務(wù)端代碼,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-02-02Java利用布隆過濾器實(shí)現(xiàn)快速檢查元素是否存在
布隆過濾器是一個很長的二進(jìn)制向量和一系列隨機(jī)映射函數(shù)。布隆過濾器可以用于檢索一個元素是否在一個集合中。本文就來詳細(xì)說說實(shí)現(xiàn)的方法,需要的可以參考一下2022-10-10