Java Code Examples for org.springframework.web.reactive.function.BodyInserters#fromPublisher()

The following examples show how to use org.springframework.web.reactive.function.BodyInserters#fromPublisher() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultClientRequestBuilder.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public <S, P extends Publisher<S>> ClientRequest.Builder body(
		P publisher, ParameterizedTypeReference<S> typeReference) {

	this.body = BodyInserters.fromPublisher(publisher, typeReference);
	return this;
}
 
Example 2
Source File: DefaultWebClient.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public <T, P extends Publisher<T>> RequestHeadersSpec<?> body(
		P publisher, ParameterizedTypeReference<T> typeReference) {

	this.inserter = BodyInserters.fromPublisher(publisher, typeReference);
	return this;
}
 
Example 3
Source File: DefaultClientRequestBuilder.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public <S, P extends Publisher<S>> ClientRequest.Builder body(
		P publisher, ParameterizedTypeReference<S> typeReference) {

	this.body = BodyInserters.fromPublisher(publisher, typeReference);
	return this;
}
 
Example 4
Source File: DefaultWebClient.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public <T, P extends Publisher<T>> RequestHeadersSpec<?> body(
		P publisher, ParameterizedTypeReference<T> typeReference) {

	this.inserter = BodyInserters.fromPublisher(publisher, typeReference);
	return this;
}
 
Example 5
Source File: WebClientController.java    From tutorials with MIT License 5 votes vote down vote up
public void demonstrateWebClient() {
    // request
    WebClient.UriSpec<WebClient.RequestBodySpec> request1 = createWebClientWithServerURLAndDefaultValues().method(HttpMethod.POST);
    WebClient.UriSpec<WebClient.RequestBodySpec> request2 = createWebClientWithServerURLAndDefaultValues().post();

    // request body specifications
    WebClient.RequestBodySpec uri1 = createWebClientWithServerURLAndDefaultValues().method(HttpMethod.POST)
        .uri("/resource");
    WebClient.RequestBodySpec uri2 = createWebClientWithServerURLAndDefaultValues().post()
        .uri(URI.create("/resource"));

    // request header specification
    WebClient.RequestHeadersSpec<?> requestSpec1 = uri1.body(BodyInserters.fromPublisher(Mono.just("data"), String.class));
    WebClient.RequestHeadersSpec<?> requestSpec2 = uri2.body(BodyInserters.fromObject("data"));

    // inserters
    BodyInserter<Publisher<String>, ReactiveHttpOutputMessage> inserter1 = BodyInserters
            .fromPublisher(Subscriber::onComplete, String.class);

    LinkedMultiValueMap<String, String> map = new LinkedMultiValueMap<>();
    map.add("key1", "value1");
    map.add("key2", "value2");

    // BodyInserter<MultiValueMap<String, ?>, ClientHttpRequest> inserter2 = BodyInserters.fromMultipartData(map);
    BodyInserter<String, ReactiveHttpOutputMessage> inserter3 = BodyInserters.fromObject("body");

    // responses
    WebClient.ResponseSpec response1 = uri1.body(inserter3)
        .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
        .accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML)
        .acceptCharset(Charset.forName("UTF-8"))
        .ifNoneMatch("*")
        .ifModifiedSince(ZonedDateTime.now())
        .retrieve();
    WebClient.ResponseSpec response2 = requestSpec2.retrieve();

}
 
Example 6
Source File: DefaultClientRequestBuilder.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public <S, P extends Publisher<S>> ClientRequest.Builder body(P publisher, Class<S> elementClass) {
	this.body = BodyInserters.fromPublisher(publisher, elementClass);
	return this;
}
 
Example 7
Source File: DefaultWebClient.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public <T, P extends Publisher<T>> RequestHeadersSpec<?> body(P publisher, Class<T> elementClass) {
	this.inserter = BodyInserters.fromPublisher(publisher, elementClass);
	return this;
}
 
Example 8
Source File: DefaultClientRequestBuilder.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public <S, P extends Publisher<S>> ClientRequest.Builder body(P publisher, Class<S> elementClass) {
	this.body = BodyInserters.fromPublisher(publisher, elementClass);
	return this;
}
 
Example 9
Source File: DefaultWebClient.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public <T, P extends Publisher<T>> RequestHeadersSpec<?> body(P publisher, Class<T> elementClass) {
	this.inserter = BodyInserters.fromPublisher(publisher, elementClass);
	return this;
}
 
Example 10
Source File: WebReactiveHttpClient.java    From feign-reactive with Apache License 2.0 4 votes vote down vote up
protected BodyInserter<?, ? super ClientHttpRequest> provideBody(ReactiveHttpRequest request) {
	return bodyActualType != null
               ? BodyInserters.fromPublisher(request.body(), bodyActualType)
               : BodyInserters.empty();
}
 
Example 11
Source File: ModifyRequestBodyGatewayFilterFactory.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public GatewayFilter apply(Config config) {
	return new GatewayFilter() {
		@Override
		public Mono<Void> filter(ServerWebExchange exchange,
				GatewayFilterChain chain) {
			Class inClass = config.getInClass();
			ServerRequest serverRequest = ServerRequest.create(exchange,
					messageReaders);

			// TODO: flux or mono
			Mono<?> modifiedBody = serverRequest.bodyToMono(inClass)
					.flatMap(originalBody -> config.getRewriteFunction()
							.apply(exchange, originalBody))
					.switchIfEmpty(Mono.defer(() -> (Mono) config.getRewriteFunction()
							.apply(exchange, null)));

			BodyInserter bodyInserter = BodyInserters.fromPublisher(modifiedBody,
					config.getOutClass());
			HttpHeaders headers = new HttpHeaders();
			headers.putAll(exchange.getRequest().getHeaders());

			// the new content type will be computed by bodyInserter
			// and then set in the request decorator
			headers.remove(HttpHeaders.CONTENT_LENGTH);

			// if the body is changing content types, set it here, to the bodyInserter
			// will know about it
			if (config.getContentType() != null) {
				headers.set(HttpHeaders.CONTENT_TYPE, config.getContentType());
			}
			CachedBodyOutputMessage outputMessage = new CachedBodyOutputMessage(
					exchange, headers);
			return bodyInserter.insert(outputMessage, new BodyInserterContext())
					// .log("modify_request", Level.INFO)
					.then(Mono.defer(() -> {
						ServerHttpRequest decorator = decorate(exchange, headers,
								outputMessage);
						return chain
								.filter(exchange.mutate().request(decorator).build());
					})).onErrorResume(
							(Function<Throwable, Mono<Void>>) throwable -> release(
									exchange, outputMessage, throwable));
		}

		@Override
		public String toString() {
			return filterToStringCreator(ModifyRequestBodyGatewayFilterFactory.this)
					.append("Content type", config.getContentType())
					.append("In class", config.getInClass())
					.append("Out class", config.getOutClass()).toString();
		}
	};
}
 
Example 12
Source File: ModifyResponseBodyGatewayFilterFactory.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {

	Class inClass = config.getInClass();
	Class outClass = config.getOutClass();

	String originalResponseContentType = exchange
			.getAttribute(ORIGINAL_RESPONSE_CONTENT_TYPE_ATTR);
	HttpHeaders httpHeaders = new HttpHeaders();
	// explicitly add it in this way instead of
	// 'httpHeaders.setContentType(originalResponseContentType)'
	// this will prevent exception in case of using non-standard media
	// types like "Content-Type: image"
	httpHeaders.add(HttpHeaders.CONTENT_TYPE, originalResponseContentType);

	ClientResponse clientResponse = prepareClientResponse(body, httpHeaders);

	// TODO: flux or mono
	Mono modifiedBody = extractBody(exchange, clientResponse, inClass)
			.flatMap(originalBody -> config.getRewriteFunction().apply(exchange,
					originalBody))
			.switchIfEmpty(Mono.defer(() -> (Mono) config.getRewriteFunction()
					.apply(exchange, null)));

	BodyInserter bodyInserter = BodyInserters.fromPublisher(modifiedBody,
			outClass);
	CachedBodyOutputMessage outputMessage = new CachedBodyOutputMessage(exchange,
			exchange.getResponse().getHeaders());
	return bodyInserter.insert(outputMessage, new BodyInserterContext())
			.then(Mono.defer(() -> {
				Mono<DataBuffer> messageBody = writeBody(getDelegate(),
						outputMessage, outClass);
				HttpHeaders headers = getDelegate().getHeaders();
				if (!headers.containsKey(HttpHeaders.TRANSFER_ENCODING)
						|| headers.containsKey(HttpHeaders.CONTENT_LENGTH)) {
					messageBody = messageBody.doOnNext(data -> headers
							.setContentLength(data.readableByteCount()));
				}
				// TODO: fail if isStreamingMediaType?
				return getDelegate().writeWith(messageBody);
			}));
}
 
Example 13
Source File: EntityResponse.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * Create a builder with the given publisher.
 * @param publisher the publisher that represents the body of the response
 * @param typeReference the type of elements contained in the publisher
 * @param <T> the type of the elements contained in the publisher
 * @param <P> the type of the {@code Publisher}
 * @return the created builder
 */
static <T, P extends Publisher<T>> Builder<P> fromPublisher(P publisher,
		ParameterizedTypeReference<T> typeReference) {

	return new DefaultEntityResponseBuilder<>(publisher,
			BodyInserters.fromPublisher(publisher, typeReference));
}
 
Example 14
Source File: EntityResponse.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * Create a builder with the given publisher.
 * @param publisher the publisher that represents the body of the response
 * @param typeReference the type of elements contained in the publisher
 * @param <T> the type of the elements contained in the publisher
 * @param <P> the type of the {@code Publisher}
 * @return the created builder
 */
static <T, P extends Publisher<T>> Builder<P> fromPublisher(P publisher,
		ParameterizedTypeReference<T> typeReference) {

	return new DefaultEntityResponseBuilder<>(publisher,
			BodyInserters.fromPublisher(publisher, typeReference));
}
 
Example 15
Source File: EntityResponse.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Create a builder with the given publisher.
 * @param publisher the publisher that represents the body of the response
 * @param elementClass the class of elements contained in the publisher
 * @param <T> the type of the elements contained in the publisher
 * @param <P> the type of the {@code Publisher}
 * @return the created builder
 */
static <T, P extends Publisher<T>> Builder<P> fromPublisher(P publisher, Class<T> elementClass) {
	return new DefaultEntityResponseBuilder<>(publisher,
			BodyInserters.fromPublisher(publisher, elementClass));
}
 
Example 16
Source File: EntityResponse.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * Create a builder with the given publisher.
 * @param publisher the publisher that represents the body of the response
 * @param elementClass the class of elements contained in the publisher
 * @param <T> the type of the elements contained in the publisher
 * @param <P> the type of the {@code Publisher}
 * @return the created builder
 */
static <T, P extends Publisher<T>> Builder<P> fromPublisher(P publisher, Class<T> elementClass) {
	return new DefaultEntityResponseBuilder<>(publisher,
			BodyInserters.fromPublisher(publisher, elementClass));
}