SpringBoot集成Swagger2 ui自定义界面

1.导入swagger相应的依赖

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
  • ui的依赖 因为第三方的swagger-ui的访问页面是和官方不同的,所以是可以共存的,下面是3个第三方开发的swagger-ui页面(一般用bootstrap的 看起来清爽)
<!-- 引入swagger-bootstrap-ui包 /doc.html-->
   <dependency>
       <groupId>com.github.xiaoymin</groupId>
       <artifactId>swagger-bootstrap-ui</artifactId>
       <version>1.9.1</version>
   </dependency>
   <!-- 引入swagger-ui-layer包 /docs.html-->
   <dependency>
       <groupId>com.github.caspar-chen</groupId>
       <artifactId>swagger-ui-layer</artifactId>
       <version>1.1.3</version>
   </dependency>
   <!-- 引入swagger-ui-layer包 /document.html-->
   <dependency>
       <groupId>com.zyplayer</groupId>
       <artifactId>swagger-mg-ui</artifactId>
       <version>1.0.6</version>
   </dependency>
  • 解决在访问swagger-ui页面但在后端控制台出现异常错误信息的问题
 <!--增加两个配置-->
   <dependency>
       <groupId>io.swagger</groupId>
       <artifactId>swagger-annotations</artifactId>
       <version>1.5.22</version>
   </dependency>
   <dependency>
       <groupId>io.swagger</groupId>
       <artifactId>swagger-models</artifactId>
       <version>1.5.22</version>
   </dependency>

2.处理swagger2的版本问题
Failed to start bean ‘documentationPluginsBootstrapper

  • 方式一:配置文件中处理
spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher
  • 方式二:配置类中添加@EnableWebMvc注解
import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
@EnableWebMvc
public class SwaggerConfig {

}
  • 方式三:降低Spring Boot版本号至2.6.0以下
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
  • 方式四:添加其他依赖
        <!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>25.1-jre</version>
        </dependency>

3.创建swagger2 的配置类

import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

}

4.直接启动项目 测试swagger的地址即可

http://localhost:8080/swagger-ui.html
bootstrap-ui访问地址:http://ip地址:端口号/doc.html
layer-ui访问地址:http://ip地址:端口号/docs.html
mg-ui访问地址:http://ip地址:端口号/document.html

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值