I tried to replace RestTemplate from WebClient because according to the Java Doc the RestTemplate will be deprecated. Spring team advises using the WebClient if possible.
previous code with RestTempalte is as follows
public Map<String,String> getInfo()
{
HttpHeaders headers = new HttpHeaders();
headers.set( ACCEPT, MediaType.APPLICATION_JSON_VALUE );
HttpEntity<?> entity = new HttpEntity<>( headers );
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl( this.endpoint + VERSION_INFO_PATH );
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<CPResponse> response = restTemplate.exchange(
builder.toUriString(),
HttpMethod.GET,
entity,
CPResponse.class );
List<Object> resultList = response.getBody().getResults();
if( response.getBody().getResults() != null && !( resultList )
.isEmpty() )
{
return ( ( LinkedHashMap ) resultList.get( 0 ) );
}
else
{
throw new CrawlerRuntimeExceptions( "Invalid response from API" );
}
}
I want to replace RestTemplate from WebClient. So I implement class WebClientConnection as follows
public class WebClientConnection
{
private WebClient webClient;
public WebClientConnection( String baseUrl )
{
this.webClient = WebClient.create( baseUrl );
}
public Mono<CPResponse> get( String url )
{
return webClient.get().uri( "/{url}",url ).retrieve().bodyToMono( CPResponse.class );
}
public Flux<CPResponse> getAll( String url )
{
return webClient.get().uri( "/{url}",url ).retrieve().bodyToFlux( CPResponse.class );
}
public Mono<CPResponse> post( String url, HttpEntity entity )
{
return webClient.post().uri( "/{url}",url ).retrieve().bodyToMono( CPResponse.class );
}
}
I use this dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<version>>2.1.3.RELEASE</version>
</dependency>
public void getInfo()
{
WebClientConnection webClientConnection = new WebClientConnection( endpoint );
Mono<CPResponse> response = webClientConnection.get( VERSION_INFO_PATH );
}
There is stackOverflow error on webclient create
public WebClientConnection( String baseUrl )
{
this.webClient = WebClient.create( baseUrl );
}
How to properly do this migration from RestTemplate to WebClient?