Quick revision
The short answer
Once a response is committed, its status line and headers have already been sent to the client, so they cannot be changed: the 200 is on the wire, and the error handler's setStatus(500) and JSON body arrive too late. A response commits when its buffer fills, often 8 KB by default in Tomcat, or when someone flushes it, so an error halfway through writing a large body leaves the client with a 200 and part of the body. response.isCommitted() tells us whether changes are still possible, and streaming endpoints need their own error signalling, such as an in-band error record or aborting the connection.
What to remember
In this answer, we saw that a response commits when its buffer, often 8 KB in Tomcat, fills or is flushed, sending the status line and headers, and that after commit setStatus is ignored and sendError or resetBuffer throw, so an error handler cannot turn a sent 200 into a 500. response.isCommitted() tells us whether changes are still possible. We fixed normal endpoints by doing failing work before writing, buffering or sizing the response, and gave streaming endpoints their own error signalling with in-band markers or an aborted connection.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Once a response is committed, its status line and headers have already been sent to the client, so they cannot be changed: the 200 is on the wire, and the error handler's setStatus(500) and JSON body arrive too late. A response commits when its buffer fills, often 8 KB by default in Tomcat, or when someone flushes it, so an error halfway through writing a large body leaves the client with a 200 and part of the body. response.isCommitted() tells us whether changes are still possible, and streaming endpoints need their own error signalling, such as an in-band error record or aborting the connection.
Why this question is asked
This bug is particularly harmful because it lies: monitoring counts a success, the client receives a 200, and the body is half a JSON document that fails to parse somewhere else, much later. Interviewers ask about it to see whether we understand how HTTP responses are actually written by a servlet container, and where error handling can and cannot help. In this answer, we will recap how a response is buffered and committed, reproduce the truncated 200, explain why the handler cannot fix it, and then look at fixes for normal endpoints and for streaming endpoints.
1A Quick Recap: Buffering and Committing
An HTTP response starts with a status line, such as HTTP/1.1 200 OK, then headers, then the body. On the wire, the status and headers must come first. The servlet container lets us set status and headers at any time before the body is sent, by keeping the body in a response buffer. While the bytes we write fit in the buffer, nothing has been sent, and we can still change the status, add headers, or reset the buffer entirely.
The response becomes committed the moment the container starts sending: the status line and headers go out, followed by the buffered body. That happens when the buffer fills, when our code calls flush() on the writer or output stream or calls flushBuffer(), or when the response is completed. The default buffer is commonly 8 KB in Tomcat; response.getBufferSize() tells us the actual size.
- Controller writes body byteskept in the response buffer
- Buffer full or flush() called?YesCommit: send status and headers, then bytesNoKeep buffering; status can still change
- Committedstatus 200 is on the wire;
setStatusis ignored - Error after commitonly the connection or the body can still signal it
4 steps in order. Play them, or step through them one at a time.
2Reproducing the Truncated 200
Here is an endpoint that writes a large JSON array of orders row by row, and fails partway through, plus an error handler that tries to report the failure.
After about 8 KB of output, the buffer filled and the container committed the response with the status that was current at that moment, 200, and the content type. Chunks of body followed. When the exception came at order 12,345, the client already had the status and a large part of the body.
3Why the Error Handler Cannot Fix It
HTTP has no way to take back a status that has been sent. So once the response is committed, the Servlet API treats status and header changes as too late: setStatus and setHeader are silently ignored, while sendError and resetBuffer throw IllegalStateException because they would need to discard what was sent. We can check before trying: response.isCommitted() returns true from the moment of commit.
Spring knows this too. When an exception reaches DispatcherServlet after the response has been committed, Spring can no longer apply the result of an @ExceptionHandler, and it typically logs that the response was already committed (the exact message varies by version). What the client sees then depends on the container: it may end the body where it stopped, so the client gets a well-formed 200 with half a document, or abort the connection, so a careful client notices an incomplete chunked response. In this question, the client got the first case.
4Fixes for Normal Endpoints
For an ordinary endpoint, the cleanest rule is to finish all work that can fail before writing any of the response. In Spring MVC that is the default when a controller returns an object: the message converter serializes the whole value at the end, and an exception thrown while building it in the service happens before anything is written, so the @ExceptionHandler can still return a 500. The trouble starts when we write to the response directly or stream large results.
If a response is moderately large, for example up to a few hundred kilobytes, we can serialize it into memory first and then write it in one go, or raise the buffer with resp.setBufferSize(512 * 1024) before writing anything, so an error during writing still happens before commit. Setting Content-Length when we know it also helps clients, because a body shorter than the declared length is detected as truncated.
5Fixes for Streaming Endpoints
A real export of 5 MB or 5 GB should stream, so it will commit early by design. Then the endpoint needs its own way to signal failure, because the status line cannot. Common options are to validate as much as possible before starting, so most errors become a proper 4xx or 5xx before the first byte; to use a format with in-band error markers, such as newline-delimited JSON where the last record is either a summary with a count or an error record; and, when a clean marker is not possible, to abort the connection rather than end the body normally, so the client sees a failed transfer instead of a plausible but incomplete success.
Notice that the client must be written to check the final record. That is the contract a streaming API has to document, because HTTP status alone cannot carry the result of a long stream.
6Follow-up questions
Can a filter change the status after the controller has written the body?
Only if the response has not been committed yet. A filter that wraps the response with ContentCachingResponseWrapper holds the whole body in memory, so nothing is committed until copyBodyToResponse() is called, and the filter can change the status before that. That costs memory, so it is suitable for small responses only.
Why did monitoring record a 200?
Access logs and metrics record the status that was sent, and the status sent was 200. That is why truncated responses are dangerous: they look like successes everywhere except in the client that tries to parse the body. Counting errors logged after commit, or the final status record in streaming responses, is the way to make them visible.
Does the same problem exist with WebFlux or SSE?
Yes, in the same form. Once headers are written and the first data is sent, an error can only be expressed in the stream, for example an error event in server-sent events, or by terminating the connection.
7Conclusion
In this answer, we saw that a response commits when its buffer, often 8 KB in Tomcat, fills or is flushed, sending the status line and headers, and that after commit setStatus is ignored and sendError or resetBuffer throw, so an error handler cannot turn a sent 200 into a 500. response.isCommitted() tells us whether changes are still possible. We fixed normal endpoints by doing failing work before writing, buffering or sizing the response, and gave streaming endpoints their own error signalling with in-band markers or an aborted connection.
Open with
The response was already committed. The container buffers the body, and while it fits in the buffer the status and headers can still change. But once the buffer fills, which is often 8 KB in Tomcat, or once someone flushes, the container commits the response: it sends the status line and headers, here a 200, and starts sending the body.
Then, if they want more
HTTP cannot take a status back, so after commit setStatus is silently ignored and sendError or resetBuffer throw IllegalStateException. In this case the endpoint wrote a large export directly to the response, the buffer filled and committed a 200, and then an exception occurred halfway through.
The @ExceptionHandler's 500 and JSON body came too late, Spring could only log that the response was already committed, and the container ended the body where it stopped, so the client got a 200 with truncated JSON. I can check response.isCommitted() before trying to change the response.
To fix it for normal endpoints, I make sure all work that can fail happens before anything is written, which is what returning an object from a controller already gives, or I serialize into memory or enlarge the buffer with setBufferSize before writing, and set Content-Length when I can so clients detect truncation.
For real streaming endpoints that commit early by design, I validate up front, and use in-band signalling, such as NDJSON with a final status record that is either complete with a count or an error, and if I cannot write a marker I abort the connection so the client sees a failed transfer rather than a fake success.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Jakarta EE specifications
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "An error handler tries to set a 500 status and JSON body after part of the response has...": https://scrivio-interview-library.netlify.app/java/servlets/5113b7579a/