springboot 集成 swagger-bootstrap-ui

springboot 集成 swagger-bootstrap-ui

  • 添加依赖 【springboot 使用的的版本是 2.6.2】
     <dependencies>
	        <dependency>
	            <groupId>org.springframework.boot</groupId>
	            <artifactId>spring-boot-starter-web</artifactId>
	        </dependency>
	        <dependency>
	            <groupId>org.springframework.boot</groupId>
	            <artifactId>spring-boot-starter-test</artifactId>
	            <scope>test</scope>
	        </dependency>
	        <dependency>
	            <groupId>io.springfox</groupId>
	            <artifactId>springfox-swagger2</artifactId>
	            <version>2.9.2</version>
	        </dependency>
	        <dependency>
	            <groupId>com.github.xiaoymin</groupId>
	            <artifactId>swagger-bootstrap-ui</artifactId>
	            <version>1.9.6</version>
	        </dependency>
	    </dependencies>
  • 配置类
/**
 * @author lc
 * @version 1.0.0
 * @ClassName SwaggerConfig.java
 * @Description swagger 配置
 * @createTime 2022年01月07日 11:06:00
 */

@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(Predicates.or(RequestHandlerSelectors.withClassAnnotation(RestController.class),
                        RequestHandlerSelectors.withClassAnnotation(Controller.class)))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("swagger-bootstrap-ui api")
                .description("swagger-bootstrap-ui")
                .termsOfServiceUrl("http://localhost:8080/")
                .version("1.0.0")
                .build();
    }
}

  • 最后在配置文件中添加以下配置信息
  spring.mvc.pathmatch.matching-strategy=ant_path_matcher

可以在 github 中拉取完整demo

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot项目中集成Swagger UI非常简单。以下是一些基本的步骤: 1. 添加Swagger依赖:在项目的`pom.xml`文件中添加Swagger的相关依赖。可以使用以下依赖: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency> ``` 2. 创建Swagger配置类:创建一个Java类,用于配置Swagger。可以使用以下示例代码: ```java 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.EnableSwagger2; @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket apiDocket() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.example.controller")) .paths(PathSelectors.any()) .build() .apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("API Documentation") .description("API Documentation for My Awesome App") .version("1.0.0") .build(); } } ``` 在上述代码中,`@EnableSwagger2`注解启用Swagger,`@Bean`注解创建一个`Docket`实例,配置Swagger的基本信息和扫描的包路径。 3. 启动应用程序:运行Spring Boot应用程序,Swagger UI将在`http://localhost:8080/swagger-ui.html`上自动启动。 现在,您可以在Swagger UI中浏览和测试您的API。Swagger UI将根据您的代码和注释生成API文档,并提供一个交互式的界面,供用户查看和测试API的各个端点和参数。 请注意,上述示例代码中的包路径和其他配置可能需要根据您的项目结构进行调整。此外,还可以通过其他配置选项来自定义Swagger UI的行为和外观。更多详细信息,请参阅Swagger和Springfox的官方文档。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值