use of cn.taketoday.http.ReactiveHttpOutputMessage in project today-infrastructure by TAKETODAY.
the class BodyInserters method fromProducer.
/**
* Inserter to write the given producer of value(s) which must be a {@link Publisher}
* or another producer adaptable to a {@code Publisher} via
* {@link ReactiveAdapterRegistry}.
* <p>Alternatively, consider using the {@code body} shortcuts on
* {@link WebClient WebClient} and
* {@link cn.taketoday.web.reactive.function.server.ServerResponse ServerResponse}.
*
* @param <T> the type of the body
* @param producer the source of body value(s).
* @param elementClass the class of values to be produced
* @return the inserter to write a producer
* @since 4.0
*/
public static <T> BodyInserter<T, ReactiveHttpOutputMessage> fromProducer(T producer, Class<?> elementClass) {
Assert.notNull(producer, "'producer' must not be null");
Assert.notNull(elementClass, "'elementClass' must not be null");
ReactiveAdapter adapter = ReactiveAdapterRegistry.getSharedInstance().getAdapter(producer.getClass());
Assert.notNull(adapter, "'producer' type is unknown to ReactiveAdapterRegistry");
return (message, context) -> writeWithMessageWriters(message, context, producer, ResolvableType.fromClass(elementClass), adapter);
}
use of cn.taketoday.http.ReactiveHttpOutputMessage in project today-infrastructure by TAKETODAY.
the class BodyInsertersTests method ofObject.
@Test
public void ofObject() {
User body = new User("foo", "bar");
BodyInserter<User, ReactiveHttpOutputMessage> inserter = BodyInserters.fromValue(body);
MockServerHttpResponse response = new MockServerHttpResponse();
Mono<Void> result = inserter.insert(response, this.context);
StepVerifier.create(result).expectComplete().verify();
StepVerifier.create(response.getBodyAsString()).expectNext("{\"username\":\"foo\",\"password\":\"bar\"}").expectComplete().verify();
}
use of cn.taketoday.http.ReactiveHttpOutputMessage in project today-infrastructure by TAKETODAY.
the class BodyInsertersTests method ofObjectWithHints.
@Test
public void ofObjectWithHints() {
User body = new User("foo", "bar");
BodyInserter<User, ReactiveHttpOutputMessage> inserter = BodyInserters.fromValue(body);
this.hints.put(JSON_VIEW_HINT, SafeToSerialize.class);
MockServerHttpResponse response = new MockServerHttpResponse();
Mono<Void> result = inserter.insert(response, this.context);
StepVerifier.create(result).expectComplete().verify();
StepVerifier.create(response.getBodyAsString()).expectNext("{\"username\":\"foo\"}").expectComplete().verify();
}
use of cn.taketoday.http.ReactiveHttpOutputMessage in project today-infrastructure by TAKETODAY.
the class BodyInsertersTests method ofPublisher.
@Test
public void ofPublisher() {
Flux<String> body = Flux.just("foo");
BodyInserter<Flux<String>, ReactiveHttpOutputMessage> inserter = BodyInserters.fromPublisher(body, String.class);
MockServerHttpResponse response = new MockServerHttpResponse();
Mono<Void> result = inserter.insert(response, this.context);
StepVerifier.create(result).expectComplete().verify();
StepVerifier.create(response.getBody()).consumeNextWith(buf -> {
String actual = buf.toString(UTF_8);
assertThat(actual).isEqualTo("foo");
}).expectComplete().verify();
}
use of cn.taketoday.http.ReactiveHttpOutputMessage in project today-infrastructure by TAKETODAY.
the class BodyInsertersTests method ofProducerWithMono.
@Test
public void ofProducerWithMono() {
Mono<User> body = Mono.just(new User("foo", "bar"));
BodyInserter<?, ReactiveHttpOutputMessage> inserter = BodyInserters.fromProducer(body, User.class);
MockServerHttpResponse response = new MockServerHttpResponse();
Mono<Void> result = inserter.insert(response, this.context);
StepVerifier.create(result).expectComplete().verify();
StepVerifier.create(response.getBodyAsString()).expectNext("{\"username\":\"foo\",\"password\":\"bar\"}").expectComplete().verify();
}
Aggregations