Thursday, 6 August 2015

Webfonts mime types with Springboot

Springboot (1.2.5) per default doesn't have mime mapping for WEB fonts (glyphicons).

This is not a real problem because the browsers which handle the WEB fonts correctly should recognize the files from the extension.

Anyway is a good practice to send the HTTP answer with the proper mime type.

Springboot make this simple: I need a component which implements the interface org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer  in the classpath of the WEB application.


package config;

import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.MimeMappings;
import org.springframework.context.annotation.Configuration;

@Configuration
public class WebFontMimeMapper implements EmbeddedServletContainerCustomizer {

    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {
        MimeMappings mappings = new MimeMappings(MimeMappings.DEFAULT);
        mappings.add("eot", "application/vnd.ms-fontobject eot;");
        mappings.add("otf", "font/opentype otf;");
        mappings.add("ttf", "font/truetype ttf;");
        mappings.add("woff", "application/font-woff;");
        mappings.add("woff2", "application/font-woff2;");
        container.setMimeMappings(mappings);
    }
}

No comments:

Post a Comment