java
39 lines · 6 steps
Configuring upload size limits in Spring
Cap multipart upload sizes and turn the resulting overflow exception into a clean HTTP 413 response.
Explained by
highlit
1package com.example.uploads.config;
2
3import jakarta.servlet.MultipartConfigElement;
4import org.springframework.boot.web.servlet.MultipartConfigFactory;
5import org.springframework.context.annotation.Bean;
6import org.springframework.context.annotation.Configuration;
7import org.springframework.util.unit.DataSize;
8import org.springframework.web.bind.annotation.ExceptionHandler;
9import org.springframework.web.bind.annotation.RestControllerAdvice;
10import org.springframework.web.multipart.MaxUploadSizeExceededException;
11import org.springframework.http.HttpStatus;
12import org.springframework.http.ProblemDetail;
13
14@Configuration
15public class MultipartConfig {
16
17 @Bean
18 public MultipartConfigElement multipartConfigElement() {
19 MultipartConfigFactory factory = new MultipartConfigFactory();
20 factory.setMaxFileSize(DataSize.ofMegabytes(5));
21 factory.setMaxRequestSize(DataSize.ofMegabytes(15));
22 factory.setFileSizeThreshold(DataSize.ofKilobytes(512));
23 return factory.createMultipartConfig();
24 }
25
26 @RestControllerAdvice
27 static class UploadSizeAdvice {
28
29 @ExceptionHandler(MaxUploadSizeExceededException.class)
30 public ProblemDetail handleOversizedUpload(MaxUploadSizeExceededException ex) {
31 ProblemDetail problem = ProblemDetail.forStatusAndDetail(
32 HttpStatus.PAYLOAD_TOO_LARGE,
33 "Uploaded content exceeds the maximum allowed size.");
34 problem.setTitle("Payload Too Large");
35 problem.setProperty("maxFileSizeBytes", ex.getMaxUploadSize());
36 return problem;
37 }
38 }
39}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Enforcing upload limits at the container level rejects oversized requests before they consume memory or disk.
- 2A @RestControllerAdvice centralizes exception-to-response mapping so every endpoint returns consistent errors.
- 3ProblemDetail gives you a standardized, machine-readable error body with room for custom fields.
Related explainers
java
@Component @Order(Ordered.HIGHEST_PRECEDENCE) public class TenantResolutionFilter extends OncePerRequestFilter {
How a tenant-resolution filter works in Spring
multi-tenancy
servlet-filter
thread-local
Intermediate
8 steps
java
@Repository public interface SubscriptionRepository extends JpaRepository<Subscription, Long> { @Modifying(clearAutomatically = true, flushAutomatically = true)
Bulk JPQL updates in a Spring Data repository
jpql
bulk-update
modifying-query
Intermediate
5 steps
java
public final class Levenshtein { private Levenshtein() { }
Two-row Levenshtein distance in Java
dynamic-programming
edit-distance
space-optimization
Intermediate
8 steps
java
package com.example.lb; import java.util.List; import java.util.concurrent.atomic.AtomicInteger;
A thread-safe round-robin load balancer in Java
concurrency
load-balancing
round-robin
Intermediate
9 steps
java
public interface GitHubClient { @GetExchange("/users/{username}") GitHubUser getUser(@PathVariable String username);
Declarative HTTP clients in Spring
http-client
declarative-api
proxy
Intermediate
8 steps
java
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;
Building a trie for autocomplete in Java
trie
prefix-tree
recursion
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/configuring-upload-size-limits-in-spring-explained-java-efd5/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.