@@ -66,13 +66,16 @@ public class ResponseBodyEmitter {
66
66
@ Nullable
67
67
private final Long timeout ;
68
68
69
- private final Set <DataWithMediaType > earlySendAttempts = new LinkedHashSet <>(8 );
70
-
71
69
@ Nullable
72
70
private Handler handler ;
73
71
72
+ /** Store send data before handler is initialized */
73
+ private final Set <DataWithMediaType > earlySendAttempts = new LinkedHashSet <>(8 );
74
+
75
+ /** Store complete invocation before handler is initialized */
74
76
private boolean complete ;
75
77
78
+ /** Store completeWithError invocation before handler is initialized */
76
79
@ Nullable
77
80
private Throwable failure ;
78
81
@@ -147,6 +150,11 @@ protected void extendResponse(ServerHttpResponse outputMessage) {
147
150
* Write the given object to the response.
148
151
* <p>If any exception occurs a dispatch is made back to the app server where
149
152
* Spring MVC will pass the exception through its exception handling mechanism.
153
+ * <p><strong>Note:</strong> if the send fails with an IOException, you do
154
+ * not need to call {@link #completeWithError(Throwable)} in order to clean
155
+ * up. Instead the Servlet container creates a notification that results in a
156
+ * dispatch where Spring MVC invokes exception resolvers and completes
157
+ * processing.
150
158
* @param object the object to write
151
159
* @throws IOException raised when an I/O error occurs
152
160
* @throws java.lang.IllegalStateException wraps any other errors
@@ -156,9 +164,8 @@ public void send(Object object) throws IOException {
156
164
}
157
165
158
166
/**
159
- * Write the given object to the response also using a MediaType hint.
160
- * <p>If any exception occurs a dispatch is made back to the app server where
161
- * Spring MVC will pass the exception through its exception handling mechanism.
167
+ * Overloaded variant of {@link #send(Object)} that also accepts a MediaType
168
+ * hint for how to serialize the given Object.
162
169
* @param object the object to write
163
170
* @param mediaType a MediaType hint for selecting an HttpMessageConverter
164
171
* @throws IOException raised when an I/O error occurs
@@ -187,13 +194,12 @@ private void sendInternal(Object object, @Nullable MediaType mediaType) throws I
187
194
}
188
195
189
196
/**
190
- * Complete request processing.
191
- * <p>A dispatch is made into the app server where Spring MVC completes
192
- * asynchronous request processing.
193
- * <p><strong>Note:</strong> you do not need to call this method after an
194
- * {@link IOException} from any of the {@code send} methods. The Servlet
195
- * container will generate an error notification that Spring MVC will process
196
- * and handle through the exception resolver mechanism and then complete.
197
+ * Complete request processing by performing a dispatch into the servlet
198
+ * container, where Spring MVC is invoked once more, and completes the
199
+ * request processing lifecycle.
200
+ * <p><strong>Note:</strong> this method should be called by the application
201
+ * to complete request processing. It should not be used after container
202
+ * related events such as an error while {@link #send(Object) sending}.
197
203
*/
198
204
public synchronized void complete () {
199
205
this .complete = true ;
@@ -205,7 +211,13 @@ public synchronized void complete() {
205
211
/**
206
212
* Complete request processing with an error.
207
213
* <p>A dispatch is made into the app server where Spring MVC will pass the
208
- * exception through its exception handling mechanism.
214
+ * exception through its exception handling mechanism. Note however that
215
+ * at this stage of request processing, the response is committed and the
216
+ * response status can no longer be changed.
217
+ * <p><strong>Note:</strong> this method should be called by the application
218
+ * to complete request processing with an error. It should not be used after
219
+ * container related events such as an error while
220
+ * {@link #send(Object) sending}.
209
221
*/
210
222
public synchronized void completeWithError (Throwable ex ) {
211
223
this .complete = true ;
0 commit comments