上篇文章 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平台接口说明文档")
.<

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

被折叠的 条评论
为什么被折叠?



