当前位置:   article > 正文

Spring Boot整合swagger-bootstrap-ui_swagger-bootstrap-ui访问地址

swagger-bootstrap-ui访问地址

概述

swagger-bootstrap-ui是基于swagger接口api实现的一套UI,因swagger原生ui是上下结构的,在浏览接口时不是很清晰,所以,swagger-bootstrap-ui是基于左右菜单风格的方式,适用与我们在开发后台系统左右结构这种风格类似,方便与接口浏览

这个依赖和官方的依赖的区别
1.访问地址的不同
官方:http://localhost:8763/swagger-ui.html#/
bootstrap-ui访问地址: http://localhost:8080/doc.html#/
2.界面展示不同
官方: 竖向展示参数
bootstrap-ui: 是横向展示参数
3.启动类添加注解的不同
官方: 需要添加@EnableSwagger2
 bootstrap-ui: 不需要添加@EnableSwagger2
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
那如何得知bootstrap-ui访问地址是doc.html
通过swagger-bootstrap-ui 下载源码.解压-
可以查看到doc.html 从而得知访问路径
  • 1
  • 2

在这里插入图片描述

添加依赖

1.swagger依赖

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

2…swagger-bootstrap-ui的依赖

<dependency>
  <groupId>com.github.xiaoymin</groupId>
  <artifactId>swagger-bootstrap-ui</artifactId>
  <version>1.9.6</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

增加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;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @ClassName: SwaggerConfig
 * @version: 1.0
 * @description: swagger-config类
 * @create: 2020-08-06 14:34
 *  *  通过@Configuration注解,让Spring来加载该类配置。
 *  *  通过@EnableSwagger2注解来启用Swagger2。
 **/
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
 // 要扫描的API(Controller)基础包          .apis(RequestHandlerSelectors.basePackage("com.ion.web.controller"))
                // 要扫描的API(Controller)全局-(就是可以扫描所有的带有api注解的包并生成文档)
               // .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
    //配置在线文档的基本信息
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("springboot利用swagger构建api文档")
                .description("简单优雅的restfun风格,https://me.csdn.net/blog/miachen520")
                .termsOfServiceUrl("https://me.csdn.net/blog/miachen520")
                .version("1.0")
                .build();
    }




}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
启动项目并访问

在这里插入图片描述

如果觉得界面不喜欢 可以添加如何依赖 美化swagger的界面

美化swagger

添加依赖

1.使用knife4j的引用配置-(美化swagger界面)

<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-spring-boot-starter</artifactId>
    <!--在引用时请在maven中央仓库搜索最新版本号-->
    <version>2.0.4</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
启动类添加注解
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
  • 1
  • 2
访问地址
http://localhost:8080/doc.html#/home
  • 1

在这里插入图片描述

官方文档

官方文档

maven地址

maven地址

排除静态文件配置

排除静态文件
registry.addResourceHandler("swagger-ui.html")
        .addResourceLocations("classpath:/META-INF/resources/");

registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");

registry.addResourceHandler("/webjars/**")
        .addResourceLocations("classpath:/META-INF/resources/webjars/");
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/856944
推荐阅读
  

闽ICP备14008679号