Spring: RestTemplate returns null object

40.4k Views Asked by At

With the below GET request:

ResponseEntity<String> entity = restTemplate.exchange(uri, HttpMethod.GET, requestEntity, String.class );
entity.getBody();

returns a JSON String like this:

{"userRegistrations":[{"userRegistrationToken":"fb398972","userRegistrationTokenAlias":"87f15f8"}]}

But I want to make this work with an object not with a string. So with the code below I receive a UserRegistrations object with a null UserTokenResponse List

ResponseEntity<UserRegistrations> entity = restTemplate.exchange(uri, HttpMethod.GET, requestEntity, UserRegistrations.class );
entity.getBody();

And my domain class looks like this:

public class UserRegistrations {
    List<UserTokenResponse> userRegistrationList;
    //..getters and setters
}

public class UserTokenResponse {
   private String userRegistrationToken;
   private String userRegistrationTokenAlias;
   //getters and setters
}

What am I missing?

3

There are 3 best solutions below

2
On BEST ANSWER

Assuming you're using Jackson, RestTemplate automatically registers a MappingJackson2HttpMessageConverter which configures the underlying ObjectMapper to ignore unknown properties.

The JSON object has a single attribute named userRegistrations, whereas your Java class has a single attribute named userRegistrationList. They don't match.

They need to match, or you need to add a @JsonProperty annotation of the attribute to make Jackson serialize/parse it as userRegistrations.

0
On

I encountered a similar error and it was returning null too. The problem is over when Object.class is replaced with the name of the class we want to convert on the client side.

Like that:

Token = restTemplate.exchange(uri, HttpMethod.POST, request, Object.class);

the problem was probably due to the fact that it is not directly compatible with the class we want to convert.

0
On

This happens when your class property names doesn't match with the JSON property names coming in the response. For instance take the below example

    public class ScheduledCallbacks {

    private List<Callback> callbacks;

    public List<Callback> getCallbacks() {
        return callbacks;
    }

    public void setCallbacks(List<Callback> callbacks) {
        this.callbacks = callbacks;
    }

    @Override
    public String toString() {
        return "ScheduledCallbacks [callbacks=" + callbacks + "]";
    }
}

and if the response is the following way

{
  "scheduledCallbacks": [
    {
      "sessionId": "string",
      "customerNbr": "string",
      "topicName": "string",
      "desiredTime": "string",
      "callbackState": "string",
      "serviceName": "string",
      "expirationTime": "string",
      "programCode": "string"
    }
  ]
}

Then you get null response because the name scheduledCallbacks in the JSON response doesn't match with the name callbacks in class.

But if your class is as following

public class ScheduledCallbacks {

    private List<Callback> scheduledCallbacks;

    public List<Callback> getScheduledCallbacks() {
        return scheduledCallbacks;
    }
    public void setScheduledCallbacks(List<Callback> scheduledCallbacks) {
        this.scheduledCallbacks = scheduledCallbacks;
    }
    @Override
    public String toString() {
        return "ScheduledCallbacks [scheduledCallbacks=" + scheduledCallbacks + "]";
    }
}

Then you get the expected response in response entity