springboot集成Swagger3.0.0

在spring boot项目中
maven依赖:

<!--swagger-->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

建个config包,创建Swagger配置类如下,可直接复制使用

package com.ywp.springboot2022.config;

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.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import sp

Spring Boot 项目中集成 Swagger 后访问文档出现 404 错误是一个较为常见的问题,通常与配置、路径变更或依赖版本不兼容有关。以下是一些可能的原因及对应的解决方案: ### 1. 检查 Swagger 的访问路径 Spring Boot 集成 Swagger 的访问路径在 Swagger 3.0.0 版本之后发生了变化。如果使用的是 Swagger 3.0.0 或更高版本,访问路径应为: ``` http://127.0.0.1:8080/swagger-ui/index.html ``` 而旧版本(3.0.0 之前)的访问路径为: ``` http://127.0.0.1:8080/swagger-ui.html ``` 如果访问路径不正确,就会导致 404 错误。请确认使用的 Swagger 版本,并确保访问路径与其匹配[^1]。 ### 2. 确保正确配置了 SwaggerSpring Boot 项目中,需要确保正确启用了 Swagger 并进行了配置。通常需要添加以下注解: ```java @Configuration @EnableOpenApi public class SwaggerConfig { // 配置内容 } ``` 如果使用的是 `springfox`,由于其已停止更新,可能无法正常工作,特别是在 Spring Boot 3 中。推荐改用 `springdoc-openapi`,它支持 OpenAPI 3 规范,并与 Spring Boot 3 兼容[^2]。 ### 3. 添加正确的依赖 对于 Spring Boot 3 项目,`springfox` 不再适用,需要使用 `springdoc-openapi`。在 `pom.xml` 文件中添加以下依赖: ```xml <dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId> <version>1.6.14</version> </dependency> ``` 确保依赖版本与 Spring Boot 版本兼容。如果使用的是较新版本的 Spring Boot,可能需要选择更高版本的 `springdoc-openapi`。 ### 4. 检查 Spring Boot 的 Web 配置 确保没有错误地配置了静态资源或 URL 映射。如果使用了自定义的 `WebMvcConfigurer`,检查是否覆盖了默认的静态资源处理方式。例如,以下代码可能会导致 Swagger 页面无法访问: ```java @Configuration public class WebConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**") .addResourceLocations("classpath:/static/"); } } ``` 上述代码可能会阻止对 Swagger 资源的访问。可以修改为以下方式,确保静态资源和 Swagger 资源都能正确加载: ```java @Configuration public class WebConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**") .addResourceLocations("classpath:/static/") .setCachePeriod(0); registry.addResourceHandler("/swagger-ui/**") .addResourceLocations("classpath:/META-INF/resources/webjars/springdoc/openapi-ui/"); } } ``` ### 5. 检查应用的上下文路径 如果应用的上下文路径不是默认的 `/`,例如在 `application.properties` 中配置了 `server.servlet.context-path=/api`,那么访问 Swagger 的完整路径应该是: ``` http://127.0.0.1:8080/api/swagger-ui/index.html ``` 确保在配置了上下文路径后,正确调整了 Swagger 的访问路径。 ### 6. 检查防火墙或安全配置 如果项目中使用了 Spring Security 或其他安全框架,需要确保没有对 Swagger 资源进行拦截。例如,在 Spring Security 的配置中添加以下代码: ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/swagger-ui/**", "/v3/api-docs/**").permitAll() .anyRequest().authenticated(); } } ``` 上述代码确保了对 Swagger 资源的访问不受到安全限制。 ### 7. 确保应用启动时没有错误 检查应用启动日志,确认没有与 Swagger 相关的错误或异常。例如,如果依赖版本不兼容,可能会导致应用启动失败,从而无法访问 Swagger 文档。 ### 8. 清理并重新构建项目 如果以上步骤均未解决问题,可以尝试清理项目并重新构建。例如,使用 Maven 执行以下命令: ```bash mvn clean install ``` 然后重新启动应用,尝试访问 Swagger 文档。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值