I'm consuming a REST API via Jersey-Client. The API is paginated.
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. Now I want to create a new WebTarget
or reuse the current WebTarget
. What has changed are the query parameters.
What is the easiest way to create the new 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? 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)
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.