SpringBoot集成Swagger2 ui自定义界面

本文介绍了如何在SpringBoot项目中集成Swagger2并使用自定义UI,包括导入第三方Swagger-UI依赖以实现清爽界面,处理Swagger2版本问题的多种方法,以及创建Swagger2配置类。通过设置,可以访问不同风格的Swagger UI,如bootstrap-ui、layer-ui和mg-ui。
摘要由CSDN通过智能技术生成

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: Spring Boot 是一个用于快速构建Java应用程序的框架,而Swagger UI 是一个用于展示和测试RESTful API的工具。 当我们在使用Spring Boot集成Swagger UI时,可能会遇到404错误。这种错误通常是由于Swagger UI配置不正确或缺少必要的依赖导致的。 要解决这个问题,首先确保在项目的pom.xml文件中添加了swagger相关的依赖,如 springfox-boot-starter 和 springfox-swagger-ui。 接下来,确保在Spring Boot的配置类上添加 @EnableSwagger2 注解,以启用Swagger。 设置好依赖和配置后,重新编译和启动项目。然后,在浏览器中输入要访问的Swagger UI的地址,通常是 "http://localhost:8080/swagger-ui.html"。 如果你仍然遇到404错误,可以尝试以下解决方法: 1. 检查控制器类和方法上是否添加了Swagger的注解,例如 @ApiOperation 或 @Api。 2. 检查Swagger配置类中的包扫描路径是否正确,确保它可以扫描到你的控制器类。 3. 确认项目的启动类上是否添加了 @ComponentScan 注解,以扫描到Swagger配置类。 如果上述步骤都正确配置,但仍然遇到404错误,可能是由于其他因素引起的。你可以查看项目的日志文件或尝试重启项目进行排查。你也可以在Swagger官方文档或社区中查找关于解决404错误的更多信息和解决方案。 总结:要解决Spring Boot集成Swagger UI出现404错误的问题,需要正确配置Swagger的依赖、注解和包扫描路径,并确保启动项目后访问的URL正确。如果问题仍然存在,可以查看日志文件或在Swagger社区中寻求帮助。 ### 回答2: 当我们使用Spring Boot集成Swagger UI时,出现404错误通常是由于以下几个常见原因导致的: 1. Swagger配置错误:首先,我们需要在Spring Boot项目的配置类上添加`@EnableSwagger2`注解以启用Swagger,然后在配置类中创建一个`Docket` Bean。在创建Docket Bean时,我们需要设置API文档的基本信息,如标题、描述、版本等,并指定要扫描的API包路径。确认我们的配置正确无误。 2. 缺少Swagger依赖:我们需要在项目的pom.xml文件中添加Swagger的相关依赖,如`springfox-swagger2`和`springfox-swagger-ui`。确认我们的项目中已经正确添加了这些依赖。 3. 项目访问路径错误:默认情况下,Swagger UI页面的访问路径是`/swagger-ui.html`。请确保我们在浏览器中访问的路径与这个路径一致。如果我们想要修改访问路径,可以在Docket Bean的配置中设置`path`属性。 4. 项目的访问权限限制:如果我们的Spring Boot项目中设置了访问权限限制,例如使用了Spring Security,我们可能需要手动配置允许访问Swagger UI的路径。可以在Spring Security的配置类中添加一个针对Swagger UI路径的免认证配置。 总结一下,Spring Boot集成Swagger UI出现404错误通常是由于Swagger的配置错误、缺少相关依赖、访问路径设置不正确或者访问权限限制等问题引起的。我们需要仔细检查这些方面,并做出相应的调整和修正。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值