I'm building a Spring Boot and AngularJS application. I'm having a problem with this error in my browser when I try to execute it:
Refused to execute script from 'http://localhost:8080/webjars/angularjs/1.5.3/angular.min.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
repeated several times for each js file. I also get a similar error for the CSS file.
I found this answer Spring MVC (Boot) does not send MIME type for certain files (WOFF, etc) however my implementation of it doesn't seem to work. Here is what I've done:
@Configuration
public class ServletCustomizer implements EmbeddedServletContainerCustomizer {
private Logger logger = Logger.getLogger(ServletCustomizer.class);
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
MimeMappings mappings = new MimeMappings(MimeMappings.DEFAULT);
mappings.add("js","application/javascript");
mappings.add("css","text/css");
container.setMimeMappings(mappings);
logger.info("Configured custom mime mappings");
}
}
The log message confirms the code is running.
I would have thought that js and css mappings would be part of the default mappings. Can anyone advise me how to correctly set the mappings so css and js files are sent with the correct mime types from the embedded Tomcat server?
Edit:
Here's my home.html template:
<html lang="en" >
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<!-- Angular Material style sheet -->
<link rel="stylesheet" href="/webjars/angular_material/1.1.0-rc2/angular-material.min.css" />
</head>
<body ng-app="BlankApp" ng-cloak layout="column">
<md-toolbar>
My Title
</md-toolbar>
<div flex layout="row">
<md-sidenav flex="15" md-is-locked-open="true" class="md-whiteframe-z1">
<md-content>
sidenav
</md-content>
</md-sidenav>
<div layout="column" flex>
<div class="box1">
70
</div>
</div>
</div>
<!-- Angular Material requires Angular.js Libraries -->
<script src="/webjars/angularjs/1.5.3/angular.min.js"></script>
<script src="/webjars/angularjs/1.5.3/angular-animate.min.js"></script>
<script src="/webjars/angularjs/1.5.3/angular-aria.min.js"></script>
<script src="/webjars/angularjs/1.5.3/angular-messages.min.js"></script>
<!-- Angular Material Library -->
<script src="/webjars/angular_material/1.1.0-rc2/angular-material.min.js"></script>
<!-- Your application bootstrap -->
<script type="text/javascript">
/**
* You must include the dependency on 'ngMaterial'
*/
angular.module('BlankApp', ['ngMaterial']);
</script>
</body>
</html>
And my gradle configuration:
buildscript {
ext {
springBootVersion = '1.3.6.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'
jar {
baseName = 'freestyle'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.cloud:spring-cloud-starter-oauth2')
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-hateoas')
compile('org.springframework.boot:spring-boot-starter-security')
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.hsqldb:hsqldb')
compile 'org.webjars:angularjs:1.5.7'
compile 'org.webjars:angular-material:1.0.9'
compile group: 'net.sourceforge.nekohtml', name: 'nekohtml', version: '1.9.22'
testCompile('org.springframework.boot:spring-boot-starter-test')
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:Brixton.SR2"
}
}
eclipse {
classpath {
containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
}
}
The application container I'm using is:
@SpringBootApplication
public class FreestyleApplication {
public static void main(String[] args) {
SpringApplication.run(FreestyleApplication.class, args);
}
}
Not sure what less I could show that might show where the problem is. I initially did try it without the ServletCustomizer.