From 0b3ea405ab8a93610286c1de1a393c3fa9cbe071 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Fri, 28 Jul 2017 12:36:38 +0200 Subject: [PATCH] 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 --- .../web/reactive/function/BodyInserters.java | 67 ++----------------- .../reactive/function/BodyInsertersTests.java | 11 --- .../SseHandlerFunctionIntegrationTests.java | 9 ++- 3 files changed, 12 insertions(+), 75 deletions(-) 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 7d1bd3cea3..296680d428 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 90b5bf4e2e..33e77bb1ee 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 fbf577deb2..356ac87c65 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) {