![](/img/trans.png)
[英]Bad return type in method reference: cannot convert reactor.core.publisher.Mono<S> to reactor.core.publisher.Mono<? extends R>
[英]Spring WebFlux - Inconvertible types; cannot cast 'reactor.core.publisher.Mono'
我正在為我的 API 使用 Spring 引導。 我正在重寫我的 API,以采用微服務架構。
我有 2 節課:
1) 產品
2) 成分
我的代碼:
這是我的產品class:
public class Product{
private String name;
@OneToMany
private List<Ingredient> productIngredients; //Ingredient
private Double quantity = 0.0;
private Double productCost = 0.0;
public void addIngredient(Ingredient myIngredient){
this.productComponents.add(myIngredient);
}
}
這是我的成分class:
public class Ingredient{
private String name;
private String unit;
private Double quantity = 0.0;
}
在產品微服務中,我正在對成分微服務進行 API 調用:
// Making a call to the Ingredients microservice from the product microservice
WebClient myWebClient = WebClient.create("http://localhost:8090/api");
@PostMapping("/component/list/add/{id}")
public Mono<Ingredient> addProductComponent(@PathVariable Long id){
Product myProduct = new Product();
Mono<Ingredient> myProductComponentMono =
myWebClient
.get()
.uri("/components/component/get/{"+ id +'}')
.retrieve()
.bodyToMono(Ingredient.class);
return myProduct.addProductIngredient((Ingredient) myIngredientMono); //this is the line where I am getting the error.
}
這是我在上述方法中得到錯誤的行:
return myProduct.addProductIngredient((Ingredient) myProductComponentMono);
我收到以下錯誤:
不可轉換的類型; 無法將“reactor.core.publisher.Mono<com.product.product.Entity.Ingredient>”轉換為“com.product.product.Entity.Ingredient”
解決上述問題的可能解決方案是什么?
只需使用myProductComponentMono.block()
而不是將檢索到的 mono 轉換為Ingredient
,因為您在方法myProduct.addProductIngredient(...)
中的代碼只接受 object 的Ingredient
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.