java - 在 GET 请求中将 java.util.Set 作为 QueryParam 值传递

标签 java rest curl cxf

如何向以下服务发出 GET 请求:

public class MyService {

  @GET
  @Path("/trendingcontent/home")
  Map<String, Object> getTrendingContentHome(@QueryParam("max") @DefaultValue("5") int max,
    @QueryParam("containerType") @DefaultValue("-1") int containerType,
    @QueryParam("containerID") @DefaultValue("-1") long containerID,
    @QueryParam("contentTypes") Set<Integer> objectTypes) {

    if (recommendationManager.isEnabled()) {
            EntityDescriptor descriptor = null;
            if (containerType > 0 && containerID > 0) {
                // only set the descriptor if the container is not root
                Community root = communityManager.getRootCommunity();
                if (containerType != root.getObjectType() || containerID != root.getID()) {
                    descriptor = new EntityDescriptor(containerType, containerID);
                }
            }

            RecommendationQuery query;
            if (objectTypes == null || objectTypes.isEmpty()) {
                objectTypes = recommendationQueryHelper.getContainableTypes();
            }

            if (descriptor == null) {
                query = recommendationQueryHelper.getSystemTrendingContent(objectTypes);
            }
            else {
                query = recommendationQueryHelper.getContainerTrendingContent(objectTypes, Sets.newHashSet(descriptor));
            }

            return getRecommendationResponse(query, max, home);
        }
        else {
            return getPopularContent(max, home);
        }

  }

}

我正在使用这个:

curl 'http://localhost:8080/__services/v2/rest/recommendation/trendingcontent/home/3/2020/1/1100' -H 'Host: localhost:8080' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Firefox/24.0' -H 'Accept: application/json, text/javascript, */*; q=0.01' -H 'Accept-Language: en-US,en;q=0.5' -H 'Accept-Encoding: gzip, deflate' -H 'X-J-Token: 6430c792bc77967ce8c7' -H 'X-Requested-With: XMLHttpRequest'

我关心的是如何将 Set 作为 QueryParam 传递。是否可以使用传递的 http 数据发出 GET 请求?

感谢您的帮助。

最佳答案

您将查询参数作为路径参数传递。正确的curl调用是:

http://localhost:8080/__services/v2/rest/recommendation/trendingcontent/home?max=3&containerType=2020&containerID=1&contentTypes=1100

如果您需要多个contentTypes,请使用:

http://localhost:8080/__services/v2/rest/recommendation/trendingcontent/home?max=3&containerType=2020&containerID=1&contentTypes=1100&contentTypes=22340&contentTypes=43000

关于java - 在 GET 请求中将 java.util.Set 作为 QueryParam 值传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19854330/

相关文章:

java - 频繁向客户端发送服务器时间的正确方法是什么?

java - GridBagLayout 无法正确布局

java - 如何使用 Jersey 2 测试框架为此类编写单元测试

带有while循环的php多个curl url

php - 使用带有 curl 的网络代理

ssl - 将 curl 与没有密码的证书一起使用

java - Android Studio mailto Intent 不显示主题和邮件正文

Java NIO : How to know when SocketChannel read() is complete with non-blocking I/O

java - 如何接受 JAX-RS( Jersey )中的 Base64 编码数据?

java - 使用 RESTeasy 管理属性继承