Skip to content
标签
欢迎扫码关注公众号

Swagger-UI 页面 404 问题

项目使用的 Swagger 3.0 ,依赖如下。

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

之前是可以正常打开 /swagger-ui/ 页面的,经过一段时间的开发之后,页面显示 404 NOT FOUND

检查之后发现,/v3/api-docs 等接口还是可以正常访问的,对比别的应用 /swagger-ui//swagger-ui/**/webjars/** 这三个映射没有了。

这篇博客中提到了一种自定义 WebMvcConfigurer 的解决方案,运行后 /swagger-ui/index.html 地址可以访问,但是 /swagger-ui/ 地址无法访问(看代码里是加了映射的,不知道哪里出了问题)。

java
@Configuration
public class WebConfig implements WebMvcConfigurer {

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

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

StackOverflow上的一个回答上提到移除 @EnableWebMvc 注解可以解决这个问题,虽然项目中没有使用这个注解,但是项目中为了添加拦截器,自定义了一个继承自 WebMvcConfigurationSupport 的配置。

java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    @Autowired
    private CustomInterceptor customInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(customInterceptor).addPathPatterns("/**");
    }
}

将上述代码删除后,/swagger-ui/ 可以正常访问了。

那么 @EnableWebMvcWebMvcConfigurationSupport 有什么关系呢?下面是摘自pinebud55 的博客上的说明:

@EnableWebMvc = WebMvcConfigurationSupport,使用了 @EnableWebMvc 注解等于扩展了 WebMvcConfigurationSupport 但是没有重写任何方法。

  1. @EnableWebMvc + extends WebMvcConfigurationAdapter,在扩展的类中重写父类的方法即可,这种方式会屏蔽 SpringBoot 的 @EnableAutoConfiguration 中的设置
  2. extends WebMvcConfigurationSupport,在扩展的类中重写父类的方法即可,这种方式会屏蔽 SpringBoot 的 @EnableAutoConfiguration 中的设置
  3. extends WebMvcConfigurationAdapter,在扩展的类中重写父类的方法即可,这种方式依旧使用 SpringBoot 的 @EnableAutoConfiguration 中的设置

上面的第二点就是 404 的原因了,这种方式屏蔽了 SpringBoot 的 @EnableAutoConfiguration 中的设置。
采用上面第三点中的方式不会屏蔽 SpringBoot 的默认配置,不过在 SpringBoot 2.x 中这个抽象类 WebMvcConfigurationAdapter 已经过期或者被删除了,替代方案是使用 implements WebMvcConfigurer

代码修改为如下方式就可以修复 swagger-ui 的 404 问题了。

java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private CustomInterceptor customInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(customInterceptor).addPathPatterns("/**");
    }
}

参考

  1. 解析@EnableWebMvc、WebMvcConfigurationSupport 和 WebMvcConfigurationAdapter
  2. SPRINGBOOT2.3.5 整合 SWAGGER3.0 以及访问 SWAGGER 首页 404 的问题
  3. swagger-ui No mapping found for HTTP request

Page Layout Max Width

Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the page layout
A ranged slider for user to choose and customize their desired width of the maximum width of the page layout can go.

Content Layout Max Width

Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the content layout
A ranged slider for user to choose and customize their desired width of the maximum width of the content layout can go.