亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Java - 如何直接從 openapi 3.0 規范生成 Swagger UI

Java - 如何直接從 openapi 3.0 規范生成 Swagger UI

搖曳的薔薇 2022-10-12 10:09:26
我有 yaml 格式的 openapi 3.0 規范和從中生成代碼的應用程序。除了生成 swagger ui 外,一切正常。我使用 spring-fox 來生成它,但它似乎從控制器生成 swagger ui 2.0 版本,這些控制器是從 openapi 規范生成的。如何直接從我的 3.0 規范而不是從 3.0 openapi 規范生成的控制器生成 swagger ui?
查看完整描述

1 回答

?
翻閱古今

TA貢獻1780條經驗 獲得超5個贊

好吧,我已經解決了這個問題(雖然解決方案很麻煩)。


首先,我添加了 swagger ui webjar -


        <plugin>

            <!-- Download Swagger UI webjar. -->

            <artifactId>maven-dependency-plugin</artifactId>

            <version>${maven-dependency-plugin.version}</version>

            <executions>

                <execution>

                    <phase>prepare-package</phase>

                    <goals>

                        <goal>unpack</goal>

                    </goals>

                    <configuration>

                        <artifactItems>

                            <artifactItem>

                                <groupId>org.webjars</groupId>

                                <artifactId>swagger-ui</artifactId>

                                <version>${swagger-ui.version}</version>

                            </artifactItem>

                        </artifactItems>

                        <outputDirectory>${project.build.directory}/classes</outputDirectory>

                    </configuration>

                </execution>

            </executions>

        </plugin>

然后我將我的 yaml 規范轉換為 json 格式并將其復制到 swagger-ui webjar 目錄:


            <plugin>

                <groupId>org.openapitools</groupId>

                <artifactId>openapi-generator-maven-plugin</artifactId>

                <version>4.0.0-beta3</version>

                <executions>                    

                    <execution>

                        <id>generate-spec</id>

                        <goals>

                            <goal>generate</goal>

                        </goals>

                        <configuration>

                            <inputSpec>${openapi-spec-file-location}</inputSpec>

                            <validateSpec>true</validateSpec>

                            <generatorName>openapi</generatorName>

                            <output>${project.build.directory}/classes/META-INF/resources/webjars/swagger-ui/${swagger-ui.version}</output>

                        </configuration>

                    </execution>

                </executions>

            </plugin>

接下來我們需要在 swagger-ui 中設置規范路徑。根據swagger-ui API,我們可以傳遞specJSON 變量而不是 url。因此,要初始化此spec變量并編輯 swagger ui 渲染,我在 maven 中使用了替換插件:


            <plugin>

                <!-- Replace the OpenAPI specification example URL with the local one. -->

                <groupId>com.google.code.maven-replacer-plugin</groupId>

                <artifactId>replacer</artifactId>

                <version>1.5.3</version>

                <executions>

                    <execution>

                        <phase>prepare-package</phase>

                        <goals>

                            <goal>replace</goal>

                        </goals>

                    </execution>

                </executions>

                <configuration>

                    <includes>

                        <!-- Static index html with swagger UI rendering and OAS in JSON format. -->

                        <include>${project.build.directory}/classes/META-INF/resources/webjars/swagger-ui/${swagger-ui.version}/index.html</include>

                        <include>${project.build.directory}/classes/META-INF/resources/webjars/swagger-ui/${swagger-ui.version}/openapi.json</include>

                    </includes>

                    <regexFlags>

                        <regexFlag>CASE_INSENSITIVE</regexFlag>

                        <regexFlag>MULTILINE</regexFlag>

                    </regexFlags>

                    <replacements>

                        <!-- This replacement imports spec json variable into static html page. -->

                        <replacement>

                            <token>&lt;script&gt;</token>

                            <value>&lt;script src="./openapi.json"&gt; &lt;/script&gt;&lt;script&gt;</value>

                        </replacement>

                        <!-- This part replaces url input variable with spec variable. -->

                        <replacement>

                            <token>url:\s"https:\/\/petstore\.swagger\.io\/v2\/swagger\.json"</token>

                            <value>spec: spec</value>

                        </replacement>

                        <replacement>

                        <!-- This replacement initializes spec variable, that will be passed to swagger ui index.html. -->

                            <token>^\{</token>

                            <value>spec = {</value>

                        </replacement>

                    </replacements>

                </configuration>

            </plugin>

所以在構建后的這一步,我們得到了帶有 swagger ui 的靜態資源。最后要做的就是用 Spring 來服務它。


@Configuration

@EnableWebMvc

public class SwaggerConfiguration implements WebMvcConfigurer {


    @Override

    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/**")

                .addResourceLocations("classpath:/META-INF/resources/webjars/swagger-ui/3.22.0/");

    }


    //this method was introduced just for convenient swagger ui access. Without it swagger ui can be accessed with /index.html GET call   

    @Override

    public void addViewControllers(ViewControllerRegistry registry) {

        registry.addViewController("/swagger-ui.html").setViewName("forward:/index.html");

    }

}

所以就是這樣。如果您評論此答案并指出如何簡化此過程,那就太好了。


查看完整回答
反對 回復 2022-10-12
  • 1 回答
  • 0 關注
  • 184 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號