HTTP 408 Request Timeout in JHipster Spring MVC: Intercepting Container‑Generated Timeouts
0 reputation · 29 Sept 2024, 12:15 UTC
0 reputation · 29 Sept 2024, 12:15 UTC
To enable graceful handling of HTTP 408 Request Timeout responses in a JHipster‑generated Spring MVC application by ensuring that a global @ControllerAdvice can capture, log, or transform the error before the client receives it.
JHipster relies solely on the servlet container’s server.connection-timeout setting; the container issues a 408 response before the request reaches any controller or advice. The generated code provides no cancellation token or per‑endpoint timeout configuration.
Whether to:
ResponseStatusException for global handling.@ControllerAdvice to process it?33275 reputation · 30 Sept 2024, 00:13 UTC
In a standard JHipster Spring MVC architecture, it is not possible to intercept an HTTP 408 Request Timeout using @ControllerAdvice or a Servlet Filter if the timeout is triggered by the servlet container (e.g., Tomcat) via server.connection-timeout.
This is because the container generates the 408 response at the connector level. The connection is terminated before the request is ever dispatched to the DispatcherServlet, meaning the Spring context—and by extension, your global exception handlers—is never invoked for that request cycle.
To achieve graceful handling and logging of timeouts, you must shift the timeout responsibility from the container to the application layer. The recommended pattern in Spring MVC is using Asynchronous Request Processing.
DeferredResult<T>.DeferredResult instance.onTimeout callback to throw a specific exception or return a custom error object.@GetMapping("/long-process")
public DeferredResult<ResponseEntity<String>> handleLongProcess() {
DeferredResult<ResponseEntity<String>> output = new DeferredResult<>(5000L);
output.onTimeout(() -> {
// This allows @ControllerAdvice to catch the resulting exception
throw new ResponseStatusException(HttpStatus.REQUEST_TIMEOUT, "Endpoint timeout exceeded");
});
// Process logic asynchronously
return output;
}
| Approach | Effectiveness | Trade-off |
|---|---|---|
| Custom Annotation + Aspect | Moderate | Requires an underlying async mechanism (like CompletableFuture) to actually enforce the clock; otherwise, it is just metadata. |
| Spring WebFlux | High | Requires a full migration from Spring MVC to a reactive stack to use .timeout() operators. |
| Servlet Filter | Low | Cannot catch 408s generated by the container's connector. |
To verify this behavior, set server.connection-timeout=100ms in your properties. Attempt a slow request; you will observe the 408 response in the browser without any corresponding logs in your @ControllerAdvice. Transitioning the endpoint to DeferredResult will allow the application to regain control.
Diagnostic Requirement: Please specify if these timeouts are occurring during the initial request upload (connection phase) or during long-running server processing, as the latter can be solved with the async patterns described above.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.