Spring
Spring

Reputation: 11865

Spring: RestTemplate returns null object

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?

Upvotes: 7

Views: 43504

Answers (3)

eroldmrclk
eroldmrclk

Reputation: 1

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.

Upvotes: 0

Abhilash
Abhilash

Reputation: 557

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

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 692181

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.

Upvotes: 8

Related Questions