0

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?

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • What sort of payload are you talking about? Have you explicitly set a maximum size or are you relying on existing defaults? – Deadron Feb 02 '21 at 15:02
  • That code is used to render the response when a `DataBufferLimitException` is thrown, it does *not* cause the exception to be thrown. – Andreas Feb 02 '21 at 15:30
  • Does this answer your question? [Increase HTTP Post maxPostSize in Spring Boot](https://stackoverflow.com/q/33232849/5221149) – Andreas Feb 02 '21 at 15:31
  • @Andreas I understand it does not cause the exception to be thrown. What I'm trying to do to make sure it gets thrown is to call my endpoint with a gigantic POST JSON Payload. I have 1GB of memory reserved to this app, but when I send this big payload, it consumes the whole app memory before the exception is thrown, causing my app to crash. – Lucas Schneider Feb 02 '21 at 15:59
  • So if you know the exception hasn't been thrown (yet), what was the point of showing us that irrelevant code, that only becomes relevant once the exception *is* thrown? – Andreas Feb 02 '21 at 16:01
  • @Deadron: I'm talkin about a POST JSON payload. I have tried both: setting an explicit maximum size and relying on the default, both of them do not throw the exception. As said on the other comment, the memory of my app is fully consumed before the exception is thrown. But as far as I know, the default limit is 2MB. – Lucas Schneider Feb 02 '21 at 16:02
  • How did you set an explicit maximum size? You don't show, and perhaps you did it wrong. – Andreas Feb 02 '21 at 16:03
  • @Andreas: I was expecting that that code would handle the exception when it does get thrown. The problem is that it does not get thrown when I was expecting it to be. – Lucas Schneider Feb 02 '21 at 16:03
  • Your question doesn't say that, so it's very unclear what you're asking. Perhaps you should take another look at what you're asking and **clarify** it. See: [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – Andreas Feb 02 '21 at 16:05
  • @Andreas I have tried it with these entries on application.properties: server.tomcat.max-http-post-size=1MB server.tomcat.max-swallow-size=1MB spring.servlet.multipart.max-request-size=10MB – Lucas Schneider Feb 02 '21 at 16:06
  • Don't say in a comment. Edit the question and clarify it. – Andreas Feb 02 '21 at 16:06
  • @Andreas I believe I've made it clearer on the question. – Lucas Schneider Feb 02 '21 at 16:11
  • If you let the servlet container enforce a max payload size I do not believe you can manage the result yourself. The request will get rejected before your handler is invoked. – Deadron Feb 02 '21 at 17:50

0 Answers0