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
This commit is contained in:
parent
dbe25cf717
commit
0b3ea405ab
|
@ -149,6 +149,9 @@ public abstract class BodyInserters {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a {@code BodyInserter} that writes the given {@code ServerSentEvent} publisher.
|
* Return a {@code BodyInserter} that writes the given {@code ServerSentEvent} publisher.
|
||||||
|
* <p>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 eventsPublisher the {@code ServerSentEvent} publisher to write to the response body
|
||||||
* @param <T> the type of the elements contained in the {@link ServerSentEvent}
|
* @param <T> the type of the elements contained in the {@link ServerSentEvent}
|
||||||
* @return a {@code BodyInserter} that writes a {@code ServerSentEvent} publisher
|
* @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 <T> 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 <a href="https://www.w3.org/TR/eventsource/">Server-Sent Events W3C recommendation</a>
|
|
||||||
*/
|
|
||||||
// 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 <T, S extends Publisher<T>> BodyInserter<S, ServerHttpResponse> fromServerSentEvents(S eventsPublisher,
|
|
||||||
Class<T> 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 <T> 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 <a href="https://www.w3.org/TR/eventsource/">Server-Sent Events W3C recommendation</a>
|
|
||||||
*/
|
|
||||||
// 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 <T, S extends Publisher<T>> BodyInserter<S, ServerHttpResponse> fromServerSentEvents(S eventsPublisher,
|
|
||||||
ParameterizedTypeReference<T> 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 <T, S extends Publisher<T>> BodyInserter<S, ServerHttpResponse> 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<T> 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
|
* Return a {@code BodyInserter} that writes the given {@code MultiValueMap} as URL-encoded
|
||||||
* form data.
|
* form data.
|
||||||
|
@ -241,7 +184,7 @@ public abstract class BodyInserters {
|
||||||
*/
|
*/
|
||||||
// Note that the returned BodyInserter is parameterized to ClientHttpRequest, not
|
// Note that the returned BodyInserter is parameterized to ClientHttpRequest, not
|
||||||
// ReactiveHttpOutputMessage like other methods, since sending form data only typically happens
|
// ReactiveHttpOutputMessage like other methods, since sending form data only typically happens
|
||||||
// on the server-side
|
// on the client-side
|
||||||
public static BodyInserter<MultiValueMap<String, String>, ClientHttpRequest> fromFormData(
|
public static BodyInserter<MultiValueMap<String, String>, ClientHttpRequest> fromFormData(
|
||||||
MultiValueMap<String, String> formData) {
|
MultiValueMap<String, String> formData) {
|
||||||
|
|
||||||
|
@ -262,7 +205,7 @@ public abstract class BodyInserters {
|
||||||
*/
|
*/
|
||||||
// Note that the returned BodyInserter is parameterized to ClientHttpRequest, not
|
// Note that the returned BodyInserter is parameterized to ClientHttpRequest, not
|
||||||
// ReactiveHttpOutputMessage like other methods, since sending form data only typically happens
|
// ReactiveHttpOutputMessage like other methods, since sending form data only typically happens
|
||||||
// on the server-side
|
// on the client-side
|
||||||
public static BodyInserter<MultiValueMap<String, ?>, ClientHttpRequest> fromMultipartData(
|
public static BodyInserter<MultiValueMap<String, ?>, ClientHttpRequest> fromMultipartData(
|
||||||
MultiValueMap<String, ?> multipartData) {
|
MultiValueMap<String, ?> multipartData) {
|
||||||
|
|
||||||
|
|
|
@ -247,17 +247,6 @@ public class BodyInsertersTests {
|
||||||
StepVerifier.create(result).expectNextCount(0).expectComplete().verify();
|
StepVerifier.create(result).expectNextCount(0).expectComplete().verify();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void ofServerSentEventClass() throws Exception {
|
|
||||||
Flux<String> body = Flux.just("foo");
|
|
||||||
BodyInserter<Flux<String>, ServerHttpResponse> inserter =
|
|
||||||
BodyInserters.fromServerSentEvents(body, String.class);
|
|
||||||
|
|
||||||
MockServerHttpResponse response = new MockServerHttpResponse();
|
|
||||||
Mono<Void> result = inserter.insert(response, this.context);
|
|
||||||
StepVerifier.create(result).expectNextCount(0).expectComplete().verify();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void ofFormData() throws Exception {
|
public void ofFormData() throws Exception {
|
||||||
MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
|
MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
|
||||||
|
|
|
@ -25,6 +25,7 @@ import reactor.core.publisher.Mono;
|
||||||
import reactor.test.StepVerifier;
|
import reactor.test.StepVerifier;
|
||||||
|
|
||||||
import org.springframework.core.ParameterizedTypeReference;
|
import org.springframework.core.ParameterizedTypeReference;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.codec.ServerSentEvent;
|
import org.springframework.http.codec.ServerSentEvent;
|
||||||
import org.springframework.web.reactive.function.client.WebClient;
|
import org.springframework.web.reactive.function.client.WebClient;
|
||||||
|
|
||||||
|
@ -120,13 +121,17 @@ public class SseHandlerFunctionIntegrationTests extends AbstractRouterFunctionIn
|
||||||
|
|
||||||
public Mono<ServerResponse> string(ServerRequest request) {
|
public Mono<ServerResponse> string(ServerRequest request) {
|
||||||
Flux<String> flux = Flux.interval(Duration.ofMillis(100)).map(l -> "foo " + l).take(2);
|
Flux<String> 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<ServerResponse> person(ServerRequest request) {
|
public Mono<ServerResponse> person(ServerRequest request) {
|
||||||
Flux<Person> flux = Flux.interval(Duration.ofMillis(100))
|
Flux<Person> flux = Flux.interval(Duration.ofMillis(100))
|
||||||
.map(l -> new Person("foo " + l)).take(2);
|
.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<ServerResponse> sse(ServerRequest request) {
|
public Mono<ServerResponse> sse(ServerRequest request) {
|
||||||
|
|
Loading…
Reference in New Issue