如何在Java中使用构造函数进行WebClient初始化?

3

我试图使用WebClient替换RestTemplate,因为Java Doc文档中将弃用RestTemplate。Spring团队建议尽可能使用WebClient。

之前使用RestTemplate的代码如下:

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创建时出现堆栈溢出错误。

public WebClientConnection( String baseUrl )
    {
        this.webClient = WebClient.create( baseUrl );

    }

如何正确地从RestTemplate迁移到WebClient?

2个回答

1

实际上,StackOverflow异常javadoc

当应用程序递归太深时发生堆栈溢出时抛出。

(好的解释在这里

创建WebClient本身并不包含这样的递归。也许您在某个地方(隐式地)使用了递归?

堆栈跟踪可能有助于找出问题所在。


1
我找出了问题。这是一个与代码问题有关的依赖项。添加后。
   <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 );
    }
}

然后问题就解决了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接