当前位置: 首页 > 工具软件 > swagger-mg-ui > 使用案例 >

SpringBoot集成Swagger2 ui自定义界面

衡泰
2023-12-01

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

 类似资料: