SpringBoot動(dòng)態(tài)生成接口實(shí)現(xiàn)流程示例講解
對(duì)于這個(gè)需求,我首先要研究的是程序是怎么加載非@Controller/@RequestMapping等等注解下的接口,然后發(fā)現(xiàn)加載接口都需要被RequestMappingInfo處理,可以通過(guò)該類(lèi)進(jìn)行動(dòng)態(tài)接口生成。
一,簡(jiǎn)單例子
首先,我要做一件最簡(jiǎn)單的事,就是在程序運(yùn)行時(shí)加載一個(gè)我自定義的接口,具體代碼如下:
@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";
}
}運(yùn)行程序后,訪問(wèn)接口 http://localhost:8070/lmcTest,可以正常訪問(wèn)到接口內(nèi)容,結(jié)果如下:
this is test request
二,各種請(qǐng)求方法以及條件
剛才的例子是一個(gè)最簡(jiǎn)單無(wú)參的get請(qǐng)求,但實(shí)際需求中我們的接口可能帶有參數(shù)等等不同的需求。對(duì)于各種條件下的動(dòng)態(tài)接口,如下所示
2.1 無(wú)參GET方法
// 無(wú)參get方法
RequestMappingInfo requestMappingInfo = RequestMappingInfo.paths("/lmcTest").methods(RequestMethod.GET).build();
bean.registerMapping(requestMappingInfo, "adapterController", AdapterController.class.getDeclaredMethod("myTest"));
請(qǐng)求舉例: 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;
}
請(qǐng)求舉例:http://localhost:8070/lmcTest2?fileName=hhh
結(jié)果如下:
this is my param : hhh
2.3 帶多參的GET方法
// 帶多個(gè)參數(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();
}
請(qǐng)求舉例:http://localhost:8070/lmcTest3?fileName=hhh&isSort=false&type=KKK
結(jié)果如下:
values : {"isSort":false,"fileName":"hhh","type":"KKK"}
2.4 無(wú)參POST方法
// 無(wú)參post方法
RequestMappingInfo requestMappingInfo3 = RequestMappingInfo.paths("/lmcTest4").methods(RequestMethod.POST).build();
bean.registerMapping(requestMappingInfo3, "adapterController", AdapterController.class.getDeclaredMethod("myTest"));
請(qǐng)求舉例: 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));
請(qǐng)求舉例: 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"));
}
請(qǐng)求舉例:POST http://localhost:8070/lmcTest6
請(qǐng)求體JSON:
{
"name":"kkk",
"age":12
}
結(jié)果如下:
name is kkk and age is 12
三,運(yùn)行時(shí)生成接口
前面介紹了幾種動(dòng)態(tài)接口生成方式,下面我將介紹一下調(diào)用一個(gè)接口,來(lái)生成新接口的場(chǎng)景
AdapterController.java
@GetMapping("create")
public String create() throws NoSuchMethodException {
RequestMappingHandlerMapping bean = applicationContext.getBean(RequestMappingHandlerMapping.class);
// 無(wú)參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()";
運(yùn)行后訪問(wèn)接口: http://localhost:8070/create,會(huì)生成一個(gè)新接口 http://localhost:8070/leenai
訪問(wèn)結(jié)果如2.1所示
前面幾種方式都調(diào)試成功后,基本上可以自己自定義大部分的接口了。動(dòng)態(tài)接口生成之后,可以存儲(chǔ)到數(shù)據(jù)庫(kù)中,等到下一次或者新集群實(shí)例發(fā)布時(shí),直接就可以引用了。
這是我找到的一種動(dòng)態(tài)生成接口方式,不明確有沒(méi)有更優(yōu)解。
在我的實(shí)際需求中,動(dòng)態(tài)接口生成之后還要被Swagger發(fā)現(xiàn),可能這也是比較常見(jiàn)的使用方式,我將在下篇文章再來(lái)介紹我的處理過(guò)程。
到此這篇關(guān)于SpringBoot動(dòng)態(tài)生成接口實(shí)現(xiàn)流程示例講解的文章就介紹到這了,更多相關(guān)SpringBoot動(dòng)態(tài)生成接口內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java捕獲ThreadPoolExecutor內(nèi)部線程異常的四種方法
這篇文章主要為大家詳細(xì)介紹了Java捕獲ThreadPoolExecutor內(nèi)部線程異常的四種方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下2025-03-03
Spring MVC之WebApplicationContext_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Spring MVC之WebApplicationContext的相關(guān)資料,需要的朋友可以參考下2017-08-08
java實(shí)現(xiàn)合并2個(gè)文件中的內(nèi)容到新文件中
這篇文章主要介紹了java實(shí)現(xiàn)合并2個(gè)文件中的內(nèi)容到新文件中,思路非常不錯(cuò),這里推薦給大家。2015-03-03
聊聊SpringCloud和SpringCloudAlibaba的區(qū)別
這篇文章主要介紹了SpringCloud和SpringCloudAlibaba的區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
SpringBoot實(shí)現(xiàn)HTTP調(diào)用的七種方式總結(jié)
小編在工作中,遇到一些需要調(diào)用三方接口的任務(wù),就需要用到 HTTP 調(diào)用工具,這里,我總結(jié)了一下 實(shí)現(xiàn) HTTP 調(diào)用的方式,共有 7 種(后續(xù)會(huì)繼續(xù)新增),需要的朋友可以參考下2023-09-09
java編程數(shù)據(jù)類(lèi)型全面詳解教程新手必入
這篇文章主要為大家介紹了java編程數(shù)據(jù)類(lèi)型全面詳解教程,強(qiáng)烈推薦新手入,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-10-10
Java新特性之Nashorn_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Java新特性之Nashorn的相關(guān)資料,需要的朋友可以參考下2017-06-06
Java中DateTimeFormatter的使用方法和案例
在Java中,DateTimeFormatter類(lèi)用于格式化和解析日期時(shí)間對(duì)象,它是日期時(shí)間格式化的強(qiáng)大而靈活的工具,本文將和大家一起探討Java中DateTimeFormatter的使用方法和案例,需要的朋友可以參考下2023-10-10

