Spring 5 Reactive WebClient does not seem to deserialize parameterized types
Asked Answered
N

1

7

This question used to be titled "Spring 5 Reactive WebClient consuming HAL+JSON HATEOAS PagedResources", but the new title is more appropriate.

The following code works perfectly with RestTemplate (the parametrized type is being returned from a HATEOAS/HAL PagedResource served from Spring DataREST):

// use exchange with ParameterizedTypeReference
ResponseEntity<PagedResources<Foo>> responseEntity = 
    restTemplate.exchange("/foos", HttpMethod.GET, null, 
    new ParameterizedTypeReference<PagedResources<Foo>>() {}, 
    randomServerPort, 0, 100);
// then the actual list of foos can be obtained like so
PagedResources<Foo> resources = responseEntity.getBody();
List<foo> foos = new ArrayList(resources.getContent());

This is NOT working with Spring 5 Reactive WebClient:

public Mono<PagedResources<Foo>> getFoos() {
        return client.get()
            .uri("/foos").accept(MediaTypes.HAL_JSON)
            .retrieve()
            .bodyToMono(new ParameterizedTypeReference<PagedResources<Foo>>(){});
}

The controller code that calls the service method above is:

@GetMapping("/foos")
public Mono<PagedResources<Foo>> getFoos() {
    return dataService.getFoos();
}

The result with curl is:

{"links":[],"content":[],"page":null}

After a lot of research, the code above should work as the parametrized type should be passed along to Jackson just like in the RestTemplate's case shown above.

This is looking now more like a bug. But before filing one I would like to see if someone has WebClient bodyToXXX working with parametrized types (i.e. ParameterizedTypeReference, Super Type Tokens, etc.)

Note there is a test for this in the WebClient code but maybe the embedded type ref (PagedResources) is not currently supported or has a bug:

https://github.com/spring-projects/spring-framework/blob/master/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientIntegrationTests.java#L149

Bug filed: https://jira.spring.io/browse/SPR-16715

Nerve answered 22/3, 2018 at 4:34 Comment(0)
S
3

Neither Spring Data REST nor Spring HATEOAS currently support Reactor types at this point in time.

Shamus answered 27/11, 2018 at 4:33 Comment(4)
Thanks for your answer @gregturn, this was our conclusion as well. But do you have any official reference link from Pivotal and/or Spring Community that says this explicitly?Nerve
As the lead developer for Spring HATEOAS, I figured this answer suffices as an official answer.Shamus
thanks for following up and confirming there is a disconnect between the Webflux stack and other Spring projects such as Spring HATEOAS! But why are you answering this from a Spring Data REST's perspective? IMHO this seems a limitation of Spring 5 Reactive WebClient and not that Spring Data REST nor Spring HATEOAS. Shouldn't someone from the Webflux team step up and explain why their reactive web client is unable to consume resources generated by Spring HATEOAS? and not the other way around?Nerve
Spring Framework doesn't know anything about Spring HATEOAS. It's the same reason that it takes extra code to register the same handlers for RestTemplate[1]. [1] - github.com/spring-projects/spring-hateoas/blob/master/src/main/…Shamus

© 2022 - 2024 — McMap. All rights reserved.