如何为Spring Boot/MVC资源设置可识别的内容/MIME类型?
使用它很容易为资源服务 但是,某些文件(即ico文件)无法正确识别,因此无法获取正确的内容类型标题。这似乎是由ResourceHttpRequestHandler处理的:如何为Spring Boot/MVC资源设置可识别的内容/MIME类型?,spring,spring-mvc,tomcat,spring-boot,undertow,Spring,Spring Mvc,Tomcat,Spring Boot,Undertow,使用它很容易为资源服务 但是,某些文件(即ico文件)无法正确识别,因此无法获取正确的内容类型标题。这似乎是由ResourceHttpRequestHandler处理的: 那么如何在SpringBoot1.2中配置可识别的文件扩展名/MIME类型呢?对于Tomcat和Undertow,定制的EmbeddedServletContainerFactory都有一个。您可以通过提供EmbeddedServletContainerCustomizer类型的bean来获得回调。但是,当应用程序作为war部
那么如何在SpringBoot1.2中配置可识别的文件扩展名/MIME类型呢?对于Tomcat和Undertow,定制的EmbeddedServletContainerFactory都有一个。您可以通过提供EmbeddedServletContainerCustomizer类型的bean来获得回调。但是,当应用程序作为war部署在单独的servlet容器中时,这不起作用。然后您应该配置容器,但我正在尝试提出一个不需要配置所有服务器实例的解决方案。关于如何在Spring Boot中处理这个问题,您有什么想法吗?它可能是您可以在容器中配置的东西,例如Tomcat有一个web.xml,可以为所有应用程序创建默认映射。
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;
}