Spring MVC(启动)不会为某些文件(WOFF等)发送MIME类型 [英] Spring MVC (Boot) does not send MIME type for certain files (WOFF, etc)

查看:181
本文介绍了Spring MVC(启动)不会为某些文件(WOFF等)发送MIME类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个基于Spring Boot的应用程序,并注意到了chrome中的一些警告.它抱怨说例如网络字体(扩展名woff)以纯文本/文本而不是其正确的mime类型发送.

I am writing a spring boot based application and noticed a few warnings in chrome. It complains that for example web fonts (extension woff) are send as plain/text instead of their correct mime type.

我对静态文件使用常规机制,而无需进行特殊配置. 我发现的源代码似乎无法为常规" ResourceHandler添加更多的模仿类型. Resourcehandler将mime类型识别分派到servlet容器,该容器是spring-boot 1.2的默认tomcat.

I am using the regular mechanism for static files without special configuration. The sourcecode I found looks like it's not possible to add more mimetypes for the "stock" ResourceHandler. The Resourcehandler dispatches the mime type recognition to the servlet container, which is the default tomcat for spring-boot 1.2.

我错过了什么吗?有人知道一种简单的方法来增强资源映射,以使用正确的mime类型提供更多文件类型吗?

Am I missing something? Does someone know an easy way to to enhance the resource mapping to serve more file types with the correct mime type?

现在,我正在考虑编写一个过滤器,该过滤器针对静态内容触发,并在事后修补缺少的mimetypes.也许我应该在springsource上创建功能请求...;-)

Right now I'm thinking to write a filter that is triggered for static content and patches missing mimetypes after the fact. Maybe I should create a feature request at springsource... ;-)

推荐答案

好的,我自己找到了它:-)

OK, found it myself :-)

在Spring引导中,您可以使用此定制程序自定义servlet容器,并在其中添加新的mimetypes.

In Spring boot you can customize the servlet container with this customizer and add new mimetypes there.

( UPDATE )

Spring-boot 2.x:

Spring-boot 2.x:

@Component
public class ServletCustomizer implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {
    @Override
    public void customize(TomcatServletWebServerFactory factory) {
        MimeMappings mappings = new MimeMappings(MimeMappings.DEFAULT);
        mappings.add("woff", "application/x-font-woff");
        factory.setMimeMappings(mappings);
    }
}

Spring-boot 1.x:

Spring-boot 1.x:

@Component
public class ServletCustomizer implements EmbeddedServletContainerCustomizer {

    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {
        MimeMappings mappings = new MimeMappings(MimeMappings.DEFAULT);
        mappings.add("woff","application/font-woff");
        mappings.add("woff2","application/font-woff2");
        container.setMimeMappings(mappings);
    }
}

这篇关于Spring MVC(启动)不会为某些文件(WOFF等)发送MIME类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆