Skip to content

Commit 0b3ea40

Browse files
committed
Remove BodyInserters.fromServerSentEvent variants
Removed superfluous `fromServerSentEvent` variants from `BodyInserters`, as their functionality can also be obtained by passing a stream of strings or POJOs (to be encoded as JSON) to `fromPublisher(Publisher, Class)}`, and specifying a `text/event-stream` Content-Type. Issue: SPR-15826
1 parent dbe25cf commit 0b3ea40

File tree

3 files changed

+12
-75
lines changed

3 files changed

+12
-75
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/function/BodyInserters.java

Lines changed: 5 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ private static HttpMessageWriter<Resource> resourceHttpMessageWriter(BodyInserte
149149

150150
/**
151151
* Return a {@code BodyInserter} that writes the given {@code ServerSentEvent} publisher.
152+
* <p>Note that a SSE {@code BodyInserter} can also be obtained by passing a stream of strings
153+
* or POJOs (to be encoded as JSON) to {@link #fromPublisher(Publisher, Class)}, and specifying a
154+
* {@link MediaType#TEXT_EVENT_STREAM text/event-stream} Content-Type.
152155
* @param eventsPublisher the {@code ServerSentEvent} publisher to write to the response body
153156
* @param <T> the type of the elements contained in the {@link ServerSentEvent}
154157
* @return a {@code BodyInserter} that writes a {@code ServerSentEvent} publisher
@@ -173,66 +176,6 @@ public static <T, S extends Publisher<ServerSentEvent<T>>> BodyInserter<S, Serve
173176
};
174177
}
175178

176-
/**
177-
* Return a {@code BodyInserter} that writes the given {@code Publisher} publisher as
178-
* Server-Sent Events.
179-
* @param eventsPublisher the publisher to write to the response body as Server-Sent Events
180-
* @param eventClass the class of event contained in the publisher
181-
* @param <T> the type of the elements contained in the publisher
182-
* @return a {@code BodyInserter} that writes the given {@code Publisher} publisher as
183-
* Server-Sent Events
184-
* @see <a href="https://www.w3.org/TR/eventsource/">Server-Sent Events W3C recommendation</a>
185-
*/
186-
// Note that the returned BodyInserter is parameterized to ServerHttpResponse, not
187-
// ReactiveHttpOutputMessage like other methods, since sending SSEs only typically happens on
188-
// the server-side
189-
public static <T, S extends Publisher<T>> BodyInserter<S, ServerHttpResponse> fromServerSentEvents(S eventsPublisher,
190-
Class<T> eventClass) {
191-
192-
Assert.notNull(eventsPublisher, "'eventsPublisher' must not be null");
193-
Assert.notNull(eventClass, "'eventClass' must not be null");
194-
return fromServerSentEvents(eventsPublisher, ResolvableType.forClass(eventClass));
195-
}
196-
197-
/**
198-
* Return a {@code BodyInserter} that writes the given {@code Publisher} publisher as
199-
* Server-Sent Events.
200-
* @param eventsPublisher the publisher to write to the response body as Server-Sent Events
201-
* @param typeReference the type of event contained in the publisher
202-
* @param <T> the type of the elements contained in the publisher
203-
* @return a {@code BodyInserter} that writes the given {@code Publisher} publisher as
204-
* Server-Sent Events
205-
* @see <a href="https://www.w3.org/TR/eventsource/">Server-Sent Events W3C recommendation</a>
206-
*/
207-
// Note that the returned BodyInserter is parameterized to ServerHttpResponse, not
208-
// ReactiveHttpOutputMessage like other methods, since sending SSEs only typically happens on
209-
// the server-side
210-
public static <T, S extends Publisher<T>> BodyInserter<S, ServerHttpResponse> fromServerSentEvents(S eventsPublisher,
211-
ParameterizedTypeReference<T> typeReference) {
212-
213-
Assert.notNull(eventsPublisher, "'eventsPublisher' must not be null");
214-
Assert.notNull(typeReference, "'typeReference' must not be null");
215-
return fromServerSentEvents(eventsPublisher,
216-
ResolvableType.forType(typeReference.getType()));
217-
}
218-
219-
static <T, S extends Publisher<T>> BodyInserter<S, ServerHttpResponse> fromServerSentEvents(S eventsPublisher,
220-
ResolvableType eventType) {
221-
222-
Assert.notNull(eventsPublisher, "'eventsPublisher' must not be null");
223-
Assert.notNull(eventType, "'eventType' must not be null");
224-
return (serverResponse, context) -> {
225-
HttpMessageWriter<T> messageWriter =
226-
findMessageWriter(context, SERVER_SIDE_EVENT_TYPE, MediaType.TEXT_EVENT_STREAM);
227-
return context.serverRequest()
228-
.map(serverRequest -> messageWriter.write(eventsPublisher, eventType,
229-
eventType, MediaType.TEXT_EVENT_STREAM, serverRequest,
230-
serverResponse, context.hints()))
231-
.orElseGet(() -> messageWriter.write(eventsPublisher, eventType,
232-
MediaType.TEXT_EVENT_STREAM, serverResponse, context.hints()));
233-
};
234-
}
235-
236179
/**
237180
* Return a {@code BodyInserter} that writes the given {@code MultiValueMap} as URL-encoded
238181
* form data.
@@ -241,7 +184,7 @@ static <T, S extends Publisher<T>> BodyInserter<S, ServerHttpResponse> fromServe
241184
*/
242185
// Note that the returned BodyInserter is parameterized to ClientHttpRequest, not
243186
// ReactiveHttpOutputMessage like other methods, since sending form data only typically happens
244-
// on the server-side
187+
// on the client-side
245188
public static BodyInserter<MultiValueMap<String, String>, ClientHttpRequest> fromFormData(
246189
MultiValueMap<String, String> formData) {
247190

@@ -262,7 +205,7 @@ public static BodyInserter<MultiValueMap<String, String>, ClientHttpRequest> fro
262205
*/
263206
// Note that the returned BodyInserter is parameterized to ClientHttpRequest, not
264207
// ReactiveHttpOutputMessage like other methods, since sending form data only typically happens
265-
// on the server-side
208+
// on the client-side
266209
public static BodyInserter<MultiValueMap<String, ?>, ClientHttpRequest> fromMultipartData(
267210
MultiValueMap<String, ?> multipartData) {
268211

spring-webflux/src/test/java/org/springframework/web/reactive/function/BodyInsertersTests.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -247,17 +247,6 @@ public void ofServerSentEventFlux() throws Exception {
247247
StepVerifier.create(result).expectNextCount(0).expectComplete().verify();
248248
}
249249

250-
@Test
251-
public void ofServerSentEventClass() throws Exception {
252-
Flux<String> body = Flux.just("foo");
253-
BodyInserter<Flux<String>, ServerHttpResponse> inserter =
254-
BodyInserters.fromServerSentEvents(body, String.class);
255-
256-
MockServerHttpResponse response = new MockServerHttpResponse();
257-
Mono<Void> result = inserter.insert(response, this.context);
258-
StepVerifier.create(result).expectNextCount(0).expectComplete().verify();
259-
}
260-
261250
@Test
262251
public void ofFormData() throws Exception {
263252
MultiValueMap<String, String> body = new LinkedMultiValueMap<>();

spring-webflux/src/test/java/org/springframework/web/reactive/function/server/SseHandlerFunctionIntegrationTests.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import reactor.test.StepVerifier;
2626

2727
import org.springframework.core.ParameterizedTypeReference;
28+
import org.springframework.http.MediaType;
2829
import org.springframework.http.codec.ServerSentEvent;
2930
import org.springframework.web.reactive.function.client.WebClient;
3031

@@ -120,13 +121,17 @@ private static class SseHandler {
120121

121122
public Mono<ServerResponse> string(ServerRequest request) {
122123
Flux<String> flux = Flux.interval(Duration.ofMillis(100)).map(l -> "foo " + l).take(2);
123-
return ServerResponse.ok().body(fromServerSentEvents(flux, String.class));
124+
return ServerResponse.ok()
125+
.contentType(MediaType.TEXT_EVENT_STREAM)
126+
.body(flux, String.class);
124127
}
125128

126129
public Mono<ServerResponse> person(ServerRequest request) {
127130
Flux<Person> flux = Flux.interval(Duration.ofMillis(100))
128131
.map(l -> new Person("foo " + l)).take(2);
129-
return ServerResponse.ok().body(fromServerSentEvents(flux, Person.class));
132+
return ServerResponse.ok()
133+
.contentType(MediaType.TEXT_EVENT_STREAM)
134+
.body(flux, Person.class);
130135
}
131136

132137
public Mono<ServerResponse> sse(ServerRequest request) {

0 commit comments

Comments
 (0)