[Springfox]使用笔记-01

SpringFox设定

maven dependency

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

如何读取Docket

springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper为起点
https://ithelp.ithome.com.tw/upload/images/20220310/201458562cWnN2TJRg.png

因实作SmartLifecycle会等所有的Bean准备完成,并执行实作方法start()
https://ithelp.ithome.com.tw/upload/images/20220310/20145856jI606Vr5h0.png

开始扫描Docket建立Documentation最後放入DocumentationCache
https://ithelp.ithome.com.tw/upload/images/20220310/20145856NDjB1rWRx9.png

当呼叫path/v3/api-docs,就会回传swagger的json文件,OpenApiControllerWebMvc是预设的API Controller
https://ithelp.ithome.com.tw/upload/images/20220310/20145856v7acT7CbMY.png

DocumentationCache取出Documentation处理完成後并回传,就是我们看到的json档案
https://ithelp.ithome.com.tw/upload/images/20220310/20145856QgmzQok9No.png

Docket建立

范例程序

@Bean
Docket docket() {
    return new Docket(DocumentationType.OAS_30)
                .servers(new Server("test", "http://localhost", "test",
                    Collections.EMPTY_LIST, Collections.EMPTY_LIST)) // 目前有些问题
                .groupName("API文件")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.app"))
                .paths(PathSelectors.regex("/api/hello/.*"))
                .build();
}

private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
        .title("测试api")
        .description("测试用的的api")
        .contact(new Contact("Lucas", "联络的网址", "联络的mail"))
        .version("1.0.0") // api的版本
        .build();
}

Docket里的servers没有反应 issue
目前Docket里的servers是被放到Documentation的resourceListing而非Documentation的servers,所以在controller取出Documentation放入OpenAPI的servers就没有改变
https://ithelp.ithome.com.tw/upload/images/20220310/20145856DQuVwMDDio.png
https://ithelp.ithome.com.tw/upload/images/20220310/20145856usArA5kSZN.png

暂时解决方式

建立一实作WebMvcOpenApiTransformationFilter的Bean

@Override
public OpenAPI transform(OpenApiTransformationContext<HttpServletRequest> context) {
    OpenAPI openApi = context.getSpecification();
    Server server = new Server();
    server.setDescription("test");
    server.setUrl("http://localhost");
    List<Server> servers = new ArrayList<>(openApi.getServers());
    servers.add(server);
    openApi.setServers(servers);
    return openApi;
}

@Override
public boolean supports(DocumentationType documentationType) {
    return documentationType.equals(DocumentationType.OAS_30);
}

<<:  学习使用 Node 和 React 进行全栈开发

>>:  SCSS ( @mixin @include )

Day 3 : 建立Python开发环境吧(Linux)!

现在越来越多人使用Linux系统,所以今天会来介绍一下在Linux上使用终端机安装Python的方法...

Python GUI 好用的设计观念

无论是使用Tkinter、PyQt5、PySide2、PyQt6、PySide6 都没关系,在设计观...

【Day1】下载VsCode来开启我们的前端测试不归路吧(╬•᷅д•᷄╬)

嗨各位看官们,对~又是我! 这是第二次参加铁人赛,除了要检视自己是不是有进步以外, 也想把这一年来有...

Day5 - 新鲜人提升开发效率的方法(扩充套件篇)

昨日提到的一些工具都安装好之後,开发的过程中如果要增加开发效率的话还是需要一些套件的辅助,今日的文章...

[Day 1] 为啥选择Vue.js咧???

一开始会参加铁人赛的原因是因为某堂课的要求(。・∀・)ノ゙ 既然都参加了当然就不能放弃这次的机会,藉...