I'm using Spring Boot in one application of mine and I wanted to block payloads that are too big for my application to process, thus, avoiding my app to crash.
As far as I understand I should be able to do this by handling the exception DataBufferLimitException, and that I have done via the snippet below:
@ExceptionHandler(DataBufferLimitException.class)
@ResponseStatus(HttpStatus.FORBIDDEN)
public ErrorResponse handleDataBufferLimitException(DataBufferLimitException ex) {
errorResponse == ###build error response
return errorResponse;
}
I've tested calling this endpoint with a big payload but the exception never gets thrown by spring-boot, even when sending big payloads that end up crashing my app. Also, on the same file that I'm handling this exception, there is the handling of other exceptions, and those are handled as expected.
I have tried to explicitly set a limit for my payload size using those lines on my application.properties file:
server.tomcat.max-http-post-size=1MB
server.tomcat.max-swallow-size=1MB
spring.servlet.multipart.max-request-size=1MB
But that explicit set also seem to be ignored, since the exception never gets thrown.
Here you can find all spring-relevant parts of my pom:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
Am I missing anything here?