Spring Boot嵌入式Tomcat Mime映射

时间:2016-07-16 23:48:11

标签: spring spring-mvc spring-boot

我正在构建一个Spring Boot和AngularJS应用程序。我尝试执行此操作时,我在浏览器中遇到此错误的问题:

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.

为每个js文件重复几次。我也得到了CSS文件的类似错误。

我找到了这个答案Spring MVC (Boot) does not send MIME type for certain files (WOFF, etc)但是我对它的实现似乎并不奏效。这就是我所做的:

@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");

    }

}

日志消息确认代码正在运行。

我原以为js和css映射是默认映射的一部分。任何人都可以建议我如何正确设置映射,以便从嵌入式Tomcat服务器使用正确的mime类型发送css和js文件?

编辑:

这是我的home.html模板:



<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>
&#13;
&#13;
&#13;

我的gradle配置:

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'
    }
}

我使用的应用程序容器是:

@SpringBootApplication
public class FreestyleApplication {

    public static void main(String[] args) {
        SpringApplication.run(FreestyleApplication.class, args);
    }

}

IntelliJ Project Structure

不确定我能展示什么,可能显示问题所在。我最初没有使用ServletCustomizer尝试它。

0 个答案:

没有答案