Java 使用泛型返回类型的Spring webclient

Java 使用泛型返回类型的Spring webclient,java,spring,reactive-programming,spring-webclient,Java,Spring,Reactive Programming,Spring Webclient,我已经编写了以下代码,我希望在方法调用中返回泛型类型(类响应),而不是JsonNode。如果使用泛型类型响应对象,则bodyToMono调用中出现错误。因此,如何使用下面的代码检索泛型类型响应 public JsonNode exchange(WebClient webClient, WebClientRequest requestDetails, Class<V> response) throws IOException { if (requestDetails.getH

我已经编写了以下代码,我希望在方法调用中返回泛型类型(类响应),而不是JsonNode。如果使用泛型类型响应对象,则bodyToMono调用中出现错误。因此,如何使用下面的代码检索泛型类型响应

 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;
}
publicjsonnode交换(WebClient-WebClient,webclientrequestdetails,Class-response)抛出IOException{
if(requestDetails.getHttpMethod()==null){
requestDetails.setHttpMethod(HttpMethod.GET);
}
if(requestDetails.getParams()==null){
setParams(新的HashMap());
}
JsonNode结果=null;
试一试{
结果=网络客户端
.method(requestDetails.getHttpMethod())
.uri(requestDetails.getServiceUrl(),requestDetails.getParams())
.headers(httpHeaders->httpHeaders.setAll(requestDetails.getHeaders())
.exchange()
.flatMap(ClientResponse->ClientResponse.bodytomino(JsonNode.class))
.retryWhen(重试.any()
.固定退避(持续时间秒(5))
雷特里马克斯先生(5))
.延迟订阅(持续时间秒(10))
.block();
}捕获(WebClientResponseException ex){
log.error(“Webclient调用中的Webclient响应异常:”,例如getMessage());
}捕获(例外情况除外){
错误(“Webclient调用中的异常:”,例如getMessage());
}
返回结果;
}

您的问题在方法声明中。你提出:

public JsonNode exchange(.......
这样,您就创建了一个名为“exchange”的方法,该方法返回JsonNode对象。要返回泛型对象,需要更改声明:

public <YourClassObject> exchange(......
但是,当调用方法exchante()时,需要强制转换结果


为了避免此类问题,可以使用T对象。例如,你可以阅读更多

您的问题在方法声明中。你提出:

public JsonNode exchange(.......
这样,您就创建了一个名为“exchange”的方法,该方法返回JsonNode对象。要返回泛型对象,需要更改声明:

public <YourClassObject> exchange(......
但是,当调用方法exchante()时,需要强制转换结果


为了避免此类问题,可以使用T对象。例如,你可以阅读更多

如果您有一个接口及其实现,请使用
@JsonTypeInfo
。在其他情况下,只需反序列化为
Object.class
(很可能是Map-Map)并从那里开始。如果您有接口及其实现,请使用
@JsonTypeInfo
。在其他情况下,只需反序列化为
Object.class
(很可能是Map-Map)并从那里开始。我更改了方法签名,如->public V exchange(WebClient WebClient,WebClient requestDetails,V responseType)抛出IOException,但方法中的这一行给出了错误->flatMap(ClientResponse->ClientResponse.BodyToNo(responseType.getClass())//lambda表达式中使用的错误变量应该是最终的或有效的最终更改的方法签名,如->public V exchange(WebClient WebClient,WebClient requestDetails,V responseType)抛出IOException,但方法中的此行给出了错误->.flatMap(ClientResponse->ClientResponse.bodyToMono(responseType.getClass())//lambda表达式中使用的错误变量应该是final或实际上是final