java - 尝试在 Quarkus 中进行 REST 调用时出错

标签 java rest cdi quarkus-rest-client

我正在尝试在我拥有的另一项服务上执行请求。 我用来创建应用程序的指南是:

QUARKUS - Using the REST Client

QUARKUS - CDI Reference

QUARKUS - Workshop

我收到如下错误:

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-clientquarkus-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);

}

关于java - 尝试在 Quarkus 中进行 REST 调用时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66450546/

相关文章:

android - 使用请求 header 在 Android 中访问 REST API

java - Java 服务器端应用程序的静态依赖注入(inject)解决方案?

java - @Inject 来自 EJB 项目的 bean 给出 UnsatisfiedResolutionException

java - Spring框架和tomcat - 如何启用以下文件夹符号链接(symbolic link)?

Java 获取引用 URI?

Java:在 X-Y 网格上绘制 .flac 音频文件的左声道和右声道。 (示波器)

c - C 中的 REST 客户端库

java - 休息模板错误 403

jakarta-ee - 为什么使用 CDI 进行 self 注入(inject)不会导致无限循环?

java - 接口(interface)模拟工作不正常