Java Generic Web Client
02:46 19 Feb 2026

I have multiple APIs that returns different response bodies. I want to have a generic service that is able deserialize any data to given type.

public class GenericResponse  {
    private T data;
    private String message;
}

here T can be any dto.

and my generic service looks like this.

public RestService{
    public void getRestUrl(url){ 
        webClient
            .get()
            .uri(url)
            .headers(httpHeaders -> httpHeaders.addAll(getApiHeaders()))
            .retrieve()
            .onStatus(HttpStatusCode::isError, this::handleError)
            .bodyToMono(new ParameterizedTypeReference() {})
    }
}
GenericResponse response = restService.getRestUrl(urlWithParameters);

however when i run the query the response is not serialized to MyCustomDTO it is serialized to linkedHashMap since java is not able to determine what T is in the runtime.

Is there any workaround or any other way to achieve a generic rest service to remove code duplication every time i make a rest call.

java spring-rest spring-webclient