首页 > 其他分享 >SpringBoot动态生成接口

SpringBoot动态生成接口

时间:2023-02-08 09:46:05浏览次数:57  
标签:String 接口 生成 RequestMappingInfo AdapterController fileName class SpringBoot

原文链接:https://blog.csdn.net/lmchhh/article/details/128634606

文章目录
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,可以正常访问到接口内容,结果如下:

 

 

 

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

刚才的例子是一个最简单无参的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

结果如下:

 

 

 

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

结果如下:

 

 

 

请求举例: POST http://localhost:8070/lmcTest4

结果与2.1相同

        // 带参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));

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
}

结果如下:

 

 

三,运行时生成接口

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

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发现,可能这也是比较常见的使用方式,我将在下篇文章再来介绍我的处理过程。
————————————————
版权声明:本文为CSDN博主「李奈 - Leemon」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/lmchhh/article/details/128634606

 

标签:String,接口,生成,RequestMappingInfo,AdapterController,fileName,class,SpringBoot
From: https://www.cnblogs.com/tuxiaoer/p/17100570.html

相关文章

  • odoo接口
     @http.route('/zimo_lunch/select/<string:db_name>',type='http',auth='none')deftrash_demo(self,db_name=None):print('db_name',db_name)db=reques......
  • SpringBoot实现电子文件签字+合同系统
    本文已经收录到Github仓库,该仓库包含计算机基础、Java基础、多线程、JVM、数据库、Redis、Spring、Mybatis、SpringMVC、SpringBoot、分布式、微服务、设计模式、架构、校......
  • springboot3 security 从始至终--02 PasswordEncoder
    接下来几个章节,将逐个介绍身份验证流程中主要接口接口描述UserDetails代表SpringSecurity所看到的用户GrantedAuthority定义应用程序目的范围内允许用户执......
  • @EnableDiscoveryClient和@EnableEurekaClient springboot3.x
    @EnableDiscoveryClient和@EnableEurekaClient将一个微服务注册到EurekaServer(或其他服务发现组件,例如Zookeeper、Consul等)的步骤1、添加客户端依赖 2、写注解注意......
  • SpringBoot集成mybatis
    SpringBoot创建项目首先创建好项目,详情见:https://www.cnblogs.com/expiator/p/15844275.htmlSpringBoot引入mybatis依赖<dependency><groupId>org......
  • Springboot2前言day0
    一》Springboot是整合spring技术栈的一站式框架,简化Spring技术栈的快速开发脚手架二》Springboot优点【创建独立spring应用,内嵌web服务器,自动starter依赖简化构建配置,自......
  • springboot运维
    先讲一下这个springboot项目在别的环境启动时(Windows的cmd),一些临时属性的设置,使用--server.xx=....,    然后还有开发环境中的一个参数,args,这是一个可以设置一些属......
  • 002_springboot项目中 pom.xml 配置的作用
    parent:用以定义一系列的常用坐标版本;定义一系列的常用坐标组合;比如在pom.xml文件中引入一个javax.servlet,<version>那里是可以不写的,也就是不写版本,而决定采用哪个版本......
  • 系统 随机生成一个4位数的纯数字验证码 对验证码+5 求余 做加密运算 反序列输出
    packagecom.fqs.demo;importjava.util.Random;publicclassSjWeiShu{publicstaticvoidmain(String[]args){//1237777777777777777=number......
  • 001_从零搭建 springboot
    ①打开IDEA,点击左上角文件→新建→项目  ②点击SpringInitializr(注意右侧区域第一行项目SDK,这里要正确选择,个人推荐1.8)  ③GAV:这决定着此项目的工程坐标......