英文:
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<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);
}
答案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<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)));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论