I did upgrade from spring boot 2.6.15 to 2.7.0
More about my set up:
html thymeleaf page named myPage.html
is in directory myproject/src/main/resources/templates/html/myPage.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
is in directory myproject/src/main/resources/static/js/app/myApp.js
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";
}
}
Before update to spring boot 2.7.0 I was able to load this html page containing javascript in my android app and everything was working fine using this code to load
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://localhost:8080/myPage");
After update to springboot 2.7.0
I'm receiving an error:
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)
I was trying to find in release notes what was changed with MIME, Content-Type etc. but I found nothing https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.7-Release-Notes I also have checked deprecation list from 2.5.0 https://docs.spring.io/spring-framework/docs/2.5.x/javadoc-api/deprecated-list.html but again nothing. Maybe some body of you know what could happen and how to fix that?
Comment From: bclozel
It would be very useful to have a minimal sample application that shows this problem. Your code snippets do not show the static resource handling configuration nor if the problem only happens in tests. Can you provide such a sample?
Comment From: zaqpiotr
I'm using default static resource handling. I didn't add any config for that purpose.
Comment From: wilkinsona
Duplicates https://stackoverflow.com/questions/76642187/springboot-migration-2-6-15-to-2-7-0-error-because-its-mime-type-is-not-ex. @zaqpiotr Please don't post in multiple places, it just wastes people's time.
Comment From: eberjoe
So what's happened here? I've run into the same issue.
Comment From: wilkinsona
Nothing as we never saw a minimal reproducible example, either here or on Stack Overflow.
If you believe you've found a bug with a supported version of Spring Boot (3.3.x or later) and you'd like us to investigate, please open a new issue with a minimal sample that reproduces the problem and we can take a look.
Comment From: adamos98
I'm facing the same problem
Comment From: zaqpiotr
How to do secured WebView request with JWT token. I have come back to answer this question.
Issue was in Android WebView configuration. In my case solution was to override shouldInterceptRequest in my class WebViewClientConfig which is extending WebViewClient and pass it a JWT token. shouldInterceptRequest will call for each resource separate using jwt token. By resources here I mean: html, css, js
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
...
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
String url = request.getUrl().toString();
try {
OkHttpClient client = new OkHttpClient.Builder().build();
Request.Builder builder = new Request.Builder().url(url);
// Add your Authorization header
builder.header("Authorization", "Bearer " + this.accessToken);
// Add other headers if needed
Map<String, String> originalHeaders = request.getRequestHeaders();
for (Map.Entry<String, String> header : originalHeaders.entrySet()) {
if (!header.getKey().equalsIgnoreCase("Authorization")) {
builder.header(header.getKey(), header.getValue());
}
}
// Build the request and execute
Request okHttpRequest = builder.build();
Response response = client.newCall(okHttpRequest).execute();
if (response.isSuccessful()) {
String mimeType = response.header("Content-Type", "text/html");
MediaType mediaType = MediaType.parse(mimeType);
String charset = "UTF-8";
if (mediaType != null && mediaType.charset() != null) {
charset = mediaType.charset().name();
}
InputStream responseBodyStream = response.body().byteStream();
Map<String, String> responseHeaders = new HashMap<>();
for (String name : response.headers().names()) {
responseHeaders.put(name, response.header(name));
}
WebResourceResponse resourceResponse = new WebResourceResponse(
mediaType.type() + "/" + mediaType.subtype(),
charset,
responseBodyStream
);
resourceResponse.setResponseHeaders(responseHeaders);
return resourceResponse;
} else {
return super.shouldInterceptRequest(view, request);
}
} catch (Exception e) {
Log.e("WebViewInterceptor", "Network error while intercepting request: " + request.getUrl(), e);
return super.shouldInterceptRequest(view, request);
}
}
Then in MyActivity I'm using WebViewClientConfig class which is extending WebViewClient there I'm setWebViewClient and passing it a JWT token
webView.setWebViewClient(new WebViewClientConfig(token));
Now my html page is requested here. shouldInterceptRequest will get with token each src mentioned in html separately: css, js etc.
webView.loadUrl(url + "/endpointName");