How to convert Mono<PojoResponseObject> to a Mono<ResponseEntity> using webclient

huangapple go评论104阅读模式
英文:

How to convert Mono<PojoResponseObject> to a Mono<ResponseEntity> using webclient

问题

I have the following webclient call

public Mono<String> sendFile(final MultipartFile multipartFile) {
    Mono<Response> response = webClient.post()
                .uri(new URI("someurl"))
                .body(BodyInserters.fromMultipartData(multiValueMap))
                .retrieve()
                .bodyToMono(Response.class);

    // Need to implement additional logic here to convert the Mono<Response> into Mono<RequestID>

}

public class Response {

    private RequestID requestID;

}

public class RequestID {

    private String requestID;

}

The call to the API gets returned back into a JSON Response POJO object, however, I am only interested in the requestID which lives in my RequestID POJO, so I want to convert my Mono<Response> into a Mono<RequestID>, but not sure how to do that?

So for my controller, I basically want something similar to this:

@PostMapping("/request")
    public Mono<ResponseEntity> sendRequest(@RequestPart(value = "file") final MultipartFile multipartFile) {
        Mono<RequestID> request = service.sendFile(multipartFile);
        Response2<String> response = new Response<>(request.getRequestID, true, "SUCCESS"); // Response2 is just a POJO with three fields
        return new Mono<ResponseEntity>(response, HttpStatus.CREATED);
    }
英文:

I have the following webclient call

public Mono&lt;String&gt; sendFile(final MultipartFile multipartFile) {
    Mono&lt;Response&gt; response = webClient.post()
                .uri(new URI(&quot;someurl&quot;))
                .body(BodyInserters.fromMultipartData(multiValueMap))
                .retrieve()
                .bodyToMono(Response.class);

    // Need to implement additional logic here to convert the Mono&lt;Response&gt; into Mono&lt;RequestID&gt;

}

public class Response {

    private RequestID requestID;

}

public class RequestID {

    private String requestID;

}

The call to the API gets returned back into a JSON Response POJO object however I am only interested in the requestID which lives in my RequestID POJO so I want to convert my Mono < Response > into a Mono< RequestId > but not sure how to do that?

So for my controller I basically want something similar to this:

@PostMapping(&quot;/request&quot;)
    public Mono&lt;ResponseEntity&gt; sendRequest(@RequestPart(value = &quot;file&quot;) final MultipartFile multipartFile) {
        Mono&lt;RequestID&gt; request =  service.sendFile(multipartFile);
        Response2&lt;String&gt; response = new Response&lt;&gt;(request.getRequestID, true, &quot;SUCCESS&quot;); // Response2 is just a POJO with three fields
        return new Mono&lt;ResponseEntity&lt;&gt;&gt;(response, HttpStatus.CREATED);
    }

答案1

得分: 0

You have to use map to transform the emitted data by Mono into another Mono

通过使用 map 来将Mono发射的数据转换为另一个Mono

so the logic will be

public Mono<String> sendFile(final MultipartFile multipartFile) {
Mono<Response> response = webClient.post()
            .uri(new URI("someurl"))
            .body(BodyInserters.fromMultipartData(multiValueMap))
            .retrieve()
            .bodyToMono(Response.class)
            .map(data->data.getRequestID().getRequestID());

  return response;

}

In the controller also use map method for data transformation into Mono<ResponseEntity>

@PostMapping("/request")
public Mono<ResponseEntity> sendRequest(@RequestPart(value = "file") final MultipartFile multipartFile) {
    Mono<RequestID> request =  service.sendFile(multipartFile);
    
    return request.map(id->ResponseEntity.body(new Response<>(id, true, "SUCCESS")).status(HttpStatus.CREATED));
}
英文:

You have to use map to transform the emitted data by Mono into another Mono

>Transform the item emitted by this Mono by applying a synchronous function to it.

so the logic will be

public Mono&lt;String&gt; sendFile(final MultipartFile multipartFile) {
Mono&lt;Response&gt; response = webClient.post()
            .uri(new URI(&quot;someurl&quot;))
            .body(BodyInserters.fromMultipartData(multiValueMap))
            .retrieve()
            .bodyToMono(Response.class)
            .map(data-&gt;data.getRequestID().getRequestID());

  return response;

}

In the controller also use map method for data transformation into Mono&lt;ResponseEntity&gt;

@PostMapping(&quot;/request&quot;)
public Mono&lt;ResponseEntity&gt; sendRequest(@RequestPart(value = &quot;file&quot;) final MultipartFile multipartFile) {
    Mono&lt;RequestID&gt; request =  service.sendFile(multipartFile);
    
    return request.map(id-&gt;ResponseEntity.body(new Response&lt;&gt;(id, true, &quot;SUCCESS&quot;)).status(HttpStatus.CREATED)));
}

huangapple
  • 本文由 发表于 2023年3月31日 02:28:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75891760.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定