Spring 将 returns js 文件引导为 MIME 'application/json' 而不是 'text/javascript'

Spring Boot returns js files as MIME 'application/json' instead of 'text/javascript'

我在 Spring Boot 中使用 Thymeleaf。加载项目站点的页面时,它说我的脚本被阻止,因为它被返回为 json 而不是 text/javascript.

我使用标准的 Thymeleaf 模板将脚本包含在 html 中:

  <script type="text/javascript" th:src="@{/js/admin-register-users.js}" defer></script>

我按以下方式设置了资源处理:


@Configuration
public class WebConfig implements WebMvcConfigurer {
 
    
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
        registry.addResourceHandler("/img/**").addResourceLocations("classpath:/static/img/");
        registry.addResourceHandler("/css/**").addResourceLocations("classpath:/static/css/");
        registry.addResourceHandler("/js/**").addResourceLocations("classpath:/static/js/");

        WebMvcConfigurer.super.addResourceHandlers(registry);
    }
}

出于某种原因,我在浏览器中收到错误消息:

The resource from “https://localhost:8090/js/admin-register-users.js” was blocked due to MIME type (“application/json”) mismatch (X-Content-Type-Options: nosniff).

结果是找不到文件,因此从服务器返回了 json 响应,但该响应被阻止,无法读取。

长话短说,与资源配置无关。文件不是从 TypeScript 生成的。所以服务器只找到一个空的js文件夹。