Example usage for org.springframework.web.reactive.function BodyInserters fromObject

List of usage examples for org.springframework.web.reactive.function BodyInserters fromObject

Introduction

In this page you can find the example usage for org.springframework.web.reactive.function BodyInserters fromObject.

Prototype

public static <T> BodyInserter<T, ReactiveHttpOutputMessage> fromObject(T body) 

Source Link

Document

Inserter to write the given object.

Usage

From source file:top.zhacker.ms.reactor.spring.function.Client.java

public void createPerson() {
    URI uri = URI.create(String.format("http://%s:%d/person", Server.HOST, Server.PORT));
    Person jack = new Person("Jack Doe", 16);

    ClientRequest request = ClientRequest.method(HttpMethod.POST, uri).body(BodyInserters.fromObject(jack))
            .build();// ww  w.ja  v a 2  s  . c  o m

    Mono<ClientResponse> response = exchange.exchange(request);

    System.out.println(response.block().statusCode());
}

From source file:org.mongodb.redis.integration.WebFluxIntegrationTests.java

@Test
@DisplayName("Invalid Data")
void testCreateBookFail() {
    Book book = Book.builder().author("Raja").text("This is a Test Book")
            .title(RandomStringUtils.randomAlphanumeric(200)).build();

    this.webTestClient.post().uri("/books").contentType(MediaType.APPLICATION_JSON_UTF8)
            .accept(MediaType.APPLICATION_JSON_UTF8).body(BodyInserters.fromObject(book)).exchange()
            .expectStatus().isBadRequest().expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8)
            .expectBody().jsonPath("$.message").isNotEmpty().jsonPath("$.errors.[0].defaultMessage")
            .isEqualTo("size must be between 0 and 140");

    book = Book.builder().build();
    this.webTestClient.post().uri("/books").contentType(MediaType.APPLICATION_JSON_UTF8)
            .accept(MediaType.APPLICATION_JSON_UTF8).body(Mono.just(book), Book.class).exchange().expectStatus()
            .isBadRequest().expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8).expectBody()
            .jsonPath("$.message").isNotEmpty().jsonPath("$.errors.[0].defaultMessage")
            .isEqualTo("must not be blank");
}

From source file:org.springframework.boot.autoconfigure.web.reactive.error.DefaultErrorWebExceptionHandler.java

/**
 * Render the error information as a JSON payload.
 * @param request the current request//from  w  w  w.  ja va2  s.c o  m
 * @return a {@code Publisher} of the HTTP response
 */
protected Mono<ServerResponse> renderErrorResponse(ServerRequest request) {
    boolean includeStackTrace = isIncludeStackTrace(request, MediaType.ALL);
    Map<String, Object> error = getErrorAttributes(request, includeStackTrace);
    HttpStatus errorStatus = getHttpStatus(error);
    return ServerResponse.status(getHttpStatus(error)).contentType(MediaType.APPLICATION_JSON_UTF8)
            .body(BodyInserters.fromObject(error)).doOnNext((resp) -> logError(request, errorStatus));
}