Spring webclient using generic return type

1 answer

I have written below code, i want to return a generic type (Class response) in the method call instead of JsonNode. I am getting error in bodyToMono call if i am using a generic type response object. So how to retrive a generic type response using below code.

 public JsonNode exchange(WebClient webClient, WebClientRequest requestDetails, Class<V> response) throws IOException {     if (requestDetails.getHttpMethod() == null) {         requestDetails.setHttpMethod(HttpMethod.GET);     }     if (requestDetails.getParams() == null) {         requestDetails.setParams(new HashMap<>());     }     JsonNode result = null;     try {         result = webClient                 .method(requestDetails.getHttpMethod())                 .uri(requestDetails.getServiceUrl(), requestDetails.getParams())                 .headers(httpHeaders -> httpHeaders.setAll(requestDetails.getHeaders()))                 .exchange()                 .flatMap(ClientResponse -> ClientResponse.bodyToMono(JsonNode.class))                 .retryWhen(Retry.any()                         .fixedBackoff(Duration.ofSeconds(5))                         .retryMax(5))                 .delaySubscription(Duration.ofSeconds(10))                 .block();     } catch (WebClientResponseException ex) {         log.error("WebClientResponseException in Webclient call : ", ex.getMessage());     } catch (Exception ex) {         log.error("Exception in Webclient call : ", ex.getMessage());     }     return result; } 

All answers to this question, which has the identifier 61215703

The best answer:

Your problem is in the method declaration. You have put:

public JsonNode exchange(....... 

With this, you are making a method named "exchange" that returns JsonNode object. To return generic object you need to change the declaration:

public <YourClassObject> exchange(...... 

To do it, you can use generic Object class:

public Object exchange(... 

But, when call method exchante(), you will need to cast the result.

To avoid this type of problems, you can use T object. You can read more here, for example.

Last questions

how do i remove the switch on my home screen?
how to edit the JS date and time to update atuomatically?
How to utilize data stored in a multidimensional array
Powermockito not mocking URL constructor in URI.toURL() method
Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices
docker wordpress container can't connect to mysql container
How can I declare a number in java that is more than 64-bits? [duplicate]
Optaplanner solutionClass entityCollectionProperty should never return null error when simple JSON object passed to controller
Anylogic, get the time a pedestrain is in a queue
How do I fix this syntax issue with my .flex file?
Optimizing query in PHP
How to find the highest number of a column and print two columns of that row in R?
Ideas on “Error: Type com.google.firebase.iid.zzav is referenced as an interface from com.google.firebase.messaging.zzd”?
JCIFS SmbFile.exists() and SmbFile.isDirectory() return false when it exists and I can listFiles()
PHP total order
Laravel booking system design
neural net - undefined column selected
How to indicate y axis does not start from 0 in ggplot?
Fragments in backStack
Spinner how to change the data