来自String的WebTarget

时间:2015-05-05 09:16:13

标签: java rest url jersey-client

我正在通过Jersey-Client使用REST API。 API是分页的。

Client client = ClientBuilder.newClient()
        .register(HttpAuthenticationFeature.basic(USER_NAME, PASSWORD));

WebTarget target = client.target(HOST).path(API_BASE).path("content");
PageResult pageResult = target.request(JSON).get()readEntity(PageResult.class);

响应看起来像这样:

{
    "results":[...
    ],
    "start": 0,
    "limit": 25,
    "size": 25,
    "_links": {
        "self": "http://localhost:8090/rest/api/content",
        "next": "/rest/api/content?limit=25&start=25",
        "base": "http://localhost:8090",
        "context": ""
    }
}

使用响应正确填充pageResult对象。 现在我想创建一个新的WebTarget或重用当前的WebTarget。更改的是查询参数。

创建新WebTarget的最简单方法是什么?

target = client.target(pageResult._links.base).path(pageResult._links.next);

这样就无法正确解释查询参数。我也不想自己解析网址。有什么API?我可以手动添加查询参数,但我认为应该有像WebTarget.fromString(pageResult._links.base + pageResult._links.next)

这样的方法

1 个答案:

答案 0 :(得分:0)

更改行

target = client.target(pageResult._links.base).path(pageResult._links.next);

target = client.target(pageResult._links.base + pageResult._links.next)

来自doku

/**
 * Build a new web resource target.
 *
 * @param uri web resource URI. May contain template parameters. Must not be {@code null}.
 * @return web resource target bound to the provided URI.
 * @throws IllegalArgumentException in case the supplied string is not a valid URI template.
 * @throws NullPointerException     in case the supplied argument is {@code null}.
 */
public WebTarget target(String uri);
相关问题