四、实战 - Swagger 2 升至 3.0 (Open API 3.0)
Stage 4:获取 json,保存为 swagger.json
Stage 5:showdoc 中导入 swagger.json
公司需要将 swagger-ui 中的接口导入到 showdoc 中
思路 :获取 swagger.json 并导入到 showdoc 中 (http://ip:port/v2/api-docs 获取json)
Spring boot : 2.5.4
Swagger ui : 2.9.2
JDK : 1.8
http://ip:port/v2/api-docs 报错无法获取完整的JSON
方案一:升至Swagger 3,获取json,保存为xxx.json,再导入 showdoc中 (适合全量导入)
方案二:集成 knife4j ,单个接口json,保存为xxx.json,再导入 showdoc中(适合部分导入 + 前端调试)
-
- <dependency>
- <groupId>io.springfoxgroupId>
- <artifactId>springfox-boot-starterartifactId>
- <version>3.0.0version>
- dependency>
注意: .title("XPH- iot 接口文档")
-
-
- import io.swagger.annotations.ApiOperation;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
- import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
- import springfox.documentation.builders.ApiInfoBuilder;
- import springfox.documentation.builders.PathSelectors;
- import springfox.documentation.builders.RequestHandlerSelectors;
- import springfox.documentation.service.ApiInfo;
- import springfox.documentation.service.Contact;
- import springfox.documentation.spi.DocumentationType;
- import springfox.documentation.spring.web.plugins.Docket;
-
- // 自定义swagger3文档信息
- @Configuration
- @ConditionalOnProperty(value = "springfox.documentation.enabled", havingValue = "true", matchIfMissing = true)
- @Slf4j
- public class Swagger3Config extends WebMvcConfigurationSupport {
-
- @Value(value = "${host:localhost}")
- private String host;
-
- @Value("${server.port:8005}")
- private String port;
-
- @Bean
- public Docket createRestApi() {
- return new Docket(DocumentationType.OAS_30)
- .apiInfo(apiInfo())
- .select()
- .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
- .paths(PathSelectors.any())
- .build();
- }
-
- private ApiInfo apiInfo() {
- log.info("http://{}:{}/swagger-ui.html", host, port);
- log.info("http://localhost:{}/swagger-ui.html", port);
- log.info("http://localhost:{}/swagger-ui/index.html", port);
- log.info("http://localhost:{}/doc.html", port);
- return new ApiInfoBuilder()
- .title("XPH- iot 接口文档")
- .description("更多请咨询")
- .contact(new Contact("lijiong", "https://", "xxx@"))
- .version("1.0.0")
- .build();
- }
-
- @Override
- public void addResourceHandlers(ResourceHandlerRegistry registry) {
- registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
- //swagger 2
- registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
- //swagger 3
- registry.addResourceHandler("/swagger-ui/**").addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/");
- registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
- registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
- super.addResourceHandlers(registry);
- }
- }
全量导入,会自动生成一个项目名为:swaggerUI title的项目