During the development of the SpringBoot project a few days ago, I encountered a startup error:
Failed to bind properties under 'spring.servlet.multipart.max-request-size' to org
After searching for information and troubleshooting, I finally solved this problem, and now I will share it with you.
1. Description of problem symptoms
When starting the SpringBoot project, the console outputs the following error message:
Failed to bind properties under 'spring.servlet.multipart.max-request-size' to org.springframework.boot.autoconfigure.web.servlet.MultipartProperties$FileSize:
Property: spring.servlet.multipart.max-request-size
Value: 10240KB
Reason: The size 10240KB is not in the set of values []
2. The cause of the problem
The reason for this problem is that parameters are set in the application.properties file spring.servlet.multipart.max-request-size
, but the set values are not within the preset range.
spring.servlet.multipart.max-request-size
It is the file upload limit parameter provided by SpringBoot, which is used to limit the size of the uploaded file. It has a default value of 1MB, which can be modified in application.properties. Generally we will set it to a larger value in order to upload large files.
3. Solution
By consulting the official SpringBoot documentation, we found that the default value of this parameter is -1, which means that the file size is not limited. So we can set it to -1 and it will solve the problem.
Add the following configuration to the application.properties file:
spring.servlet.multipart.max-request-size=-1
Or add the following configuration to the application.yml file:
spring:
servlet:
multipart:
max-request-size: -1
After the modification, start the project again and find that the startup is successful.
4. Summary
Through this problem-solving process, we learned about the file upload limit parameters provided by SpringBoot, as well as the possible problems and solutions that may arise when setting this parameter. Hope this sharing can help you.
The complete application.properties file is as follows:
# 应用名称
spring.application.name=demo
# 服务端口
server.port=8080
# 文件上传限制
spring.servlet.multipart.enabled=true
spring.servlet.multipart.max-file-size=512MB
spring.servlet.multipart.max-request-size=-1
# 数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/demo?useSSL=false&serverTimezone=UTC&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# MyBatis配置
mybatis.mapper-locations=classpath:mapper/*.xml