Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-15725

Add BodyToMono and BodyToFlux overrides that receive ParameterizedTypeReference

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Minor
    • Resolution: Complete
    • Affects Version/s: 5.0 RC2
    • Fix Version/s: 5.0 RC3
    • Component/s: Reactive, Web
    • Labels:
      None
    • Last commented by a User:
      true

      Description

      Sometimes the body of responses from WebClient should map to a type with generics.
      Currently the bodyToMono, BodyToFlux and toEntity methods on ClientResponse and on ResponseSpec only receive Class<T> bodyType.
      This does not allow for extracting bodies of a type that has generics.

      Since the class BodyExtractors already has a static method: public static <T> BodyExtractor<Mono<T>, ReactiveHttpInputMessage> toMono(ParameterizedTypeReference<T> typeReference)
      It seems like all there is left to do is to add bodyToMono, BodyToFlux and toEntity overrides that utilize that factory method (and the similar toFlux factory method)

      Code that uses the suggested functionality would look like the following:

      Mono<Page<ExampleDto>> singlePageOfExamples = webClient.get()
                      .uri("http://spring.io/example?page=0")
                      .retrieve()
                      .bodyToMono(new ParameterizedTypeReference<Page<ExampleDto>>() {});
      
      singlePageOfExamples = webClient.get()
                      .uri("http://spring.io/example?page=1")
                      .retrieve()
                      .toEntity(new ParameterizedTypeReference<Page<ExampleDto>>() {})
                      .map(responseEntity -> responseEntity.getBody())
      
      singlePageOfExamples = webClient.get()
                      .uri("http://spring.io/example?page=2")
                      .exchange()
                      .flatMap(clientResponse -> clientResponse.bodyToMono(new ParameterizedTypeReference<Page<ExampleDto>>() {}));
      

        Attachments

          Activity

            People

            Assignee:
            arjen.poutsma Arjen Poutsma
            Reporter:
            dorongold Doron Gold
            Last updater:
            Spring Issues Spring Issues
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Days since last comment:
              2 years, 45 weeks, 4 days ago