Configuring Spring Boot’s Server
Changing the Embedded Server in Spring Boot
Spring Boot is a powerful yet flexible framework that tries to auto-configure most of the stuff for you, allowing you to get up and running quickly with your application. However, there are times when you need to tweak the default configurations to suit your needs. In this article, we’ll explore the most common configuration tweaks that you might need to do in your application.
Using Jetty as the Embedded Server in Spring Boot
By default, Spring Boot uses Tomcat as the embedded server. If you want to use a different server like Jetty or Undertow, you can exclude the Tomcat dependency and add the other server dependency to your project.
To use Jetty as the embedded server, you need to exclude the Tomcat dependency and include the Jetty dependency in your project.
<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>
<Dependency>
<GroupId>org.springframework.boot</GroupId>
<ArtifactId>spring-boot-starter-jetty</ArtifactId>
</Dependency>
Using Undertow as the Embedded Server in Spring Boot
Similarly, to use Undertow as the embedded server, you need to exclude the Tomcat dependency and include the Undertow dependency in your project.
<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>
<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 want to change the default port and context path, you can specify 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 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. To enable GZip compression in Spring Boot, you need to 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
Enabling HTTP/2 Support in Spring Boot
HTTP/2 is an improvement over the HTTP/1 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. To enable HTTP/2 support in Spring Boot, you need to add the following property to your application.properties file.
# 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 speed of loading your page. By setting cache-control headers, you can tell browsers to cache static resources for a certain period of time. To enable browser caching of static resources in Spring Boot, you need to add the following properties to your 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
Configuring Multipart File Uploads in Spring Boot
Multipart file uploads are enabled by default in Spring Boot. However, there are few other default multipart properties that you might need to change. To configure multipart file uploads in Spring Boot, you need to add the following properties to your application.properties file.
# 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 sizes
spring.servlet.multipart.max-request-size = 75MB
Conclusion
In this article, we’ve explored the most common configuration tweaks that you might need to do in your Spring Boot application. We’ve covered changing the embedded server, default server port and context path, enabling GZip compression, HTTP/2 support, browser caching of static resources, and configuring multipart file uploads. Whenever you need to configure anything in your Spring Boot application, the first thing you should do is check the official Spring Boot page for common application properties.