泽西岛客户。多值映射为空

时间:2014-10-06 11:02:08

标签: rest jersey jersey-client

我的RESTful客户端有这个方法:

public void testGetCateogrywiseData() {
        ClientConfig config = new DefaultClientConfig();
        Client client = Client.create(config);
        client.addFilter(new LoggingFilter(System.out));
        WebResource service = client
                .resource("http://localhost:8080/MyApp/rest/publicdata");

        @SuppressWarnings("rawtypes")
        MultivaluedMap queryParams = new MultivaluedMapImpl();
        queryParams.add("latitude", "18.522387");
        queryParams.add("longitude", "73.878437");
        queryParams.add("categoryID", "2");

        service.queryParams(queryParams);

        ClientResponse response = service.get(ClientResponse.class);

        System.out.println(response.getStatus());
        System.out.println("Form response " + response.getEntity(String.class));
    }

在服务器端,方法如下所示:

@Path("publicdata")
@GET
@Produces(MediaType.TEXT_HTML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String getPublicData() throws JSONException {
    MultivaluedMap<String, String> valueMap = uriInfo.getQueryParameters();
    Long latString = Long.parseLong(valueMap.getFirst("latitude"));
    Long lonString = Long.parseLong(valueMap.getFirst("longitude"));
    Long categoryId = Long.parseLong(valueMap.getFirst("categoryID"));

    // Do necessary stuff and return json string
    return null;
}

我的问题是服务器端的valueMap始终为空。它永远不会得到我从客户端代码发送的三个参数。我错过了什么?

1 个答案:

答案 0 :(得分:1)

问题出现在这一行:

service.queryParams(queryParams);

它成功添加了查询参数,但它不会更改原始service,它会向您返回一个新参数。为了使其工作,您需要更改为:

service = service.queryParams(queryParams);