SpringBoot整合Swagger,访问Swagger-ui.html导致404问题

本文档详细记录了解决Springfox Swagger2在3.0.0版本中访问404的问题。问题源于访问路径和资源映射的改变,解决方案是引入springfox-boot-starter依赖或手动配置WebMvcConfigurer以适配新的访问方式。通过配置正确的访问路径和资源映射,最终可以成功访问Swagger UI。

错误复盘:

1、导入jar包

<dependency>
     <groupId>io.springfox</groupId>
     <artifactId>springfox-swagger2</artifactId>
     <version>3.0.0</version>
</dependency>
<dependency>
     <groupId>io.springfox</groupId>
     <artifactId>springfox-swagger-ui</artifactId>
     <version>3.0.0</version>
</dependency>

2、配置Swagger

package com.lpf.tools;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * swagger配置类
 */
@Configuration
@EnableSwagger2
@ComponentScan(basePackages = "com.lpf.controller")
public class MyAPIConfig {

    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();

    }

    private ApiInfo apiInfo(){
        Contact contact = new Contact("个人", "localhost:8082", "xxxx@qq.com");
        return new ApiInfoBuilder()
                .title("文档标题")
                .description("文档描述")
                .contact(contact)
                .build();
    }
}

上面两步没错

3、直接访问localhost:8080/项目名(如果配置了的话)/swagger-ui.html

出现404错误

解决方案:

404:要么地址错误,要么文件不存在

地址错误情况:由于访问的是swagger-ui.html,如果你看的是教学视频,大概是访问这个链接,暂时认为是正确的。

文件不存在情况:查看swagger的ui包,如下

并不存在swagger.html这个文件,所以暂时认为是版本较高导致与之前的访问方式不一致。

4、在网上找原因查找了一会,发现一个包

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

导包后,查看

public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String baseUrl = StringUtils.trimTrailingCharacter(this.baseUrl, '/');
        registry.addResourceHandler(new String[]{baseUrl + "/swagger-ui/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/springfox-swagger-ui/"}).resourceChain(false);
    }

    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController(this.baseUrl + "/swagger-ui/").setViewName("forward:" + this.baseUrl + "/swagger-ui/index.html");
    }

通过这段代码可以知道,swagger访问方式发生了改变,不再是之前版本的swagger-ui.html;我们访问/swagger-ui/xx的资源,才会转到访问/META-INF/resources/webjars/springfox-swagger-ui/文件夹下的资源!,这个文件帮我们在mvc中加了静态资源的过滤。

正确的访问连接应该是:localhost:8080/项目名(如果配置了的话)/swagger-ui/index.html

自此访问swagger成功。

404的原因是访问路径改变和访问的文件名也与之前版本不一致!

 

根本原因是我们需要配置正确的访问方式和资源映射方式:

回归到之前导入两个包的情况,没有了starter帮我们配置访问路径额和资源映射:

那我们需要自己配置一下mvc

package com.lpf.tools;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * mvc配置文件
 *
 */
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    /**
     *
     * 页面路由功能
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //此处也可以不配置,直接访问index.html
        registry.addViewController("/swagger-ui/swagger.html").setViewName("forward:/swagger-ui/index.html");
    }


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/swagger-ui/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/");
    }
}

这样我们也能通过localhost:8080/项目名(如果配置了的话)/swagger-ui/index.html访问swagger

注意:路径中必须存在/swagger-ui/  不然会导致swagger页面空白!

通过浏览器的Console窗口发现,原因是在swagger的js文件中设置了基本的路径中必须存在/swagger-ui/

必须存在该路径

在使用 Spring Boot 3 集成 Swagger 时,访问 `/swagger-ui/index.html` 出现 404 错误,通常是由于静态资源路径映射问题或依赖版本不兼容导致的。以下是解决该问题的综合方案: ### 1. 确保使用兼容 Spring Boot 3 的 Swagger 依赖 Spring Boot 3 已经全面转向 Jakarta EE 9,包名从 `javax` 改为 `jakarta`,因此需要确保使用的 Swagger 依赖兼容 Spring Boot 3。 推荐使用 `springdoc-openapi` 而非 `springfox`,因为后者尚未完全支持 Spring Boot 3。以下是适用于 Spring Boot 3 的依赖配置: ```xml <dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId> <version>1.6.14</version> </dependency> ``` 该依赖会自动引入 OpenAPI 3.0 支持,并提供 `/swagger-ui.html` 和 `/swagger-ui/index.html` 的访问能力。 ### 2. 检查静态资源路径配置 在某些情况下,由于自定义了 `WebMvcConfigurer` 或继承了 `WebMvcConfigurationSupport`,可能导致默认的静态资源路径映射被覆盖。需要确保以下配置存在或调整: ```java @Configuration public class WebConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/swagger-ui/**") .addResourceLocations("classpath:/META-INF/resources/webjars/springdoc/"); registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); } } ``` 此配置确保了 `/swagger-ui/` 路径下的资源能够正确加载。 ### 3. 确认是否启用了 Swagger UI 在 Spring Boot 3 中使用 `springdoc-openapi` 时,可以通过以下配置启用并自定义 Swagger访问路径: ```yaml springdoc: swagger-ui: path: /swagger-ui/index.html enabled: true ``` 该配置确保 `/swagger-ui/index.html` 是有效的访问路径。 ### 4. 检查跨域(CORS)配置 如果项目中集成了 Shiro 或 Spring Security,并且存在跨域问题,需要在配置中添加合适的 CORS 支持: ```java @Configuration public class CorsConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/swagger-ui/**") .allowedOrigins("*") .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") .allowedHeaders("*") .exposedHeaders("*"); } } ``` ### 5. 确保没有冲突的 Swagger 实现 避免同时引入 `springfox` 和 `springdoc`,因为它们功能重叠且可能导致路径冲突。建议完全移除 `springfox` 依赖,仅使用 `springdoc`。 ### 6. 检查项目上下文路径(Context Path) 如果应用配置了 `server.servlet.context-path`,例如: ```yaml server: servlet: context-path: /api ``` 则访问路径应为:`http://localhost:8080/api/swagger-ui/index.html`。确保路径中包含正确的上下文前缀。 ---
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值