WebClient.RequestHeadersSpec is a raw type

Viewed 985

I got this weird warning on WebClient.RequestHeadersSpec and wasn't sure what type I should be passing into the WebClient.RequestHeadersSpec<?> as part of the initialization of this requestSpec.

    
    WebClient.RequestHeadersSpec requestSpec = webClientBuilder.build().get().uri(uri);
    
    try {
          return requestSpec
              .header("Content-Type", HEADER_VALUE_CONTENT_TYPE)
              .retrieve()
              .bodyToMono(String.class)
              .timeout(Duration.ofMillis(config.getTimeoutInMilliseconds()))
              .block();
    
    } catch (WebClientResponseException e) {   // do something }

WebClient.RequestHeadersSpec is a raw type. References to generic type WebClient.RequestHeadersSpec should be parameterized Java(16777788)

However, this works fine with post(), so I am curious to know what's the main difference


    WebClient.RequestBodySpec requestBodySpec = webClientBuilder.build().post().uri(uri);

note: the block() is needed, it is a sync call

1 Answers

If you check the WebClient interface you will see that get() has return type of RequestHeadersUriSpec<?>, while post() has RequestBodyUriSpec which extends RequestHeadersUriSpec<RequestBodySpec>. While the post method's return type is parameterized through inheritance, the get method's is not. Hence the warning.

To avoid the warning you can simply provide the generic type for type declaration:

WebClient.RequestHeadersSpec<?> requestSpec = webClientBuilder.build().get().uri(uri);
Related