Support conditional streaming with ResponseEntity<?>

Closes gh-35153
This commit is contained in:
rstoyanchev
2025-07-09 12:44:01 +01:00
parent 3dc22379a0
commit 9670388e0c
4 changed files with 129 additions and 7 deletions
@@ -51,6 +51,7 @@ import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.context.request.async.AsyncRequestNotUsableException;
@@ -205,6 +206,21 @@ class RequestMappingHandlerAdapterTests {
assertMethodProcessorCount(RESOLVER_COUNT, INIT_BINDER_RESOLVER_COUNT, 1);
}
@Test // gh-35153
void responseEntityWithWildCardAndConditionalStream() throws Exception {
HandlerMethod handlerMethod = handlerMethod(new SseController(), "handle", String.class);
this.handlerAdapter.afterPropertiesSet();
this.request.setAsyncSupported(true);
this.request.addParameter("q", "sse");
this.handlerAdapter.handle(this.request, this.response, handlerMethod);
assertThat(this.response.getStatus()).isEqualTo(200);
assertThat(this.response.getHeader("Content-Type")).isEqualTo("text/event-stream");
assertThat(this.response.getContentAsString()).isEqualTo("data:event 1\n\ndata:event 2\n\n");
}
@Test
void modelAttributeAdvice() throws Exception {
this.webAppContext.registerSingleton("maa", ModelAttributeAdvice.class);
@@ -377,6 +393,22 @@ class RequestMappingHandlerAdapterTests {
}
private static class SseController {
public ResponseEntity<?> handle(@RequestParam String q) throws IOException {
if (q.equals("sse")) {
SseEmitter emitter = new SseEmitter();
emitter.send("event 1");
emitter.send("event 2");
emitter.complete();
return ResponseEntity.ok().body(emitter);
}
return ResponseEntity.ok("text");
}
}
@ControllerAdvice
private static class ModelAttributeAdvice {