温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

swagger-ui 2.10.5 怎么正确的在springboot中使用

发布时间:2020-12-25 14:32:09 来源:亿速云 阅读:2404 作者:Leah 栏目:开发技术

这篇文章给大家介绍swagger-ui 2.10.5 怎么正确的在springboot中使用,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

问题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:

图片入下图所示:

swagger-ui 2.10.5 怎么正确的在springboot中使用

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

关于swagger-ui 2.10.5 怎么正确的在springboot中使用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI