Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webflux webclient and generic types

I am trying to build a generic class that will consume a REST api. The api returns lists of objects depending on the url.

I have built a Generic class

public class RestConsumer<T> {
    WebClient client;

    public RestConsumer(){
        //Initialize client
    }

    public List<T> getList(String relativeUrl){
        try{
            return client
                .get()
                .uri(relativeUrl)
                .retrieve()
                .bodyToMono(new ParameterizeTypeReference<List<T>> (){}
                .block()
        catch(Exception e){}
}

}

The problem is that T is replaced by Object at compilation time and the whole thing return a List of LinkedHashMap instead of a List of T. I tried lots of workarounds but no luck. Any suggestions?

like image 880
liorc2121 Avatar asked Nov 19 '18 15:11

liorc2121


People also ask

What is the difference between webclient and webflux?

WebFlux client and server rely on the same non-blocking codecs to encode and decode request and response content. Internally WebClient delegates to an HTTP client library.

What is spring webflux used for?

Overview Spring WebFlux is part of Spring 5 and provides reactive programming support for web applications. In this tutorial, we'll be creating a small reactive REST application using the reactive web components RestController and WebClient. We will also be looking at how to secure our reactive endpoints using Spring Security.

Does webflux support Reactive Streams and WebSocket?

Other than Reactive RestController and WebClient, the WebFlux framework also supports reactive WebSocket and the corresponding WebSocketClient for socket style streaming of Reactive Streams. For more information, we also have a detailed article focused on working with Reactive WebSocket with Spring 5.

What is reactive webclient in spring 5?

A lot of frameworks and projects are introducing reactive programming and asynchronous request handling. Consequently, Spring 5 introduced a reactive WebClient implementation as a part of the WebFlux framework. In this tutorial, we'll see how to reactively consume REST API endpoints with WebClient.


1 Answers

I encountered the same problem and in order to work I added ParameterizedTypeReference as a parameter for that function.

public <T> List<T> getList(String relativeUrl, 
                           ParameterizedTypeReference<List<T>> typeReference){
    try{
        return client
            .get()
            .uri(relativeUrl)
            .retrieve()
            .bodyToMono(typeReference)
            .block();
    } catch(Exception e){
        return null;
    }
}

And call that function with

ParameterizedTypeReference<List<MyClass>> typeReference = new ParameterizedTypeReference<List<MyClass>>(){};
List<MyClass> strings = getList(relativeUrl, typeReference);
like image 89
Andrei Stoicescu Avatar answered Sep 17 '22 12:09

Andrei Stoicescu