欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot動態(tài)生成接口實現(xiàn)流程示例講解

 更新時間:2023年01月11日 08:57:40   作者:李奈 - Leemon  
最近遇到一個需求,需要在程序運行過程中,可以動態(tài)新增接口,自定義接口參數(shù)名稱,基本類型,以及請求方法,請求頭等等。通過幾天的研究,找到了我需要的解決方案

對于這個需求,我首先要研究的是程序是怎么加載非@Controller/@RequestMapping等等注解下的接口,然后發(fā)現(xiàn)加載接口都需要被RequestMappingInfo處理,可以通過該類進行動態(tài)接口生成。

一,簡單例子

首先,我要做一件最簡單的事,就是在程序運行時加載一個我自定義的接口,具體代碼如下:

@SpringBootApplication
public class ServiceApiApplication {
    public static void main(String[] args) throws NoSuchMethodException {
        ApplicationContext application = SpringApplication.run(ServiceApiApplication.class, args);
        RequestMappingHandlerMapping bean = application.getBean(RequestMappingHandlerMapping.class);
        RequestMappingInfo requestMappingInfo = RequestMappingInfo.paths("/lmcTest").methods(RequestMethod.GET).build();
        bean.registerMapping(requestMappingInfo, "adapterController", AdapterController.class.getDeclaredMethod("myTest"));
    }

AdapterController.java

/**
 * @ClassName: AdapterController
 * @Description: TODO
 * @version: 1.0
 */
@RestController
@Slf4j
public class AdapterController {
    Object myTest() {
        return "this is test request";
    }
}

運行程序后,訪問接口 http://localhost:8070/lmcTest,可以正常訪問到接口內(nèi)容,結(jié)果如下:

this is test request

二,各種請求方法以及條件

剛才的例子是一個最簡單無參的get請求,但實際需求中我們的接口可能帶有參數(shù)等等不同的需求。對于各種條件下的動態(tài)接口,如下所示

2.1 無參GET方法

		// 無參get方法
        RequestMappingInfo requestMappingInfo = RequestMappingInfo.paths("/lmcTest").methods(RequestMethod.GET).build();
        bean.registerMapping(requestMappingInfo, "adapterController", AdapterController.class.getDeclaredMethod("myTest"));

請求舉例: http://localhost:8070/lmcTest

2.2 帶1參的GET方法

        // 帶一參數(shù)的get方法
        RequestMappingInfo requestMappingInfo1 = RequestMappingInfo.paths("/lmcTest2").params(new String[]{"fileName"}).methods(RequestMethod.GET).build();
        bean.registerMapping(requestMappingInfo1, "adapterController", AdapterController.class.getDeclaredMethod("myTest2", String.class));

AdapterController.java

	Object myTest2(@RequestParam("fileName") String value) {
        return "this is my param : " + value;
    }

	Object myTest2(String fileName) {
        return "this is my param : " + fileName;
    }

請求舉例:http://localhost:8070/lmcTest2?fileName=hhh

結(jié)果如下:

this is my param : hhh

2.3 帶多參的GET方法

        // 帶多個參數(shù)的get方法
        RequestMappingInfo requestMappingInfo2 = RequestMappingInfo.paths("/lmcTest3")
                .params(new String[]{"fileName", "type", "isSort"})
                .methods(RequestMethod.GET).build();
        bean.registerMapping(requestMappingInfo2, "adapterController", AdapterController.class.getDeclaredMethod("myTest3", String.class, String.class, Boolean.class));

AdapterController.java

	Object myTest3(String fileName, String type, Boolean isSort) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("fileName", fileName);
        jsonObject.put("type", type);
        jsonObject.put("isSort", isSort);
        return "values : " + jsonObject.toJSONString();
    }

請求舉例:http://localhost:8070/lmcTest3?fileName=hhh&isSort=false&type=KKK

結(jié)果如下:

values : {"isSort":false,"fileName":"hhh","type":"KKK"}

2.4 無參POST方法

		// 無參post方法
		RequestMappingInfo requestMappingInfo3 = RequestMappingInfo.paths("/lmcTest4").methods(RequestMethod.POST).build();
        bean.registerMapping(requestMappingInfo3, "adapterController", AdapterController.class.getDeclaredMethod("myTest"));

請求舉例: POST http://localhost:8070/lmcTest4

結(jié)果與2.1相同

2.5 帶參POST方法

        // 帶參post方法
        RequestMappingInfo requestMappingInfo4 = RequestMappingInfo.paths("/lmcTest5")
                .params(new String[]{"fileName", "type", "isSort"})
                .methods(RequestMethod.POST).build();
        bean.registerMapping(requestMappingInfo4, "adapterController", AdapterController.class.getDeclaredMethod("myTest3", String.class, String.class, Boolean.class));

請求舉例: POST http://localhost:8070/lmcTest5?fileName=hhh&isSort=false&type=KKK

結(jié)果與2.3相同

2.6 Body帶數(shù)據(jù)的POST方法

        // body帶參的post方法
        RequestMappingInfo requestMappingInfo5 = RequestMappingInfo.paths("/lmcTest6")
                .produces(new String[]{"text/plain;charset=UTF-8"})
                .methods(RequestMethod.POST).build();
        bean.registerMapping(requestMappingInfo5, "adapterController", AdapterController.class.getDeclaredMethod("myTest4", HttpServletRequest.class));
        System.err.println("已經(jīng)加載/lmcTest");

AdapterController.java

    Object myTest4(HttpServletRequest request) {
        byte[] body = new byte[request.getContentLength()];
        JSONObject json = null;
        try (
                ServletInputStream in = request.getInputStream();
        ) {
            in.read(body, 0, request.getContentLength());
            json = JSON.parseObject(new String(body, "UTF-8"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (Objects.isNull(json)) {
            return "fail to parse request";
        }
        return String.format("name is %s and age is %s", json.getString("name"), json.getString("age"));
    }

請求舉例:POST http://localhost:8070/lmcTest6

請求體JSON:

{
	"name":"kkk",
    "age":12
}

結(jié)果如下:

name is kkk and age is 12

三,運行時生成接口

前面介紹了幾種動態(tài)接口生成方式,下面我將介紹一下調(diào)用一個接口,來生成新接口的場景

AdapterController.java

    @GetMapping("create")
    public String create() throws NoSuchMethodException {
        RequestMappingHandlerMapping bean = applicationContext.getBean(RequestMappingHandlerMapping.class);
        // 無參get方法
        RequestMappingInfo requestMappingInfo = RequestMappingInfo.paths("/leenai").methods(RequestMethod.GET).build();
        bean.registerMapping(requestMappingInfo, "adapterController", AdapterController.class.getDeclaredMethod("myTest"));
        return "success to create and reload createRestApi()";

運行后訪問接口: http://localhost:8070/create,會生成一個新接口 http://localhost:8070/leenai

訪問結(jié)果如2.1所示

前面幾種方式都調(diào)試成功后,基本上可以自己自定義大部分的接口了。動態(tài)接口生成之后,可以存儲到數(shù)據(jù)庫中,等到下一次或者新集群實例發(fā)布時,直接就可以引用了。

這是我找到的一種動態(tài)生成接口方式,不明確有沒有更優(yōu)解。

在我的實際需求中,動態(tài)接口生成之后還要被Swagger發(fā)現(xiàn),可能這也是比較常見的使用方式,我將在下篇文章再來介紹我的處理過程。

到此這篇關(guān)于SpringBoot動態(tài)生成接口實現(xiàn)流程示例講解的文章就介紹到這了,更多相關(guān)SpringBoot動態(tài)生成接口內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring占位符Placeholder的實現(xiàn)原理解析

    Spring占位符Placeholder的實現(xiàn)原理解析

    這篇文章主要介紹了Spring占位符Placeholder的實現(xiàn)原理,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-03-03
  • 淺談Springboot實現(xiàn)攔截器的兩種方式

    淺談Springboot實現(xiàn)攔截器的兩種方式

    本文詳細的介紹了Springboot攔截器的兩種方式實現(xiàn),一種就是用攔截器,一種就是過濾器,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Java線程實現(xiàn)的兩種方式解析

    Java線程實現(xiàn)的兩種方式解析

    這篇文章主要介紹了Java線程實現(xiàn)的兩種方式解析,注意在構(gòu)造器中啟動這個線程的話,很容易造成this逃逸的問題,這是要注意的,這是通過直接集成thread來成為線程,同時在這種情況下,你可以通過調(diào)用合適的方法來,需要的朋友可以參考下
    2024-01-01
  • 新手初學(xué)Java對象內(nèi)存構(gòu)成

    新手初學(xué)Java對象內(nèi)存構(gòu)成

    這篇文章主要介紹了深入理解JVM之Java對象的創(chuàng)建、內(nèi)存布局、訪問定位,結(jié)合實例形式詳細分析了Java對象的創(chuàng)建、內(nèi)存布局、訪問定位相關(guān)概念、原理、操作技巧與注意事項,需要的朋友可以參考下
    2021-07-07
  • Spring中@Scheduled注解的參數(shù)詳解

    Spring中@Scheduled注解的參數(shù)詳解

    這篇文章主要介紹了Spring中@Scheduled注解的參數(shù)詳解,@Scheduled注解的使用這里不詳細說明,@Scheduled注解有幾個參數(shù)需要說明一下,直接對8個參數(shù)進行講解,需要的朋友可以參考下
    2023-11-11
  • Java導(dǎo)出網(wǎng)頁表格Excel過程詳解

    Java導(dǎo)出網(wǎng)頁表格Excel過程詳解

    這篇文章主要介紹了Java導(dǎo)出網(wǎng)頁表格Excel過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-07-07
  • Java深入了解數(shù)據(jù)結(jié)構(gòu)之哈希表篇

    Java深入了解數(shù)據(jù)結(jié)構(gòu)之哈希表篇

    哈希表是一種根據(jù)關(guān)鍵碼去尋找值的數(shù)據(jù)映射結(jié)構(gòu),該結(jié)構(gòu)通過把關(guān)鍵碼映射的位置去尋找存放值的地方,說起來可能感覺有點復(fù)雜,我想我舉個例子你就會明白了,最典型的的例子就是字典
    2022-01-01
  • Java使用Runnable和Callable實現(xiàn)多線程的區(qū)別詳解

    Java使用Runnable和Callable實現(xiàn)多線程的區(qū)別詳解

    這篇文章主要為大家詳細介紹了Java使用Runnable和Callable實現(xiàn)多線程的區(qū)別之處,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起了解一下
    2022-07-07
  • Java中的Object.getClass()方法解析

    Java中的Object.getClass()方法解析

    這篇文章主要介紹了Java中的Object.getClass()方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Java實現(xiàn)url加密處理的方法示例

    Java實現(xiàn)url加密處理的方法示例

    這篇文章主要介紹了Java實現(xiàn)url加密處理的方法,涉及java基于base64、編碼轉(zhuǎn)換實現(xiàn)加密解密相關(guān)操作技巧,需要的朋友可以參考下
    2017-06-06

最新評論