多module项目的springboot配置swagger

场景

项目使用swagger提供接口文档功能, 下面演示下多module的springboot 项目是怎么配置

效果

在这里插入图片描述

分析

  1. 通过为每个module设置独立的分组,完成分组切换功能, 下面举个例子
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket group1Api() {
        return new Docket(DocumentationType.SWAGGER_2)
            .groupName("Group1")
            .apiInfo(apiInfo())
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.example.group1"))
            .paths(PathSelectors.any())
            .build();
    }

    @Bean
    public Docket group2Api() {
        return new Docket(DocumentationType.SWAGGER_2)
            .groupName("Group2")
            .apiInfo(apiInfo())
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.example.group2"))
            .paths(PathSelectors.any())
            .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
            .title("Your API Title")
            .description("Your API Description")
            .version("1.0.0")
            .build();
    }
}

  1. 项目使用了spring security 所以需要有设置Authorization Header的位置,这可以通过2种方式实现
    2.1 Docket.securitySchemes方法,入参是ApiKey列表
    2.2 通过编辑"文档管理 -> 全局参数设置完成"
    在这里插入图片描述

实现

  1. 引入依赖
        <!-- 1. swagger-bootstrap-ui 目前改名为 knife4j -->
        <!-- 2. 实现 swagger-bootstrap-ui 的自动化配置  -->
        <!-- 3. 因为 knife4j-spring 已经引入 Swagger 依赖,所以无需重复引入 -->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-ui</artifactId>
        </dependency>
  1. swagger配置

/**
 * @version V1.0
 * @author: carsonlius
 * @date: 2023/12/28 10:07
 * @company
 * @description swagger配置
 */
@AutoConfiguration
@ConditionalOnProperty(prefix = "springdoc.api-docs", name = "enabled", havingValue = "true", matchIfMissing = true)
@EnableConfigurationProperties(SwaggerProperties.class)
@EnableSwagger2
public class DemoSwaggerAutoConfiguration {

    /**
     * 设置system分组
     * */
    @Bean
    public Docket createSystemApi(SwaggerProperties properties) {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("system")
                // 设置api信息
                .apiInfo(apiInfo(properties))
                // 扫描;controller包路径, 获取api接口
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.carsonlius.module.system"))
                .paths(PathSelectors.any())
                // 构建Docket对象
                .build()
                .securitySchemes(Collections.singletonList(apiKey()));
    }

    /**
     * 设置system分组
     * */
    @Bean
    public Docket createWebApi(SwaggerProperties properties) {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("web")
                // 设置api信息
                .apiInfo(apiInfo(properties))
                // 扫描;controller包路径, 获取api接口
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.carsonlius.framework.web"))
                .paths(PathSelectors.any())
                // 构建Docket对象
                .build()
                .securitySchemes(Collections.singletonList(apiKey()));
    }


    /**
     * 设置请求头
     * */
    private springfox.documentation.service.ApiKey apiKey() {
        return new springfox.documentation.service.ApiKey("authorization", "Authorization", "header");
    }

    /**
     * 创建API信息
     * */
    public ApiInfo apiInfo(SwaggerProperties swaggerProperties) {
        return new ApiInfoBuilder().title(swaggerProperties.getTitle())
                .description(swaggerProperties.getDescription())
                .version(swaggerProperties.getVersion())
                .contact(new Contact(swaggerProperties.getAuthor(), swaggerProperties.getUrl(), swaggerProperties.getEmail()))
                .build();
    }
}

  1. security配置

对swagger请求放行

          .antMatchers("/swagger-ui.html", "/webjars/**", "/v2/api-docs", "/swagger-resources/**").permitAll()
        // 设置具体请求的权限
        httpSecurity.authorizeRequests()
                .antMatchers(HttpMethod.GET, "/*.html", "/**/*.html", "/**/*.css", "/**/*.js").permitAll() // 静态资源无需认证
                .antMatchers("/websocket/message").permitAll() // websocket无需认证
                .antMatchers("/swagger-ui.html", "/webjars/**", "/v2/api-docs", "/swagger-resources/**").permitAll()
                // 放行 Druid 监控的相关 URI
                .antMatchers("/druid/**").permitAll()
                .antMatchers(HttpMethod.GET, permitAllUrlMap.get(RequestMethod.GET).toArray(new String[0])).permitAll()
                .antMatchers(HttpMethod.POST, permitAllUrlMap.get(RequestMethod.POST).toArray(new String[0])).permitAll()
                .antMatchers(HttpMethod.PUT, permitAllUrlMap.get(RequestMethod.PUT).toArray(new String[0])).permitAll()
                .antMatchers(HttpMethod.DELETE, permitAllUrlMap.get(RequestMethod.DELETE).toArray(new String[0])).permitAll()
                .and().authorizeRequests().anyRequest().authenticated(); // 其他请求必须认证

@ConfigurationProperties 注解

SwaggerProperties 通过@ConfigurationProperties将配置属性影射到ConfigurationProperties对象。 这个注解一般配合@EnableConfigurationProperties使用,或者直接在ConfigurationProperties上加上@Component

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: SpringBoot 配置Swagger 可以参考以下步骤:1. 在pom.xml中添加Swagger2依赖;2. 在SpringBoot启动类中添加@EnableSwagger2注解;3. 创建配置Swagger2;4. 在配置类中配置Swagger2信息;5. 启动SpringBoot项目,访问Swagger2页面。 ### 回答2: Spring Boot是一个基于Java的快速开发框架,可以简化Java应用的搭建和开发过程。Swagger是一个RESTful API文档生成工具,它可以通过注解来生成API文档,并且提供了一个可交互的UI界面,方便开发人员查看和测试API接口。 要在Spring Boot项目配置Swagger,需要进行以下步骤: 1. 导入Swagger依赖:在项目的pom.xml文件中添加Swagger的依赖项,通常是通过引入`springfox-swagger2`和`springfox-swagger-ui`这两个依赖来实现。 2. 创建Swagger配置类:在项目中创建一个Swagger配置类,用于配置Swagger的相关信息和接口扫描规则。可以在配置类中使用`@EnableSwagger2`注解来启用Swagger。 3. 配置Swagger的基本信息:在配置类中使用`Docket`类的实例来配置Swagger的基本信息,比如Swagger接口文档的版本、标题、描述、联系人等。可以通过`apiInfo`方法来设置这些信息。 4. 配置接口扫描规则:在配置类中使用`select`方法来设置Swagger要扫描的接口规则,比如指定要扫描的包路径,或者使用`RequestHandlerSelectors`类提供的方法来选择要包含的接口。 5. 配置Swagger UI界面:在配置类中使用`apis`方法来设置Swagger要显示的接口,可以选择显示所有接口,或者只显示带有特定注解的接口。可以使用`PathSelectors`类提供的方法来选择要显示的接口。 6. 启动项目并访问Swagger UI:完成上述配置后,启动Spring Boot项目,并访问Swagger UI界面,通常是通过在浏览器中访问`http://localhost:port/swagger-ui.html`来查看API文档。 以上就是在Spring Boot项目配置Swagger的基本步骤。配置完毕后,开发人员就可以方便地查看和测试API接口,提高开发效率。 ### 回答3: Spring Boot是一个开源的Java开发框架,可以简化Spring应用程序的配置和开发过程。Swagger是一个用于构建、文档化和调试RESTful API的工具。在Spring Boot项目配置Swagger可以使得我们更方便地管理和测试API接口。 要配置Swagger,我们需要在Spring Boot项目的pom.xml文件中添加Swagger的依赖。可以通过以下代码完成添加: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> ``` 添加依赖后,我们需要创建一个配置类来配置Swagger。可以创建一个名为SwaggerConfig的类,并通过@Configuration注解将其标记为配置类。在SwaggerConfig类中,我们需要使用@EnableSwagger2注解启用Swagger,并创建一个Docket bean来配置Swagger的一些参数。 以下是一个示例的Swagger配置类代码: ```java @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.example.api")) .paths(PathSelectors.any()) .build(); } } ``` 在上述代码中,我们通过RequestHandlerSelectors.basePackage()方法指定了API接口所在的包路径,通过PathSelectors.any()方法指定了所有路径都被扫描。这样配置后,Swagger就可以扫描到我们想要展示的API接口。 最后,在启动类中添加@EnableSwagger2注解,启用Swagger。然后运行项目,访问Swagger UI界面(默认路径为:http://localhost:8080/swagger-ui.html),就可以看到API接口的文档、测试和调试信息了。 通过以上步骤,我们就成功地配置SwaggerSpring Boot项目中。使用Swagger可以方便地对API进行管理和测试,提高了开发效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值