我有yaml格式的openapi 3.0规范,我的应用程序也由此生成代码。除了生成swagger ui之外,其他一切都正常。我使用spring-fox进行生成,但似乎它从控制器生成swagger ui 2.0版本,该控制器是根据openapi规范生成的。
如何直接从3.0规范而不是从3.0 openapi规范生成的控制器生成swagger ui?
答案 0 :(得分:0)
好了,我已经解决了这个问题(尽管解决方案很麻烦)。
首先,我添加了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中设置规范路径。根据{{3}},我们可以传递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");
}
}
就这样。如果您评论此答案并指出如何简化此过程,那就太好了。