如何在 Rest Client - Quarkus 中动态接收查询参数的数量?

How can I receive dynamically number of query params in Rest Client - Quarkus?

我需要创建一个休息客户端来访问可以接收 0 或 n 个查询参数的 URI。

示例:

https://xpto?page=0&size=10&brand=abc&color=blue or
https://xpto?page=0&size=10&brand=abc or
https://xpto?page=0&size=10 or
https://xpto

我怎么能在这里做这个?

@RegisterRestClient
  public interface BarService {
  @GET
  @Path("/xpto")
  Response get(@QueryParam ...);
}

请参考,类似问题正在讨论中

您可以像下面这样定义您的客户端:

@RegisterRestClient
  public interface BarService {
  @GET
  @Path("/xpto")
  Response get(Map<String, String> queryParamMap);
}

并且您可以定义 ClientRequestFilter 以将 Map 转换为查询参数:

import java.io.IOException;
import java.util.Map;

import javax.ws.rs.HttpMethod;
import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.ClientRequestFilter;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.ext.Provider;

@Provider
public class QueryParamBuildingFilter implements ClientRequestFilter {

    @Override
    public void filter(ClientRequestContext requestContext) throws IOException {
        if (requestContext.getEntity() instanceof Map && requestContext.getMethod().equals(HttpMethod.GET)) {
            UriBuilder uriBuilder = UriBuilder.fromUri(requestContext.getUri());
            Map allParam = (Map)requestContext.getEntity();
            for (Object key : allParam.keySet()) {
                uriBuilder.queryParam(key.toString(), allParam.get(key));
            }
            requestContext.setUri(uriBuilder.build());
            requestContext.setEntity(null);
        }
    }

}