SpringBoot动态生成接口
admin
2024-05-09 20:41:15
0

SpringBoot动态生成接口

文章目录

  • SpringBoot动态生成接口
  • 一,简单例子
  • 二,各种请求方法以及条件
    • 2.1 无参GET方法
    • 2.2 带1参的GET方法
    • 2.3 带多参的GET方法
    • 2.4 无参POST方法
    • 2.5 带参POST方法
    • 2.6 Body带数据的POST方法
  • 三,运行时生成接口

最近遇到一个需求,需要在程序运行过程中,可以动态新增接口,自定义接口参数名称,基本类型,以及请求方法,请求头等等。通过几天的研究,找到了我需要的解决方案。

对于这个需求,我首先要研究的是程序是怎么加载非@Controller/@RequestMapping等等注解下的接口,然后发现加载接口都需要被RequestMappingInfo处理,可以通过该类进行动态接口生成。

一,简单例子

首先,我要做一件最简单的事,就是在程序运行时加载一个我自定义的接口,具体代码如下:

@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* @author: Leemon* @Description: TODO* @date: 2021/12/23 10:14* @version: 1.0*/
@RestController
@Slf4j
public class AdapterController {Object myTest() {return "this is test request";}}

运行程序后,访问接口 http://localhost:8070/lmcTest,可以正常访问到接口内容,结果如下:

this is test request

二,各种请求方法以及条件

刚才的例子是一个最简单无参的get请求,但实际需求中我们的接口可能带有参数等等不同的需求。对于各种条件下的动态接口,如下所示

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方法

        // 带一参数的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

结果如下:

this is my param : hhh

2.3 带多参的GET方法

        // 带多个参数的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

结果如下:

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

结果与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

结果与2.3相同

2.6 Body带数据的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("已经加载/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
}

结果如下:

name is kkk and age is 12

三,运行时生成接口

前面介绍了几种动态接口生成方式,下面我将介绍一下调用一个接口,来生成新接口的场景

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

访问结果如2.1所示

前面几种方式都调试成功后,基本上可以自己自定义大部分的接口了。动态接口生成之后,可以存储到数据库中,等到下一次或者新集群实例发布时,直接就可以引用了。

这是我找到的一种动态生成接口方式,不明确有没有更优解。

在我的实际需求中,动态接口生成之后还要被Swagger发现,可能这也是比较常见的使用方式,我将在下篇文章再来介绍我的处理过程。

相关内容

热门资讯

6.67%!深圳研发强度跃居中... 1980年,英国《经济学人》将新设立的深圳经济特区描述为一个“昙花一现”(ephemeral)的实验...
超800亿元市值光伏巨头明起停... 2月24日晚间,超800亿元市值的光伏巨头通威股份(600438.SH)公告称,公司正在筹划通过发行...
个税年度汇算明起可预约,操作指... 一年一度的“多退少补”要开始了!国家税务总局发布消息,2月25日起,通过个人所得税App可预约202...
汉马科技董事周建群病逝,他曾主... 2月24日,汉马科技(600375)公告,公司董事周建群于2026年2月15日因病逝世。公告显示,周...
依托海南自贸港,绿地集团落地两... 依托海南自贸港政策,2月24日,绿地集团通过在海南设立的平台公司签订大宗商品贸易和新能源汽车出口两张...
荷兰籍爷爷还是中国籍爷爷?请刘... 从欧洲到广州,“爷爷的农场”来时路究竟在哪?作者|刘钦文编辑|高远山“宝宝的第一口辅食,你选什么品牌...
春节档票房缩水近四成,A股影视... 马年首个交易日,A股影视板块没能迎来“开门红”。2月24日,A股影视板块开盘即陷入调整,全天表现垫底...
银行股马年开局遇冷,机构调研透... 马年第一个交易日A股迎来开门红,银行股在市场火热情绪中继续被“冷落”,成为当天为数不多的飘绿板块之一...
最高法通报金科股份重整案:为法... 最高法披露全国首家大型房地产上市公司重整案:重庆破产法庭通过协同重整,化解房企1470亿元债务危机并...
国投白银净值“打三折”!国投资... 作者 |付影来源 | 独角金融2月23日晚间,国投资本(600061.SH)发布公告,对国投瑞银白银...