I have figured out that my GSON problems are all about the fact that, while my return type is not a parameterized object, it should have been. Now I need to use the Gson.fromJson method with the type of parameter to specify the return type so that GSON will take care of it for me.
I have created a generic class called RestResponse such:
public class RestResponse<T> {
private String errorMessage;
private int errorReason;
private T result;
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "RestResponse [errorMessage=" + errorMessage + ", result=" + result + "]";
}
/**
* Does this response contain an error?
* @return true if in error
*/
public boolean isInError(){
return getErrorMessage()!=null;
}
/**
* @return the errorMessage
*/
public String getErrorMessage() {
return errorMessage;
}
/**
* @param errorMessage the errorMessage to set
*/
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
/**
* The error reason code
* @return the errorReason
*/
public int getErrorReason() {
return errorReason;
}
/**
* The error reason code
* @param errorReason the errorReason to set
*/
public void setErrorReason(int errorReason) {
this.errorReason = errorReason;
}
/**
* The result of the method call
* @return the result or null if nothing was returned
*/
public final T getResult() {
return result;
}
/**
* The result of the method call
* @param result the result to set or null if nothing was returned
*/
public final void setResult(T result) {
this.result = result;
}
}
Now I want to create the resulting type on the other side. I have a generic method that I use to decode these things and either throw the exception or return the result.
So my method is like this:
public Object submitUrl(String url, Class<?> clazz) throws AjApiException {
Where clazz is the type that will be specified on RestResponse.
I then go to create the RestResponse before passing to GSON:
Type typeOfT = new TypeToken<RestResponse<clazz>>(){}.getType(); //1-->What goes here?
RestResponse<clazz> restResponse; //2-->and here?
And it errors. Can someone tell me what goes in these to places in place of clazz?