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

时间:2013-11-08 08:12:15

标签: 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请求?

感谢您提供帮助。

1 个答案:

答案 0 :(得分:3)

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

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