Configuring Spring Boot’s Server, GZip compression, HTTP/2, caching and much more
Spring Boot is powerful yet flexible. It tries to auto-configure most of the stuff for you so that you can get up and running quickly with your application.
It uses sensible defaults during auto-configuration but also gives you the flexibility to change those defaults by just tweaking a few things.
In this article, you’ll learn about the most common configuration tweaks that might need to do in your application.
Changing the embedded server in Spring Boot
Spring Boot uses Tomcat as the default embedded server. If you wanna use some other popular server like Jetty or Undertow then you just need to exclude tomcat dependency and add the other server dependency.
1. Using Jetty as the embedded server in Spring Boot
<!-- Exclude tomcat dependency -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Include jetty dependency -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
2. Using undertow as the embedded server in Spring Boot
<!-- Exclude tomcat dependency -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Include undertow dependency -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
Changing the default server port and context path in Spring Boot
By default, Spring Boot runs your application on port 8080
with the context path /
.
If you wanna change the default port and context path, then it’s just a matter of specifying the corresponding values in the application.properties
file –
# HTTP Server port
server.port=8080
# Make the application accessible on the given context path (http://localhost:8080/myapp)
server.servlet.context-path=/myapp
Enabling GZip compression in Spring Boot
GZip compression is a very simple and effective way to save bandwidth and improve the speed of your website.
It reduces the response time of your website by compressing the resources and then sending it over to the clients. It saves bandwidth by at least 50%.
GZip compression is disabled by default in Spring Boot. To enable it, add the following properties to your application.properties
file –
# Enable response compression
server.compression.enabled=true
# The comma-separated list of mime types that should be compressed
server.compression.mime-types=text/html,text/xml,text/plain,text/css,text/javascript,application/javascript,application/json
# Compress the response only if the response size is at least 1KB
server.compression.min-response-size=1024
Note that, GZip compression has a small overhead. Therefore I’ve added a min-response-size
property to tell spring boot server to compress the response only if the size is more than the given value.
Enabling HTTP/2 support in Spring Boot
HTTP/2 is an improvement over the HTTP1 protocol. It improves the page load speed of your website by employing several mechanisms like data compression, server push, multiplexing of multiple requests over a single TCP connection etc.
You can enable HTTP2 in spring boot with the following property, if the server has support for it –
# Enable HTTP/2 support, if the current environment supports it
server.http2.enabled=true
Enabling browser caching of static resources in Spring Boot
Browser caching is another way to improve the page load speed of your website. You can set cache-control
headers to tell browsers to cache static resources until a certain period of time.
Browser caching is disabled by default in Spring Boot. You can enable caching by setting the following properties in the application.properties
file.
# Maximum time the response should be cached (in seconds)
spring.resources.cache.cachecontrol.max-age=120
# The cache must re-validate stale resources with the server. Any expired resources must not be used without re-validating.
spring.resources.cache.cachecontrol.must-revalidate=true
Following are few other cache related properties that you should be aware of –
# The resources are private and intended for a single user. They must not be stored by a shared cache (e.g CDN).
spring.resources.cache.cachecontrol.cache-private= # set a boolean value true/false
# The resources are public and any cache may store the response.
spring.resources.cache.cachecontrol.cache-public= # set a boolean value true/false
Configuring multipart file uploads in Spring Boot
Multipart file uploads are enabled by default in Spring Boot with the following property –
spring.servlet.multipart.enabled=true
But there are few other default multipart properties that you might need to change.
By default, Spring Boot allows you to upload a file with a maximum size of 1MB
. You might need to change this to the desired value as per your requirements.
Following is the complete set of properties –
# Write files to disk if the file size is more than 2KB.
spring.servlet.multipart.file-size-threshold=2KB
# The intermediate disk location where the uploaded files are written
spring.servlet.multipart.location=/tmp
# Maximum file size that can be uploaded
spring.servlet.multipart.max-file-size=50MB
# Maximum allowed multipart request size
spring.servlet.multipart.max-request-size=75MB
Conclusion
In this short article, you learned how to tweak Spring Boot’s default configurations as per your needs. You can find a full index of common application properties on the following official Spring Boot page –
Spring Boot Common application properties
Whenever you need to configure anything in your spring boot application, the first thing you should do is – check the above common application properties page. Most probably, you’ll find a property for configuring the stuff that you’re looking for.
I hope you enjoyed this article. As always, Thanks for reading.