springboot2.2.6集成swagger-ui(以及使用bootstrap-ui)

1、在pom文件中加入以下依赖

		<!-- swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${swagger-ui.version}</version>
        </dependency>
        <!-- swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${swagger2.version}</version>
        </dependency>
        <!-- 引入swagger-bootstrap-ui包 -->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>${swagger-bootstrap-ui.version}</version>
        </dependency>

这里我用的各依赖版本如下:

 		<swagger-ui.version>2.9.2</swagger-ui.version>
        <swagger2.version>2.9.2</swagger2.version>
        <swagger-bootstrap-ui.version>1.8.5</swagger-bootstrap-ui.version>

2、加入SwaggerConfig配置类

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;

/**
 * SwaggerConfig
 * @desc: Swagger配置
 * @version:
 * @createTime: 2020/4/23 11:08
 * @author: 
 */
@Configuration
public class SwaggerConfig {

    @Bean
    public Docket createH5RestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("智慧生态api文档")
                .description("restful 风格接口")
                .version("1.0")
                .build();
    }

}

3、启动类上加上@EnableSwagger2、以及@EnableSwaggerBootstrapUI注解

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * SmartEcologyApplication
 * @desc: TODO 类的设计目的、功能及注意事项
 * @version:
 * @createTime: 2020/4/22 16:16
 * @author: 
 */
@EnableSwaggerBootstrapUI
@EnableSwagger2
@SpringBootApplication
public class SmartEcologyApplication {

    public static void main(String[] args) {
        SpringApplication.run(SmartEcologyApplication.class, args);
    }

}

4、浏览器输入http://127.0.0.1:8080/doc.html即可查看bootstrap风格的swagger界面

springboot2.2.6集成swagger-ui(以及使用bootstrap-ui)_第1张图片

你可能感兴趣的:(Springboot)