Can Spring Boot serve precompressed assets

I'm wondering if Spring or Spring Boot supports serving precompressed resources.

For example nginx supports this with the gzip_static on flag. When a client sends the Accept-Encoding: gzip request header, nginx checks if there is a file with the requested name and the ending '.gz'. If such a file exists nginx serves that to the client and saves some CPU cycles because it does not have to compress it on the fly.

Can I do the same with Spring and compress all my files in the static folder and the embedded web server serves them like nginx?


Yes, you can configure gzip compression for specific resources and their mime-types.

Here is what it looks like if you use application.yml file:

server:
  compression:
    enabled: true
    mime-types: text/html,text/xml,text/plain,text/css,text/javascript,application/javascript
    min-response-size: 2048

And here is similar configuration for application.properties file:

server.compression.enabled=true # If response compression is enabled.
server.compression.mime-types=text/html,text/xml,text/plain,text/css,text/javascript,application/javascript # Comma-separated list of MIME types that should be compressed.
server.compression.min-response-size=2048 # Minimum response size that is required for compression to be performed.

You can define with server.compression.mime-types property which assets have to be compressed, here in this example I have used some defaults from Appendix A. Common application properties .

Here you can see style.css (439 KB) file from some demo Spring Boot app with no compression:

And here the same file but with compression enabled (65.8 KB):

Configuring Spring Boot's Server, GZip compression, HTTP/2 , Spring Boot uses Tomcat as the default embedded server. You can enable HTTP2 in spring boot with the following property, if the server has  By default, Spring Boot serves all static content under the root part of the request, that is, /**. Even though it seems to be a good default configuration, we can change it via the spring.mvc.static-path-pattern configuration property.


Spring supports this use case with the org.springframework.web.servlet.resource.GzipResourceResolver

This resolver searches for files ending with .gz when the client sends the Accept-Encoding: gzip header.

This was introduced with Spring 4.1.

You can enable this resolver with a resource configuration:

@Configuration
class ResourceConfig implements WebMvcConfigurer {

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/**")
            .addResourceLocations("classpath:/static/")
            .resourceChain(false).addResolver(new GzipResourceResolver());
  }
}

Spring Boot 1.4 added auto configuration support for the gzip resolver. Add this to your application.properties file and Spring Boot automatically registers the GzipResourceResolver

spring.resources.chain.gzipped=true

Support for serving Brotli encoded, static resources [SPR-16381 , Referenced from: commits spring-projects/spring-boot@56ab0da All it does is serving pre-compressed files with the ending .br when the client is specific encodings, and added an extra property to make that configurable. The spring-boot-starter-parent has some common configurations for a Spring Boot application. The spring-boot-starter-web is a starter for building web applications with Spring MVC. It uses Tomcat as the default embedded server. The spring-boot-maven-plugin provides Spring Boot support in Maven, allowing us to package executable JAR or WAR archives.


As far I know you haven’t such possibility but you can do this qute easilly yourself. I think this is actually should be implemented on Tomcat or Jetty level. If you implemented the filter then please contribute because I’m looking too

spring-projects / spring-boot, @wilkinsona Good point: so I guess we can still have an opinion, but let's make sure there's (I'd love to see the proposed MVC Resources support for precompressed statics.) @dsyer org.springframework.boot.autoconfigure.web. ServerProperties has a property port and declares a prefix="server" in its  We can quickly fix this by following the advice in this SO answer by Dave Syer. i.e. we can add the following to WebMvcConfig (note: if you’re using the spring-boot-devtools and it’s still not working after adding this change, try manually re-starting the server):


yes, you can.

this solution is similar to JGrassini, but using EncodedResourceResolver your application will be able to serve already compressed .gz and .br files:

@Configuration
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
            .setOrder(-1)
            .addResourceHandler("/**")
            .addResourceLocations("classpath:/where are your already compressed file to serve/")
            .resourceChain(true)
            .addResolver(new EncodedResourceResolver());
    }
}

you can read about: docs.spring.io

Appendix A. Common application properties, Property contributions can come from additional jar files on your classpath so you COMMON SPRING BOOT PROPERTIES # # This sample file is provided as a of mime types that Tomcat will compress server.tomcat.internal-proxies=10\\. Spring Boot offers a fast way to build applications. It looks at your classpath and at the beans you have configured, makes reasonable assumptions about what you are missing, and adds those items. With Spring Boot, you can focus more on business features and less on infrastructure.


How to serve webpack gzipped file in production using nginx., How to serve webpack gzipped file in production using nginx. But nginx will not picking compressed file by default will serve uncompressed js files. Load Balancing a Spring Boot Application with Nginx (and Docker). spring.jackson.serialization.indent-output doesn't work with Spring Boot 2.3.0.M2 when using Actuator #20211 BuildInfo task not run in Gradle project when project's version number changes #20185 Removal of redundant / from default value for spring.liquibase.change-log may cause the change log to be reapplied on upgrade #20177


Can gzip Compression Really Improve Web Performance , Gzip can compress almost any file type, from plain text to images, and is fast enough This example uses this method to serve pre-compressed CSS files: service marks, and logos are the exclusive property of SolarWinds  This article focuses on caching static assets (such as Javascript and CSS files) when serving them with Spring Boot and Spring MVC. We'll also touch on the concept of “perfect caching”, essentially making sure that – when a file is updated – the old version isn't incorrectly served from the cache.


Coil Spring, Therefore a material and tool which can endure without burning are required. The SMA spring is heat treated after coiling in order to memorize the shape. a suspension works “pre-compressed” around a steady state condition, due to The right hand half-axle houses its own drive shaft and a rubber boot protects the​  Spring Boot automatically configures a suitable CacheManager to serve as a provider for the relevant cache. See the Spring Boot documentation for more detail. Our sample does not use a specific caching library, so our cache store is the simple fallback that uses ConcurrentHashMap .