springboot使用swagger-ui 2.10.5 有關版本更新帶來的問題小結

問題1

常見問題

1.需要傳入後臺的為string類型 但是使用swagger-ui 接口進行測試的時候,輸入的為數字類型,建議對pom.xml文件進行調整

 <dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>${swagger.version}</version>
		</dependency>
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>${swagger.version}</version>
			<exclusions>
				<exclusion>
					<groupId>io.swagger</groupId>
					<artifactId>swagger-annotations</artifactId>
				</exclusion>
				<exclusion>
					<groupId>io.swagger</groupId>
					<artifactId>swagger-models</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>io.swagger</groupId>
			<artifactId>swagger-annotations</artifactId>
			<version>1.5.21</version>
		</dependency>
		<dependency>
			<groupId>io.swagger</groupId>
			<artifactId>swagger-models</artifactId>
			<version>1.5.21</version>
		</dependency>

將原來默認的 1.5.20 版本剔除,此時的 swagger.version 默認為 2.10.5,默認引入的為1.5.20,可以剔除再引入新的1.5.21.

2.出現如下的圖片的問題

Unable to infer base url. This is common when using dynamic servlet registration or when the API is behind an API
Gateway. The base url is the root of where all the swagger resources are served. For e.g. if the api is available at
http://example.org/api/v2/api-docs then the base url is http://example.org/api/. Please enter the location manually:

圖片入下圖所示:

在這裡插入圖片描述

Unable to infer base url. This is common when using dynamic servlet registration or when the API is behind an API
Gateway. The base url is the root of where all the swagger resources are served. For e.g. if the api is available at
http://example.org/api/v2/api-docs then the base url is http://example.org/api/. Please enter the location manually:

此時查看 pom.xml 的文件是否滿足要求, 這裡的 /api/dataStandard 路徑為後臺 yml 或者 properties 文件中的路徑,例如:

server:
 port: 18088
 servlet:
  context-path: /api/dataStandard

因swagger-ui Java使用的是 2.10.5 版本,此版本與 3.0 和 原有2.9 版本及以下的版本不同,如果你選擇使用 webflux 進行開發此時的pom.xml 文件應該引入如下配置:

<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-spring-webflux</artifactId>
			<version>2.10.5</version>
		</dependency>

同時可以在 SwaggerConfig.java 文件加上 @EnableSwagger2WebFlux 此配置,不然使用原有的 @EnableSwagger2 或者使用成 @EnableSwagger2WebMvc 會出現圖片出現的錯誤。

如果你使用的是 springboot-web 進行開發,此時應該引入 pom.xml 如下配置:

<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-spring-webmvc</artifactId>
			<version>2.10.5</version>
		</dependency>

同時可以在 SwaggerConfig.java 文件加上 @EnableSwagger2WebMvc 此配置,不然使用原有的 @EnableSwagger2 或者使用成 @EnableSwagger2WebFlux 會出現圖片出現的錯誤。

具體 SwaggerConfig.java 如題下所示:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
/**
 * @author hzp
 * @date 2020.11.05
 */
@EnableSwagger2WebMvc
@Configuration
public class SwaggerConfig {
  @Value("${swagger.enabled}")
  private Boolean enabled;

  @Bean
  @SuppressWarnings("all")
  public Docket createRestApi() {
    return new Docket(DocumentationType.SWAGGER_2)
        .enable(enabled)
        .apiInfo(apiInfo())
        .pathMapping("/")
        .select()
        .apis(RequestHandlerSelectors.basePackage("com.hzp.app.web"))
        .paths(PathSelectors.any())
        .build();
  }

  private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
        .title("sso-server 接口文檔")
        .description("後臺登錄")
        .version("1.0")
        .build();
  }

}

${swagger.enabled} 取yml中設置的是否啟用 swagger-ui 功能,如下xml:

#是否開啟 swagger-ui
swagger:
 enabled: true

以上為 springboot 采用 2.10.5 版本開發時遇到的一點問題,希望不足的地方大傢給予意見。

到此這篇關於springboot使用swagger-ui 2.10.5 有關版本更新帶來的問題小結的文章就介紹到這瞭,更多相關springboot使用swagger-ui 2版本問題內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: