Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring 5 Reactive WebClient does not seem to deserialize parameterized types

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

like image 423
aimass Avatar asked Mar 22 '18 04:03

aimass


1 Answers

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

like image 93
gregturn Avatar answered Dec 06 '22 23:12

gregturn