I have tried every property and solution mentioned here. Nothing works with SpringBoot 2.7.0 for me when running ./gradlew bootRun
.
What I ended up doing was adding a custom filter.
public class Application
{
private static final int DEFAULT_LIMIT = 10 * 1024 * 1024;
public static void main(String[] args)
{
SpringApplication.run(Application.class, args);
}
@Bean
public FilterRegistrationBean<RequestBodyLimitFilter> createRequestBodyLimitFilter(
@Value("${PAYLOAD_LIMIT:" + DEFAULT_LIMIT + "}") Integer limitBytes)
{
FilterRegistrationBean<RequestBodyLimitFilter> registration = new FilterRegistrationBean<>();
registration.setFilter(new RequestBodyLimitFilter(limitBytes));
registration.setName("RequestBodyLimitFilter");
return registration;
}
}
And RequestBodyLimitFilter
:
public class RequestBodyLimitFilter implements Filter
{
private static Logger LOG = Logger.getLogger(RequestBodyLimitFilter.class.getName());
private final Integer limitBytes;
public RequestBodyLimitFilter(Integer limitBytes)
{
this.limitBytes = limitBytes;
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
{
long contentLengthLong = request.getContentLengthLong();
if (contentLengthLong > limitBytes)
{
LOG.info("Received " + contentLengthLong + " limit is " + limitBytes);
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
httpServletResponse.setStatus(HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE);
return;
}
chain.doFilter(request, response);
}
}
```