You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Quarkus中向REST API传递QueryParams?

问题描述

我尝试调用一个REST API,但不确定目标端点是否正确携带了所需的Query参数。该API需要三个参数:其中两个在Java中设为常量,另一个作为Quarkus服务的Path参数传入,但外部服务要求这三个参数都以Query Params的形式传递。

预期的外部端点格式如下:
http://192.168.37.151:8897/api/customer-subscription/v31/subscription/?type=CED&id=0929853042&status=Active

我无法确认自己的服务是否正确构造了这样的端点,以下是我的代码实现:


REST Client接口

package com.conecel.qasecurity.config;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

@RegisterRestClient(baseUri = "http://192.168.37.151:8897/api/customer-subscription/v31/subscription/")
public interface IRestClient {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    Response getSubscriptionInfo(@QueryParam("type") String type,
                                 @QueryParam("id") String id,
                                 @QueryParam("status") String status);
}

服务层代码

package com.conecel.qasecurity.service;

import com.conecel.qasecurity.config.IRestClient;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.Response;
import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@ApplicationScoped
public class CustomerSubscriptionService implements ICustomerSubscription {

    private static final String TYPE = "CED";
    private static final String STATUS = "Active";

    @Inject
    @RestClient
    IRestClient restClient;

    private static final Logger LOGGER = LoggerFactory.getLogger(CustomerSubscriptionService.class);

    @Override
    public Response getSubscriptionInfo(String id) {
        try {
            LOGGER.info("Invocando a la API externa con ID: {}", id);
            // Formar la URL con los parámetros constantes y el parámetro dinámico
            Response response = restClient.getSubscriptionInfo(TYPE, id, STATUS);
            return Response.status(response.getStatus()).entity(response.getEntity()).build();
        } catch (Exception e) {
            LOGGER.error("Ocurrió un error al consumir el servicio: {}", e.getMessage());
            return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Error interno del servicio").build();
        }
    }
}

控制器代码

package com.conecel.qasecurity.controller;

import com.conecel.qasecurity.service.CustomerSubscriptionService;
import jakarta.inject.Inject;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;

@Path("/api")
public class CustomerSubscriptionController {

    @Inject
    private CustomerSubscriptionService customerSubscriptionService;

    @GET
    @Path("/customer-subscription/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getSubscriptionData(@PathParam("id") String id) {
        return customerSubscriptionService.getSubscriptionInfo(id);
    }
}

代码验证结论

你的代码实现是正确的,Quarkus的MicroProfile Rest Client会自动将@QueryParam注解的参数拼接成URL的查询参数:

  • @QueryParam("type")对应常量TYPE = "CED"
  • @QueryParam("id")对应从控制器Path参数传入的动态id值
  • @QueryParam("status")对应常量STATUS = "Active"

最终构造的外部请求URL完全符合你预期的格式。如果需要进一步确认,可以通过开启日志打印请求URL的方式验证:在application.properties中添加配置:

quarkus.rest-client.logging.scope=request-response
quarkus.rest-client.logging.body-limit=1024

这样就能在日志中看到完整的请求URL,确认Query参数是否正确携带。

内容的提问来源于stack exchange,提问作者Alexis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 12:17:32