简体   繁体   English

字符串中的WebTarget

[英]WebTarget from String

I'm consuming a REST API via Jersey-Client. 我正在通过Jersey-Client使用REST API。 The API is paginated. 该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);

The response looks something like this: 响应看起来像这样:

{
    "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": ""
    }
}

The pageResult object is populated correctly with the response. 用响应正确填充了pageResult对象。 Now I want to create a new WebTarget or reuse the current WebTarget . 现在,我想创建一个新的WebTarget或重用当前的WebTarget What has changed are the query parameters. 更改的是查询参数。

What is the easiest way to create the new WebTarget ? 创建新的WebTarget的最简单方法是什么?

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

This way the query parameters are not interpreted correctly. 这样,查询参数将无法正确解释。 I also don't want to parse the url by myself. 我也不想自己解析网址。 What API is there? 有什么API? I could add the query parameters by hand, but I think there should be a method like WebTarget.fromString(pageResult._links.base + pageResult._links.next) 我可以手动添加查询参数,但我认为应该有一个类似WebTarget.fromString(pageResult._links.base + pageResult._links.next)

Change the line 换线

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

to

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

From the 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM