Spring WebClient根据状态码嵌套Mono

问题描述

使用WebClient我想根据HTTP状态代码分别处理ClientResponse。在下面,您看到顶部doOnSuccess中使用了两个新的subscription()方法。如何将那些嵌套的Monos携带到WebClient的Mono链中?也就是说,如何消除内部单声道。

webClient.post()
            .uri( soapServiceUrl )
            .contentType(MediaType.TEXT_XML)
            //.body(Mono.just(req),String.class )
            .body( Mono.just(getCountryRequest),GetCountryRequest.class  )
            .exchange()
            .filter( (ClientResponse response) -> { return true; } )
            .doOnSuccess( (ClientResponse response) -> {
                //nested mono 1
                if( response.statusCode().is5xxServerError() ){
                    response.toEntity(String.class).doOnSuccess(
                            error -> {
                                System.out.println("error : "+ error);
                            }).subscribe();
                }

                //nested mono 2
                if( response.statusCode().is2xxSuccessful() ){
                    response.toEntity(GetCountryResponse.class).doOnSuccess(
                            getCountryResponse -> {
                                System.out.println("success : "+ getCountryResponse.getBody().getCountry().getCapital());
                            }).subscribe();
                }
            })
            .doOnError( (Throwable error) -> {
                System.out.println( "getCountryResponse.error : "+ error );
            })
            .subscribe();

解决方法

Webclient的retrieve()方法具有更好的处理错误代码的方法。

我会这样:

webClient.post()
            .uri( soapServiceUrl )
            .contentType(MediaType.TEXT_XML)
            //.body(Mono.just(req),String.class )
            .body( Mono.just(getCountryRequest),GetCountryRequest.class  )
            .retrieve()
            .onStatus(
              HttpStatus::isError,clientResponse ->
                  clientResponse
                   //get the error response body as a string
                    .bodyToMono(String.class)
                    //flatmap the errorResponseBody into a new error signal
                    .flatMap(
                        errorResponseBody ->
                            Mono.error(
                                new ResponseStatusException(
                                    clientResponse.statusCode(),errorResponseBody))))

            //get success response as Mono<GetCountryResponse>
            .bodyToMono(GetCountryResponse.class)
            .doOnSuccess( (GetCountryResponse response) ->
                   System.out.println("success : " + 
                    response.getBody().getCountry().getCapital()))
            //Response errors will now be logged below
            .doOnError(ResponseStatusException.class,error -> {
                System.out.println( "getCountryResponse.error : "+ error );
            })
            .subscribe();

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...