I have written a java REST client using Jersey 2.17 The code looks like this:
public <T> T query(Class<T> responseType, Result previous) {
...
for(Map.Entry<String, String> entry : map.entrySet())
webTarget = webTarget.queryParam(entry.getKey(), entry.getValue());
return webTarget.request(MediaType.APPLICATION_JSON).get(responseType);
}
Code is working as expected except one thing. JSON object returned by the service contains a list of objects with such fields:
...
"_type": "Package"
"resourceId": "nimbusnodeweb-0.0.1_20141028083104790",
"_oid": "544f5468e4b0b148bedbcfed",
...
When I am getting my object back from the query method, those _type and _oid properties are set to null. The problem is that they are object identifiers. I can't figure out how to configure this WebTarget object to make it understand keys that start with underscore. My target java object looks like this:
public class PackageInfo {
private String _type; // = "Package"
private String resourceId;
private String _oid;
...
I even put two setters for those fields
/**
* @param _oid the _oid to set
*/
public void setOid(String _oid) {
this._oid = _oid;
}
/**
* @param _oid the _oid to set
*/
public void set_oid(String _oid) {
this._oid = _oid;
}
Nothing works. Any clues will be greatly appreciated.
I found the solution. Apparently, Jackson parser which is sitting underneath Jersey implementation is not up to par. I used Google GSON and it worked wonderfully. Here is the code:
// return webTarget.request(MediaType.APPLICATION_JSON).get(responseType);
Gson gson = new Gson();
Response response = webTarget.request(MediaType.APPLICATION_JSON).get();
InputStream entity = (InputStream) response.getEntity();
return gson.fromJson(new InputStreamReader(entity), responseType);