You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SpringBoot 2.6.15升级至2.7.0后JS脚本MIME类型异常问题

Spring Boot 2.7.x升级后Android WebView加载JS脚本提示MIME类型为空问题

将Spring Boot从2.6.15升级至2.7.0及更高版本(如2.7.13)后,原本可在Android WebView正常加载的Thymeleaf页面出现异常:加载时提示拒绝执行/js/app/myApp.js脚本,原因是其MIME类型为空且启用了严格MIME类型检查。

环境配置

  • Thymeleaf页面myPage.html位于myproject/src/main/resources/templates/html/目录,代码如下:
<!DOCTYPE HTML>
<html>
<head>
    <title>MyPage</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title></title>
    <link href='https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Material+Icons' rel="stylesheet">
    <link rel="icon" type="image/png" th:href="@{/img/favicon.png}" />
</head>
<body>
<div id="app"></div>
<script type="text/javascript" th:src="@{/js/app/myApp.js}"></script>
</body>
</html>
  • myApp.js位于myproject/src/main/resources/static/js/app/目录
  • Spring Controller代码:
package com.myapp;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class ViewController {

  @GetMapping("/myPage")
  public String myPage() {
    return "html/myPage";
  }

}

升级前后表现

  • 升级前(2.6.15):使用以下Android代码加载页面完全正常:
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://localhost:8080/myPage");
  • 升级后(2.7.0及2.7.13):出现如下错误日志:

I/chromium: [INFO:CONSOLE(0)] "Refused to execute script from 'http://localhost:8080/js/app/myApp.js' because its MIME type ('') is not executable, and strict MIME type checking is enabled.", source: http://localhost/myPage (0)

已尝试的操作

  • 查阅Spring Boot 2.7发布说明及Spring Framework 2.5.x废弃列表,未找到相关变更信息
  • 使用curl测试两个版本的JS文件返回Content-Type均为application/javascript,结果一致

可能的解决方向

  1. 显式配置MIME类型映射
    在application.properties或application.yml中添加JS的MIME类型映射,强制指定响应头:
spring.mvc.contentnegotiation.media-types.js=application/javascript
  1. 检查静态资源处理配置
    Spring Boot 2.7对静态资源处理可能存在默认配置变更,检查是否存在spring.mvc.static-path-pattern或spring.web.resources.static-locations的默认值变化,确保静态资源路径映射正确。

  2. 验证WebView的MIME检查设置
    临时调整Android WebView的设置,关闭严格MIME类型检查以验证问题是否由WebView端触发(仅用于排查,不推荐生产环境使用):

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
}
  1. 排查自定义过滤器/拦截器
    检查项目中是否存在自定义的Web过滤器或拦截器,是否在升级后修改了响应头中的Content-Type字段,导致MIME类型丢失。

  2. 确认Thymeleaf生成的路径正确性
    查看WebView加载页面的源码,确认<script>标签的src属性是否为正确的/js/app/myApp.js,排除路径生成错误导致的资源请求异常。

内容的提问来源于stack exchange,提问作者Piotr Żak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 19:32:46