Spring WebFlux - 不可转换的类型;无法投射 'reactor.core.publisher.Mono'
Spring WebFlux - Inconvertible types; cannot cast 'reactor.core.publisher.Mono'
我正在为我的 API 使用 Spring 启动。我正在重写我的 API,以采用微服务架构。
我有 2 个 classes:
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;
}
在 Product 微服务中,我正在 API 调用 Ingredient 微服务:
// 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);
我收到以下错误:
Inconvertible types; cannot cast 'reactor.core.publisher.Mono<com.product.product.Entity.Ingredient>' to 'com.product.product.Entity.Ingredient'
解决上述问题的可能解决方案是什么?
只需使用 myProductComponentMono.block()
而不是将检索到的单声道转换为 Ingredient
,因为方法 myProduct.addProductIngredient(...)
中的代码仅接受 Ingredient
.[=14= 的对象]
我正在为我的 API 使用 Spring 启动。我正在重写我的 API,以采用微服务架构。
我有 2 个 classes:
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;
}
在 Product 微服务中,我正在 API 调用 Ingredient 微服务:
// 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);
我收到以下错误:
Inconvertible types; cannot cast 'reactor.core.publisher.Mono<com.product.product.Entity.Ingredient>' to 'com.product.product.Entity.Ingredient'
解决上述问题的可能解决方案是什么?
只需使用 myProductComponentMono.block()
而不是将检索到的单声道转换为 Ingredient
,因为方法 myProduct.addProductIngredient(...)
中的代码仅接受 Ingredient
.[=14= 的对象]