我有yaml格式的openapi 3.0规范和我的应用程序,它从中生成代码。一切都很好,除了一代的招摇ui。我使用spring-fox作为它的代,但它似乎从控制器产生swagger ui 2.0版本,这是从openapi规范生成的。
如何从我的3.0规范直接生成swagger ui而不是从3.0 openapi规范生成的控制器?
好吧,我已经解决了问题(虽然解决方案非常麻烦)。
首先,我添加了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,我们可以传递spec
JSON变量而不是url。所以要初始化这个spec
变量并编辑swagger ui渲染我在maven中使用replacer插件:
<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><script></token>
<value><script src="./openapi.json"> </script><script></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");
}
}
所以就是这样。如果您对此答案进行评论并指出如何简化此过程,那将会很棒。