diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/BodyInserters.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/BodyInserters.java
index 7d1bd3cea3f..296680d4282 100644
--- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/BodyInserters.java
+++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/BodyInserters.java
@@ -149,6 +149,9 @@ public abstract class BodyInserters {
/**
* Return a {@code BodyInserter} that writes the given {@code ServerSentEvent} publisher.
+ *
Note that a SSE {@code BodyInserter} can also be obtained by passing a stream of strings
+ * or POJOs (to be encoded as JSON) to {@link #fromPublisher(Publisher, Class)}, and specifying a
+ * {@link MediaType#TEXT_EVENT_STREAM text/event-stream} Content-Type.
* @param eventsPublisher the {@code ServerSentEvent} publisher to write to the response body
* @param the type of the elements contained in the {@link ServerSentEvent}
* @return a {@code BodyInserter} that writes a {@code ServerSentEvent} publisher
@@ -173,66 +176,6 @@ public abstract class BodyInserters {
};
}
- /**
- * Return a {@code BodyInserter} that writes the given {@code Publisher} publisher as
- * Server-Sent Events.
- * @param eventsPublisher the publisher to write to the response body as Server-Sent Events
- * @param eventClass the class of event contained in the publisher
- * @param the type of the elements contained in the publisher
- * @return a {@code BodyInserter} that writes the given {@code Publisher} publisher as
- * Server-Sent Events
- * @see Server-Sent Events W3C recommendation
- */
- // Note that the returned BodyInserter is parameterized to ServerHttpResponse, not
- // ReactiveHttpOutputMessage like other methods, since sending SSEs only typically happens on
- // the server-side
- public static > BodyInserter fromServerSentEvents(S eventsPublisher,
- Class eventClass) {
-
- Assert.notNull(eventsPublisher, "'eventsPublisher' must not be null");
- Assert.notNull(eventClass, "'eventClass' must not be null");
- return fromServerSentEvents(eventsPublisher, ResolvableType.forClass(eventClass));
- }
-
- /**
- * Return a {@code BodyInserter} that writes the given {@code Publisher} publisher as
- * Server-Sent Events.
- * @param eventsPublisher the publisher to write to the response body as Server-Sent Events
- * @param typeReference the type of event contained in the publisher
- * @param the type of the elements contained in the publisher
- * @return a {@code BodyInserter} that writes the given {@code Publisher} publisher as
- * Server-Sent Events
- * @see Server-Sent Events W3C recommendation
- */
- // Note that the returned BodyInserter is parameterized to ServerHttpResponse, not
- // ReactiveHttpOutputMessage like other methods, since sending SSEs only typically happens on
- // the server-side
- public static > BodyInserter fromServerSentEvents(S eventsPublisher,
- ParameterizedTypeReference typeReference) {
-
- Assert.notNull(eventsPublisher, "'eventsPublisher' must not be null");
- Assert.notNull(typeReference, "'typeReference' must not be null");
- return fromServerSentEvents(eventsPublisher,
- ResolvableType.forType(typeReference.getType()));
- }
-
- static > BodyInserter fromServerSentEvents(S eventsPublisher,
- ResolvableType eventType) {
-
- Assert.notNull(eventsPublisher, "'eventsPublisher' must not be null");
- Assert.notNull(eventType, "'eventType' must not be null");
- return (serverResponse, context) -> {
- HttpMessageWriter messageWriter =
- findMessageWriter(context, SERVER_SIDE_EVENT_TYPE, MediaType.TEXT_EVENT_STREAM);
- return context.serverRequest()
- .map(serverRequest -> messageWriter.write(eventsPublisher, eventType,
- eventType, MediaType.TEXT_EVENT_STREAM, serverRequest,
- serverResponse, context.hints()))
- .orElseGet(() -> messageWriter.write(eventsPublisher, eventType,
- MediaType.TEXT_EVENT_STREAM, serverResponse, context.hints()));
- };
- }
-
/**
* Return a {@code BodyInserter} that writes the given {@code MultiValueMap} as URL-encoded
* form data.
@@ -241,7 +184,7 @@ public abstract class BodyInserters {
*/
// Note that the returned BodyInserter is parameterized to ClientHttpRequest, not
// ReactiveHttpOutputMessage like other methods, since sending form data only typically happens
- // on the server-side
+ // on the client-side
public static BodyInserter, ClientHttpRequest> fromFormData(
MultiValueMap formData) {
@@ -262,7 +205,7 @@ public abstract class BodyInserters {
*/
// Note that the returned BodyInserter is parameterized to ClientHttpRequest, not
// ReactiveHttpOutputMessage like other methods, since sending form data only typically happens
- // on the server-side
+ // on the client-side
public static BodyInserter, ClientHttpRequest> fromMultipartData(
MultiValueMap multipartData) {
diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/BodyInsertersTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/BodyInsertersTests.java
index 90b5bf4e2eb..33e77bb1eeb 100644
--- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/BodyInsertersTests.java
+++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/BodyInsertersTests.java
@@ -247,17 +247,6 @@ public class BodyInsertersTests {
StepVerifier.create(result).expectNextCount(0).expectComplete().verify();
}
- @Test
- public void ofServerSentEventClass() throws Exception {
- Flux body = Flux.just("foo");
- BodyInserter, ServerHttpResponse> inserter =
- BodyInserters.fromServerSentEvents(body, String.class);
-
- MockServerHttpResponse response = new MockServerHttpResponse();
- Mono result = inserter.insert(response, this.context);
- StepVerifier.create(result).expectNextCount(0).expectComplete().verify();
- }
-
@Test
public void ofFormData() throws Exception {
MultiValueMap body = new LinkedMultiValueMap<>();
diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/SseHandlerFunctionIntegrationTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/SseHandlerFunctionIntegrationTests.java
index fbf577deb22..356ac87c654 100644
--- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/SseHandlerFunctionIntegrationTests.java
+++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/SseHandlerFunctionIntegrationTests.java
@@ -25,6 +25,7 @@ import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import org.springframework.core.ParameterizedTypeReference;
+import org.springframework.http.MediaType;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.reactive.function.client.WebClient;
@@ -120,13 +121,17 @@ public class SseHandlerFunctionIntegrationTests extends AbstractRouterFunctionIn
public Mono string(ServerRequest request) {
Flux flux = Flux.interval(Duration.ofMillis(100)).map(l -> "foo " + l).take(2);
- return ServerResponse.ok().body(fromServerSentEvents(flux, String.class));
+ return ServerResponse.ok()
+ .contentType(MediaType.TEXT_EVENT_STREAM)
+ .body(flux, String.class);
}
public Mono person(ServerRequest request) {
Flux flux = Flux.interval(Duration.ofMillis(100))
.map(l -> new Person("foo " + l)).take(2);
- return ServerResponse.ok().body(fromServerSentEvents(flux, Person.class));
+ return ServerResponse.ok()
+ .contentType(MediaType.TEXT_EVENT_STREAM)
+ .body(flux, Person.class);
}
public Mono sse(ServerRequest request) {