我试图从WebClient替换RestTemplate,因为根据Java Doc,将不建议使用RestTemplate。 Spring团队建议尽可能使用WebClient。
RestTempalte的先前代码如下
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" );
}
}
我想从WebClient替换RestTemplate。因此,我实现了WebClientConnection类,如下所示
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 );
}
}
我使用此依赖项
<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 );
}
在webclient创建上有stackOverflow错误
public WebClientConnection( String baseUrl )
{
this.webClient = WebClient.create( baseUrl );
}
如何正确完成从RestTemplate到WebClient的迁移?
我找出了问题。它与代码问题有关。添加后>
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>
到pom并按如下所示修改WebClientConnection类。
public class WebClientConnection { private final WebClient webClient; private static final String API_MIME_TYPE = "application/json"; private static final String API_BASE_URL = "http://localhost:8080"; private static final String USER_AGENT = "Spring 5 WebClient"; public WebClientConnection( ) { this.webClient = WebClient.builder() .baseUrl(API_BASE_URL) .defaultHeader(HttpHeaders.CONTENT_TYPE, API_MIME_TYPE) .defaultHeader(HttpHeaders.USER_AGENT, USER_AGENT) .build(); } 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 ); } }
然后问题已解决。