3

我尝试从 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 的迁移?

4

2 回答 2

1

实际上,StackOverflow 异常javadoc

当由于应用程序递归太深而发生堆栈溢出时引发。

(这里有很好的解释)

创建 WebClient 本身并不包含这种递归。也许您正在某处使用(隐式)递归?

堆栈跟踪可以帮助找出问题所在。

于 2019-10-17T09:51:33.920 回答
0

我弄清楚了这个问题。这是一个有代码问题的依赖。添加后

   <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 );
    }
}

然后问题就解决了。

于 2019-10-17T17:11:10.957 回答