1

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);
    }

}

IntelliJ Project Structure

Not sure what less I could show that might show where the problem is. I initially did try it without the ServletCustomizer.

Community
  • 1
  • 1
David Findlay
  • 1,296
  • 1
  • 14
  • 30
  • 1
    Both are part of the default mappings so it should work without any additional configuration. Can you share a minimal example that reproduces the problem? – Andy Wilkinson Jul 17 '16 at 06:51
  • I've added a bit more. It was really a barebones basic Spring Boot set up and I added the webjars for Angular JS. Nothing much else done. – David Findlay Jul 17 '16 at 07:57
  • 1
    There are a few small mistakes in your `home.html`, for example the Angular version numbers don't match those in your `build.gradle` and you have `angular_material` in a couple of places where it should be `angular-material`. These were causing a number of 404 errors. With those correct, the 404s are gone and the Javascript and CSS is served with the correct content type. To make any progress, I think you could really do with sharing a complete example that reproduces the problem. A repository on GitHub that can be cloned and run would be ideal. – Andy Wilkinson Jul 17 '16 at 19:30
  • Here's the github repository: https://github.com/davidfindlay/freestyle I just tried to load the the resources directly with the url that's in the script srcs. I got my login page. So that explains why I'm getting the wrong mime type. Is it possible my WebSecurity config is bad? None of the examples I've seen seem to show anything required to enable access to the static resources. – David Findlay Jul 17 '16 at 21:36
  • Thanks for your help with this. It turned out the main problem was the WebSecurityConfig being wrong, meaning instead of loading the resource files it was loading the login page, hence the mime type errors. The login page was hiding the 404 errors so I hadn't realised my URLs were wrong until you suggested it. I've now fixed it in my github example. – David Findlay Jul 18 '16 at 11:03

0 Answers0