如何解决尝试在 Quarkus 中进行 REST 调用时出错
我正在尝试在我拥有的另一个服务上执行请求。 我用来创建应用程序的指南是:
QUARKUS - Using the REST Client
我收到如下错误:
org.jboss.resteasy.spi.UnhandledException: java.lang.RuntimeException: Error injecting com.easy.ecomm.core.product.ProductClient com.easy.ecomm.core.cart.CartService.productClient
org.eclipse.microprofile.rest.client.RestClientDeFinitionException: Parameters and variables don't match on interface com.easy.ecomm.core.product.ProductClient::findProductById
这是 ProductClient 类
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
@Path("products")
@RegisterRestClient(configKey = "products-api")
public interface ProductClient {
@GET
@Path("{id}")
Product findProductById(String id);
}
这里是服务层:
import org.eclipse.microprofile.rest.client.inject.RestClient;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
@ApplicationScoped
public class CartService {
@Inject
@RestClient
ProductClient productClient;
public void addItem(String cartId,String productId,Integer amount){
// Code to find the cart on a queue.
Product product = findProduct(productId);
cart.getItems().add(new CartItem(amount,product));
}
private Product findProduct(String productId) {
return productClient.findProductById(productId);
}
}
和application.properties
:
products-api/mp-rest/url=http://localhost:8060
products-api/mp-rest/scope=javax.inject.Singleton
依赖关系与我们对指南 quarkus-rest-client
和 quarkus-rest-client-jackson
我已经尝试过的事情:
从 @RegisterRestClient
中删除 ConfigKey 并使用 application.properties
上的完整路径,如 here 所述在我的 POM.xml 中添加 Jandex 插件。
但是还是没有成功。每次更改都会给我相同的错误消息。
解决方法
您忘记使用 @PathParam
注释路径变量,这就是它无法实例化客户端的原因:
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
import javax.ws.rs.PathParam;
@Path("products")
@RegisterRestClient(configKey = "products-api")
public interface ProductClient {
@GET
@Path("{id}")
Product findProductById(@PathParam("id") String id);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。