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

The following examples show how to use org.springframework.web.reactive.function.BodyInserters#fromObject() . 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: ApiClient.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
/**
 * Select the body to use for the request
 * @param obj the body object
 * @param formParams the form parameters
 * @param contentType the content type of the request
 * @return Object the selected body
 */
protected BodyInserter<?, ? super ClientHttpRequest> selectBody(Object obj, MultiValueMap<String, Object> formParams, MediaType contentType) {
    if(MediaType.APPLICATION_FORM_URLENCODED.equals(contentType)) {
        MultiValueMap<String, String> map = new LinkedMultiValueMap();

        formParams
                .toSingleValueMap()
                .entrySet()
                .forEach(es -> map.add(es.getKey(), String.valueOf(es.getValue())));

        return BodyInserters.fromFormData(map);
    } else if(MediaType.MULTIPART_FORM_DATA.equals(contentType)) {
        return BodyInserters.fromMultipartData(formParams);
    } else {
        return obj != null ? BodyInserters.fromObject(obj) : null;
    }
}
 
Example 2
Source File: DefaultWebClient.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public RequestHeadersSpec<?> syncBody(Object body) {
	Assert.isTrue(!(body instanceof Publisher),
			"Please specify the element class by using body(Publisher, Class)");
	this.inserter = BodyInserters.fromObject(body);
	return this;
}
 
Example 3
Source File: DefaultWebClient.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public RequestHeadersSpec<?> syncBody(Object body) {
	Assert.isTrue(!(body instanceof Publisher),
			"Please specify the element class by using body(Publisher, Class)");
	this.inserter = BodyInserters.fromObject(body);
	return this;
}
 
Example 4
Source File: MailSender.java    From Learning-Path-Spring-5-End-to-End-Programming with MIT License 5 votes vote down vote up
public Flux<Void> send(Mail mail){
  final BodyInserter<SendgridMail, ReactiveHttpOutputMessage> body = BodyInserters
      .fromObject(SendgridMail.builder().content(mail.getMessage()).from(mail.getFrom()).to(mail.getTo()).subject(mail.getSubject()).build());
  return this.webClient.mutate().baseUrl(this.url).build().post()
      .uri("/v3/mail/send")
      .body(body)
      .header("Authorization","Bearer " + this.apiKey)
      .header("Content-Type","application/json")
      .retrieve()
      .onStatus(HttpStatus::is4xxClientError, clientResponse ->
          Mono.error(new RuntimeException("Error on send email"))
      ).bodyToFlux(Void.class);
}
 
Example 5
Source File: MailSender.java    From Spring-5.0-By-Example with MIT License 5 votes vote down vote up
public Flux<Void> send(Mail mail){
  final BodyInserter<SendgridMail, ReactiveHttpOutputMessage> body = BodyInserters
      .fromObject(SendgridMail.builder().content(mail.getMessage()).from(mail.getFrom()).to(mail.getTo()).subject(mail.getSubject()).build());
  return this.webClient.mutate().baseUrl(this.url).build().post()
      .uri("/v3/mail/send")
      .body(body)
      .header("Authorization","Bearer " + this.apiKey)
      .header("Content-Type","application/json")
      .retrieve()
      .onStatus(HttpStatus::is4xxClientError, clientResponse ->
          Mono.error(new RuntimeException("Error on send email"))
      ).bodyToFlux(Void.class);
}
 
Example 6
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 7
Source File: EntityResponse.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Create a builder with the given object.
 * @param t the object that represents the body of the response
 * @param <T> the type of the elements contained in the publisher
 * @return the created builder
 */
static <T> Builder<T> fromObject(T t) {
	return new DefaultEntityResponseBuilder<>(t, BodyInserters.fromObject(t));
}
 
Example 8
Source File: EntityResponse.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * Create a builder with the given object.
 * @param t the object that represents the body of the response
 * @param <T> the type of the elements contained in the publisher
 * @return the created builder
 */
static <T> Builder<T> fromObject(T t) {
	return new DefaultEntityResponseBuilder<>(t, BodyInserters.fromObject(t));
}