SpringBoot动态生成接口

导读:本篇文章讲解 SpringBoot动态生成接口,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

SpringBoot动态生成接口

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

对于这个需求,我首先要研究的是程序是怎么加载非@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发现,可能这也是比较常见的使用方式,我将在下篇文章再来介绍我的处理过程。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/81583.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!