2

I have a spring-boot/netty application setup to serve some static file from main/resource/static. The application is also setup for compression.

server:
  compression:
    enabled: true
    mime-types: application/geo+json, application/geo+json-seq, text/html, text/xml, text/plain, text/css, text/javascript, application/javascript, application/json, image/jpeg, application/vnd.geo+json
    min-response-size: 2KB

One of the files is World_Countries_Boundaries.geojson which is roughly 10MB and contains country boundaries.

The goal is to get this file gzip encoded.

This file should be considered application/geo+json but the response header shows application/octet-stream. It should also be gzip encoded.

response headers

How can I get this file to compress? My first thought was that spring is not resolving the mime-type correctly. This let me to an answer here. But this answer is only for a servlet container. My application is using netty. So I tried the netty factory.

import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
import org.springframework.boot.web.server.MimeMappings;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CustomMimeMappings implements WebServerFactoryCustomizer<NettyReactiveWebServerFactory> {
    @Override
    public void customize(NettyReactiveWebServerFactory factory) {
        MimeMappings mappings = new MimeMappings(MimeMappings.DEFAULT);
        mappings.add("geojson", "application/geo+json");
        factory.setMimeMappings(mappings);
    }
}

This solution does not compile because the netty factory does not have a setMimeMappings method. This is where I am stuck.

How do I configure the application to correctly resolve the mime-type and compress this geojson file?

John Mercier
  • 1,637
  • 3
  • 20
  • 44

1 Answers1

1

You're almost there. I'm using Spring Boot 3.0.4

The application config looks correct, but you'll need to add a WebFluxConfigurer...

    @Configuration
    public class GeodataConfigurer implements WebFluxConfigurer {
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            var mimeTypes = new HashMap<String, MediaType>();
            mimeTypes.put("geojson", MediaType.parseMediaType("application/geo+json"));
            registry.addResourceHandler("/World_Countries_Boundaries.geojson")
                    .addResourceLocations("classpath://internal/
World_Countries_Boundaries.geojson")
                    .setMediaTypes(mimeTypes);
        }
    }
KevinC
  • 306
  • 2
  • 6