Database Reference
In-Depth Information
public SearchObject getSearchResultObject() throws
Exception {
try{
InputStream source = getResultStream(search_url);
Reader reader = new InputStreamReader(source);
Gson gson = new Gson();
SearchObject response =
gson.fromJson(reader,SearchObject.class);
...
reader.close();
return response;
}
catch (Exception e) {
log.error(getClass().getSimpleName(), "Error for
URL "+ search_url, e);
}
return null;
}
Surely SearchObject is completely based on the IMDb JSON response. Constructing
it will also require no time, thanks to the Google library, jsonschema2pojo . Just in-
stall it, save the IMDb JSON in a file, and do the conversion (use your own parameters if
needed), using the following command line:
jsonschema2pojo --source imdb.json --target java-gen -R -a
GSON -T JSON
You will find a complete class hierarchy in the java-gen folder, suitable for consump-
tion by Gson. Well, not exactly suitable, to be honest. What we just demonstrated is not
exactly the code-first approach, as we have generated classes from the message, but this is
not a Canonical Schema (Message) by any standard, and anyone who looked at the
Google YouTube JSON, for example, knows how many "specific" things are in there.
IMDb is not an exception. Without compromising the classes' structure and hierarchy, you
really can make POJOs more logical and compact, and we advise you to do that. Anyway,
the previous code will work right away, but is not advisable from the SOA's standpoint.
To complete the following example, we demonstrate the getResultStream function.
It's entirely based on the Apache HTTP client and exceedingly simple (thanks to the
Apache community):
Search WWH ::




Custom Search