Configuring Spring Boot's Server, GZip compression, HTTP/2
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.
More resources
You might also wanna check out the following articles on Spring Boot configuration -
- Configuring Spring Boot to use Gson instead of Jackson
- How to use Log4j 2 with Spring Boot
- VFS四大对象之一 struct super_block
- 高性能网站架构方案(二)——优化网站响应时间
- VFS四大对象之二 struct inode
- Ubuntu 安装 Redis
- 高性能网站架构方案(三) ——Varnish加速与Gearman任务分发
- mysql 性能优化方案 (转)
- 《Redis设计与实现》读书笔记(一)——简单动态字符串(SDS)
- Comet:基于 HTTP 长连接的“服务器推”技术
- 编码修炼 | 快速了解Scala技术栈
- VFS四大对象之三 struct dentry
- PHP 排序算法实现讲解
- PHP7新特性介绍
- VFS四大对象之四-struct file
- 《Redis设计与实现》读书笔记(二) ——Redis中的字典(Hash)
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 介绍一下java的基本类型; 每种数据类型的位数(答错了,太菜了)
- 可读代码编写炸鸡十一 - 小黄鸭从你的心里游到脑子里
- 大数据技术之Hadoop(HDFS)第1章 HDFS概述
- Mongodb多键索引之数组
- 使用XtraBackup备份MySQL 8.0 Part 5 对全备份进行恢复
- LeetCode题目32:最长的有效括号
- C++ 万字长文第二篇---拿下字节面试
- mysqldump命令详解 Part 9 --hex-blob 参数的使用
- 使用XtraBackup备份MySQL 8.0 Part 2 XtraBackup权限及配置
- 使用XtraBackup备份MySQL 8.0 Part 6 对数据库进行增量备份
- ArrayList源码阅读笔记
- 3分钟短文 | Laravel表单验证没规则可用?你试试自定义,真香!
- 【C#】DataGridView 数据绑定的一些细节
- 3分钟短文 | Laravel 查询结果检查是不是空,5个方法你别用错!
- 使用XtraBackup备份MySQL 8.0 Part 7 对增量备份进行恢复