Spring MVC (Boot) does not send MIME type for certain files (WOFF, etc)

前端 未结 1 1162
一个人的身影
一个人的身影 2020-12-28 21:04

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

相关标签:
1条回答
  • 2020-12-28 21:46

    OK, found it myself :-)

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

    (UPDATE)

    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:

    @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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题