I'd like the ability to easily customize the file extension -> media type map used for serving static resources in a WebFlux application. Specifically, I'd like a simple method to be able to register a new mapping, or change an existing mapping.
Currently, ResourceWebHandler
and ResourceHttpMessageWriter
both call MediaTypeFactory.getMediaType(resource)
to determine the media type of a resource. MediaTypeFactory
loads its mappings from the /org/springframework/http/mime.types
file on the classpath. As far as I can tell, there is no easy way to customize these mappings in a WebFlux application. (It looks like you could technically extend ResourceHttpMessageWriter
to customizer the resolution of the media type, but most of what is needed is private, so it would basically require re-implementing most of the logic in ResourceHttpMessageWriter
. This is pretty heavyweight for just adding an additional mapping.)
For comparison, in a servlet-based application, the mappings can be easily customized via ConfigurableServletWebServerFactory.setMimeMappings(MimeMappings mimeMappings)
.
(And FWIW, I need a mapping for xsd
, and would also be happy if that was added to the defaults.)
Comment From: rstoyanchev
We could add setMediaTypes(Map<String, MediaType>)
to ResouceWebHandler
and also expose it via ResourceHandlerRegistry
for configuration in a WebFluxConfigurer
. On the Spring MVC side the same property exists in ResourceHttpRequestHandler
so we can make the two consistent.