javascript - 升级到 Spring 4.3.0,静态 JS 文件现在作为 'application/octet-stream' 而不是 Spring 4.2.6 中的 'application/javascript'

标签 javascript java spring spring-mvc

我最近从 Spring 4.2.6 升级到 4.3.0,现在当我的网页尝试加载静态 javascript 文件时,我在 Chrome 中收到以下错误。

Refused to execute script from http://.../someJsFile.js because its MIME type ('application/octet-stream') is not executable, and strict MIME type checking is enabled.

我仔细检查了响应 header (使用 Spring 4.3.0),我确实看到了 Content-Type:application/octet-stream。然而,当我回到 Spring 4.2.6 时,我看到了 Content-Type:application/javascript

文件包含在我的页面中

<!DOCTYPE html>
<html lang="en">
<!-- ... -->
<body>
  <!-- ... -->
  <!-- Adding type="text/javascript" does not help -->
  <script src="/resources/js/someJsFile.js"></script>
</body>
</html>

我的applicationContext.xml 具有以下配置

<!-- ... -->
<context:component-scan base-package="some.package.spec" /> 
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />
<!-- ... -->

我尝试查看 Spring 4.3.0 发行说明,但没有立即发现什么。

知道 4.3.0 中的哪些变化正在影响我吗?我如何告诉 Spring 4.3.0 为静态 javascript 文件设置正确的内容类型?如果您需要有关配置等的更多详细信息,请告诉我。

最佳答案

此错误已添加到里程碑 4.3.1(请参阅 bug report)并在版本 4.3.3 上标记为已完全解决。

Js 模块 (mjs) 仍然由 springframework web 作为 application/octet-stream 服务。为了让它们作为 application/javascript 服务,有必要在 servlet 描述符 (web.xml) 中映射 mime 类型:

<mime-mapping>
    <extension>mjs</extension>
    <mime-type>application/javascript</mime-type>
</mime-mapping>

关于javascript - 升级到 Spring 4.3.0,静态 JS 文件现在作为 'application/octet-stream' 而不是 Spring 4.2.6 中的 'application/javascript',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37822460/

相关文章:

javascript - 无法操作 $.post 中的数据

javascript - 多色文本动画,但仅在悬停时

javascript - 具有过滤表达式的 DynamoDB 扫描表

java - 当url在斜杠后有参数数据时,如何编写 Controller 方法的签名?

javascript - 更改 firebase.database 内的变量

java.sql.SQLException : Invalid Oracle URL specified

java - Spring Test 在 Spring Boot 中测试 Rest API 时返回 404 而不是 200

java - 对于应该由子类指定的数据,我应该使用抽象方法还是实例变量?

spring - 是否有任何可能的方法可以忽略 JPA Example Matcher 的所有路径

spring - Spring :正确学习道路上的困惑