亲宝软件园·资讯

展开

SpringBoot动态生成接口实现流程示例讲解

李奈 - Leemon 人气:0

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

加载全部内容

相关教程
猜你喜欢
用户评论