Hendy Irawan
Hendy Irawan

Reputation: 21384

How to set recognized content/MIME types for Spring Boot/MVC resources?

Using Spring MVC's ResourceHandler it's easy to serve resources.

However, some files, i.e. .ico files are not recognized properly, they don't get the proper Content-Type header. It seems that this is handled by ResourceHttpRequestHandler :

protected MediaType getMediaType(Resource resource) {
    MediaType mediaType = null;
    String mimeType = getServletContext().getMimeType(resource.getFilename());
    if (StringUtils.hasText(mimeType)) {
        mediaType = MediaType.parseMediaType(mimeType);
    }
    if (jafPresent && (mediaType == null || MediaType.APPLICATION_OCTET_STREAM.equals(mediaType))) {
        MediaType jafMediaType = ActivationMediaTypeFactory.getMediaType(resource.getFilename());
        if (jafMediaType != null && !MediaType.APPLICATION_OCTET_STREAM.equals(jafMediaType)) {
            mediaType = jafMediaType;
        }
    }
    return mediaType;
}

So how do I configure recognized file extensions / MIME types in Spring Boot 1.2? (for both Tomcat and Undertow)

Upvotes: 2

Views: 4916

Answers (1)

Dave Syer
Dave Syer

Reputation: 58094

CustomizableEmbeddedServletContainerFactory has a method for setting mime types. You can get a callback by providing a bean of type EmbeddedServletContainerCustomizer.

Upvotes: 4

Related Questions