SpringClound集成Swagger

网关模块添加依赖和配置

首先在项目的添加swagger的依赖和配置

添加依赖

<dependency>
<!-- swagger2 -->
	<groupId>io.springfox</groupId>
	<artifactId>springfox-swagger2</artifactId>
	<version>2.9.2</version>
</dependency>
<!-- swagger-ui -->
<dependency>
	<groupId>io.springfox</groupId>
	<artifactId>springfox-swagger-ui</artifactId>
	<version>2.9.2</version>
</dependency>

添加配置

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo());
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("旅游app")
                .description("接口说明文档")
                //注意contact需要与提供者的配置一致
                .contact(new Contact("znq","",""))
                .version("1.0")
                .build();
    }
}

其它模块

添加依赖

步骤和网关模块的步骤是一致的

swagger配置

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.znq.users.controller"))
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("旅游app")
                .description("接口说明文档")
                //注意contact需要与提供者的配置一致
                .contact(new Contact("znq","",""))
                .version("1.0")
                .build();
    }
    @Bean
    public UiConfiguration uiConfig() {
        return UiConfigurationBuilder.builder()
                .docExpansion(DocExpansion.FULL)
                .build();
    }

}

UiConfiguration是什么?

UiConfiguration是Swagger UI的一个配置类,用于配置Swagger UI的行为和外观。
例如:

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket api() {
        // 配置Docket对象
    }

    @Bean
    public UiConfiguration uiConfig() {
        return UiConfigurationBuilder.builder()
                .docExpansion(DocExpansion.LIST)
                .defaultModelRendering(ModelRendering.EXAMPLE)
                .filter(false)
                .build();
    }
}

docExpansion:用于指定API文档的默认展开方式。可以选用DocExpansion.NONE、DocExpansion.LIST或DocExpansion.FULL三种方式。在这个例子中,我们使用DocExpansion.LIST表示默认展开为列表方式。

  1. NONE:默认不展开任何API接口,需要手动点击才能展开。
  2. LIST:默认以列表方式展开API接口,只显示接口的摘要信息。
  3. FULL:默认以完整方式展开API接口,显示接口的所有信息。

defaultModelRendering:用于指定模型渲染方式。可以选用ModelRendering.EXAMPLE或ModelRendering.MODEL两种方式。在这个例子中,我们使用ModelRendering.EXAMPLE表示默认使用模型示例渲染。
filter:用于指定是否显示过滤器。在这个例子中,我们使用false表示默认不显示过滤器。

posted @ 2023-05-05 21:55  z1n4q2  阅读(147)  评论(0)    收藏  举报