1

I am trying to map JSON response as below:

{
    object: {
    id: 1
    name: "my name"
    email: "username@mail.com"
    username: "username"
    password: "password"
    mobile: "##########"
    fbAccessToken: "----------"
    img: null
    }
    errorMessage: ""
    successMessage: ""
    technicalErrorMessage: ""
    error: false
  }

so I wrote this method:

private <T> ResponseEntity<T> processedRequest(HttpRequestBase requestBase, Class<T> tClass) throws IOException {
        HttpResponse response = httpClient.execute(requestBase);
        HttpEntity entity = response.getEntity();
        Reader reader = new InputStreamReader(entity.getContent());

        Type type = new TypeToken<ResponseEntity<T>>() {}.getType();

        ResponseEntity<T> responseEntity = gson.fromJson(reader, type);

        return responseEntity;
    }

based on ResponseEntity class:

public class ResponseEntity<T> {

private T object;
private boolean isError;
private String errorMessage;
private String successMessage;
private String technicalErrorMessage;

public ResponseEntity() {
    setSuccessMessage("");
    setError(false);
    setErrorMessage("");
    setTechnicalErrorMessage("");
}

public T getObject() {
    return object;
}

public void setObject(T object) {
    this.object = object;
}

public boolean isError() {
    return isError;
}

public void setError(boolean error) {
    this.isError = error;
}

public String getErrorMessage() {
    return errorMessage;
}

public void setErrorMessage(String errorMessage) {
    this.errorMessage = errorMessage;
}

public String getTechnicalErrorMessage() {
    return technicalErrorMessage;
}

public void setTechnicalErrorMessage(String technicalErrorMessage) {
    this.technicalErrorMessage = technicalErrorMessage;
}

public String getSuccessMessage() {
    return successMessage;
}

public void setSuccessMessage(String successMessage) {
    this.successMessage = successMessage;
}}

but I am getting result as ResponseEntity<LinkedTreeMap> and the object is map of (Key->Value) not the actual mapped object that send by the Type ResponseEntity<T>.

The image below is what appeared in the debugger:

image from debugger

Racil Hilan
  • 24,690
  • 13
  • 50
  • 55
  • Can you be more clear what is the problem? The debugger seems to show the map as requested. – joshp Nov 20 '15 at 22:22
  • Possible duplicate of [Deserializing Generic Types with GSON](http://stackoverflow.com/questions/18397342/deserializing-generic-types-with-gson) – Dmitry Spikhalskiy Nov 20 '15 at 23:42
  • @joshp, thanks for your interest, the debugger show the object as TreeMap to the actual object. For example if the generic is instance of class UserDto the object of desalinized json must be object of UserDto to TreeMap (Key->Value) – Mohamed Hassan Nov 22 '15 at 21:44

1 Answers1

0

How GSON should understand, which class it should use in place of T and what is the "actual mapped object that send by the Type" to fill it's fields? We have type erasure for generics in Java, so no way in runtime to understand what it T. No way, so gson just uses generic Map<String, String>().

Take a look at responses to this question, it's the same situation.

Dmitry Spikhalskiy
  • 5,379
  • 1
  • 26
  • 40
  • thanks a lot for your response, i was wondering if their is work around to do that. or what is the best practice to do a generic Mapper from Json String to java Object in web service client ? – Mohamed Hassan Nov 22 '15 at 22:32