Spring 引导应用程序在提供静态内容时显示文件下载选项



我有一个 Spring 启动应用程序,其中我有一些静态文件保存在目录中resources/static目录中。其中之一是招摇目录。文件系统布局如下所示:

resources
- static
- swagger
- index.html

现在,如果我使用 URIlocalhost:8080/swagger/index.html向我的 Web 应用程序发送请求,那么它会正确提供文件。但是,如果我向localhost:8080/swagger发送请求,则 Web 应用程序会显示一个文件下载框,其中包含一个名为swagger的空二进制文件。

在我看来,第二个 URI 实际上应该自动提供index.html文件。如何修复此行为?

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class WebConfig {
@Bean
public WebMvcConfigurerAdapter forwardToIndex() {
return new WebMvcConfigurerAdapter() {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/swagger").setViewName(
"forward:/swagger/index.html");
registry.addViewController("/swagger/").setViewName(
"forward:/swagger/index.html");
}
};
}
}

最新更新