org.springframework.web.reactive.function.client

Interface ClientResponse

  • All Known Implementing Classes:
    ClientResponseWrapper


    public interface ClientResponse
    Represents an HTTP response, as returned by WebClient and also ExchangeFunction. Provides access to the response status and headers, and also methods to consume the response body.

    NOTE: When given access to a ClientResponse, through the WebClient exchange() method, you must always use one of the body or toEntity methods to ensure resources are released and avoid potential issues with HTTP connection pooling. You can use bodyToMono(Void.class) if no response content is expected. However keep in mind that if the response does have content, the connection will be closed and will not be placed back in the pool.

    Since:
    5.0
    Author:
    Brian Clozel, Arjen Poutsma
    • Method Detail

      • statusCode

        HttpStatus statusCode()
        Return the status code of this response.
        Returns:
        the status as an HttpStatus enum value
        Throws:
        java.lang.IllegalArgumentException - in case of an unknown HTTP status code
        See Also:
        HttpStatus.valueOf(int)
      • rawStatusCode

        int rawStatusCode()
        Return the (potentially non-standard) status code of this response.
        Returns:
        the status as an integer
        Since:
        5.1
        See Also:
        statusCode(), HttpStatus.resolve(int)
      • strategies

        ExchangeStrategies strategies()
        Return the strategies used to convert the body of this response.
      • body

        <T> T body(BodyExtractor<T,? super ClientHttpResponse> extractor)
        Extract the body with the given BodyExtractor.
        Type Parameters:
        T - the type of the body returned
        Parameters:
        extractor - the BodyExtractor that reads from the response
        Returns:
        the extracted body
      • bodyToMono

        <T> reactor.core.publisher.Mono<T> bodyToMono(java.lang.Class<? extends T> elementClass)
        Extract the body to a Mono.
        Type Parameters:
        T - the element type
        Parameters:
        elementClass - the class of element in the Mono
        Returns:
        a mono containing the body of the given type T
      • bodyToMono

        <T> reactor.core.publisher.Mono<T> bodyToMono(ParameterizedTypeReference<T> typeReference)
        Extract the body to a Mono.
        Type Parameters:
        T - the element type
        Parameters:
        typeReference - a type reference describing the expected response body type
        Returns:
        a mono containing the body of the given type T
      • bodyToFlux

        <T> reactor.core.publisher.Flux<T> bodyToFlux(java.lang.Class<? extends T> elementClass)
        Extract the body to a Flux.
        Type Parameters:
        T - the element type
        Parameters:
        elementClass - the class of element in the Flux
        Returns:
        a flux containing the body of the given type T
      • bodyToFlux

        <T> reactor.core.publisher.Flux<T> bodyToFlux(ParameterizedTypeReference<T> typeReference)
        Extract the body to a Flux.
        Type Parameters:
        T - the element type
        Parameters:
        typeReference - a type reference describing the expected response body type
        Returns:
        a flux containing the body of the given type T
      • toEntity

        <T> reactor.core.publisher.Mono<ResponseEntity<T>> toEntity(java.lang.Class<T> bodyType)
        Return this response as a delayed ResponseEntity.
        Type Parameters:
        T - response body type
        Parameters:
        bodyType - the expected response body type
        Returns:
        Mono with the ResponseEntity
      • toEntity

        <T> reactor.core.publisher.Mono<ResponseEntity<T>> toEntity(ParameterizedTypeReference<T> typeReference)
        Return this response as a delayed ResponseEntity.
        Type Parameters:
        T - response body type
        Parameters:
        typeReference - a type reference describing the expected response body type
        Returns:
        Mono with the ResponseEntity
      • toEntityList

        <T> reactor.core.publisher.Mono<ResponseEntity<java.util.List<T>>> toEntityList(java.lang.Class<T> elementType)
        Return this response as a delayed list of ResponseEntitys.
        Type Parameters:
        T - the type of elements in the list
        Parameters:
        elementType - the expected response body list element type
        Returns:
        Mono with the list of ResponseEntitys
      • toEntityList

        <T> reactor.core.publisher.Mono<ResponseEntity<java.util.List<T>>> toEntityList(ParameterizedTypeReference<T> typeReference)
        Return this response as a delayed list of ResponseEntitys.
        Type Parameters:
        T - the type of elements in the list
        Parameters:
        typeReference - a type reference describing the expected response body type
        Returns:
        Mono with the list of ResponseEntitys
      • from

        static ClientResponse.Builder from(ClientResponse other)
        Create a builder with the status, headers, and cookies of the given response.
        Parameters:
        other - the response to copy the status, headers, and cookies from
        Returns:
        the created builder
      • create

        static ClientResponse.Builder create(HttpStatus statusCode)
        Create a response builder with the given status code and using default strategies for reading the body.
        Parameters:
        statusCode - the status code
        Returns:
        the created builder
      • create

        static ClientResponse.Builder create(HttpStatus statusCode,
                                             ExchangeStrategies strategies)
        Create a response builder with the given status code and strategies for reading the body.
        Parameters:
        statusCode - the status code
        strategies - the strategies
        Returns:
        the created builder
      • create

        static ClientResponse.Builder create(HttpStatus statusCode,
                                             java.util.List<HttpMessageReader<?>> messageReaders)
        Create a response builder with the given status code and message body readers.
        Parameters:
        statusCode - the status code
        messageReaders - the message readers
        Returns:
        the created builder