SpringBoot项目运行时动态生成接口被Swagger发现

文章介绍了如何在SpringBoot应用中动态生成接口并使其能被Swagger发现。首先,通过在启动时加载接口到映射处理器中,然后调整Swagger配置的加载顺序,确保Swagger配置在接口加载之后。此外,还探讨了运行时加载接口的复杂性,通过重写ServiceModelToSwagger2MapperImpl类来实现实时更新Swagger文档。虽然这种方法可行,但作者认为它不够友好且需要大量额外工作。

上篇文章 https://blog.youkuaiyun.com/lmchhh/article/details/128634606?spm=1001.2014.3001.5502 我讲了SpringBoot动态生成接口,接下来要处理的就是新生成的接口如何被Swagger发现,并且可以通过/swagger-ui.html和/v2/api-docs查到

一,启动时加载

第一种最简单的方式就是在SpringBoot项目启动时加载,就像在上篇文章中,我动态创建接口的接口基本上都是在main函数中添加

@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);
        // 无参get方法
        RequestMappingInfo requestMappingInfo = RequestMappingInfo.paths("/lmcTest").methods(RequestMethod.GET).build();
        bean.registerMapping(requestMappingInfo, "adapterController", AdapterController.class.getDeclaredMethod("myTest"));
        // 带一参数的get方法
        RequestMappingInfo requestMappingInfo1 = RequestMappingInfo.paths("/lmcTest2").params(new String[]{
   
   "fileName"}).methods(RequestMethod.GET).build();
        bean.registerMapping(requestMappingInfo1, "adapterController", AdapterController.class.getDeclaredMethod("myTest2", String.class));
        // 带多个参数的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));
        // 无参post方法
        RequestMappingInfo requestMappingInfo3 = RequestMappingInfo.paths("/lmcTest4").methods(RequestMethod.POST).build();
        bean.registerMapping(requestMappingInfo3, "adapterController", AdapterController.class.getDeclaredMethod("myTest"));
        // 带参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));
        // 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");

    }

}

然后配置Swagger

@Configuration
@EnableSwagger2
@Slf4j
public class Swagger2Config {
   
   

    @Bean
    public Docket createRestApi() {
   
   
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.foxconn.serviceapi.controller")).build();
    }


    private ApiInfo apiInfo() {
   
   
        System.err.println("开始配置Swagger");
        log.info("开始配置Swagger");
        return new ApiInfoBuilder()
                .title("superFA说明文档")
                .description("API平台接口说明文档")
                .<
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值