Java Reference
In-Depth Information
public QuestionRetrievalService(String loc, String path, String search) {
this.loc = loc;
this.path = path;
this.search = search;
}
@Override
protected Task<ObservableList<Question>>createTask() {
return new Task<ObservableList<Question>>() {
@Override
protected ObservableList<Question> call() throws Exception {
Client client = ClientBuilder.newClient();
WebTarget target = client.target(loc).path(path).queryParam("tagged", search).
queryParam("site", "stackoverflow");
QuestionResponse response = target.request(MediaType.APPLICATION_JSON).
get(QuestionResponse.class);
return FXCollections.observableArrayList(response.getItem());
}
};
}
}
To show one of the nice tools of JAX-RS, we slightly modified the constructor of the
QuestionRetrievalService
to take three parameters:
public QuestionRetrievalService(String host, String path, String search);
This is because JAX-RS allows us to use the
Builder
pattern to construct REST resources, allowing a distinction
among hostname, path, query parameters, and others.
As a consequence, we have to make a slight modification in Listing 11-13:
Service<ObservableList<Question>>service = new QuestionRetrievalService(url);
is replaced by
String url = "
http://api.stackexchange.com/
"
;
String path = "2.2/search";
String search = "javafx";
Service<ObservableList<Question>>service = new QuestionRetrievalService(url, path, search);
The hostname, path, and search parameter are used to create a JAX-RS
WebTarget
:
Client client = ClientBuilder.newClient();
WebTarget target = client.target(loc).path(path).queryParam("tagged", search)
.queryParam("site", "stackoverflow");
Search WWH ::
Custom Search