1. Overview
Our services often have to communicate with other REST services in order to fetch information.
In Spring, we can use RestTemplate to perform synchronous HTTP requests. The data is usually returned as JSON, and RestTemplate can convert it for us.
In this tutorial, we’ll explore how we can convert a JSON Array into three different object structures in Java: Array of Object, Array of POJO and a List of POJO.
2. JSON, POJO and Service
Let’s imagine that we have an endpoint http://localhost:8080/users
returning a list of users as the following JSON:
[{
"id": 1,
"name": "user1",
}, {
"id": 2,
"name": "user2"
}]
We’ll require the corresponding User class to process data:
public class User {
private int id;
private String name;
// getters and setters..
}
For our interface implementation, we write a UserConsumerServiceImpl with RestTemplate as its dependency:
public class UserConsumerServiceImpl implements UserConsumerService {
private final RestTemplate restTemplate;
public UserConsumerServiceImpl(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
...
}
3. Mapping a List of JSON Objects
When the response to a REST request is a JSON array, there are a few ways we can convert it to a Java collection.
Let’s look at the options and see how easily they allow us to process the data that is returned. We’ll look at extracting the usernames of some user objects returned by a REST service.
3.1. RestTemplate With Object Array
First, let’s make the call with RestTemplate.getForEntity and use a ResponseEntity of type Object[] to collect the response:
ResponseEntity<Object[]> responseEntity =
restTemplate.getForEntity(BASE_URL, Object[].class);
Next, we can extract the body into our array of Object:
Object[] objects = responseEntity.getBody();
The actual Object here is just some arbitrary structure that contains our data but doesn’t use our User type. Let’s convert it into our User objects.
For this, we’ll need an ObjectMapper:
ObjectMapper mapper = new ObjectMapper();
We can declare it inline, though this is usually done as a private static final member of the class.
Lastly, we are ready to extract the usernames:
return Arrays.stream(objects)
.map(object -> mapper.convertValue(object, User.class))
.map(User::getName)
.collect(Collectors.toList());
With this method, we can essentially read an array of anything into an Object array in Java. This can be handy if we only wanted to count the results, for instance.
However, it doesn’t lend itself well to further processing. We had to put extra effort into converting it to a type we could work with.
The Jackson Deserializer actually deserializes JSON into a series of LinkedHashMap objects when we ask it to produce Object as the target type. Post-processing with convertValue is an inefficient overhead.
We can avoid it if we provide our desired type to Jackson in the first place.
3.2. RestTemplate With User Array
We can provide User[] to RestTemplate, instead of Object[]:
ResponseEntity<User[]> responseEntity =
restTemplate.getForEntity(BASE_URL, User[].class);
User[] userArray = responseEntity.getBody();
return Arrays.stream(userArray)
.map(User::getName)
.collect(Collectors.toList());
We can see that we no longer need the ObjectMapper.convertValue. The ResponseEntity has User objects inside it. But we still need to do some extra conversions to use the Java Stream API and for our code to work with a List.
3.3. RestTemplate With User List and ParameterizedTypeReference
If we need the convenience of Jackson producing a List of Users instead of an Array, we need to describe the List we want to create. To do this, we have to use *RestTemplate.*exchange.
This method takes a ParameterizedTypeReference produced by an anonymous inner class:
ResponseEntity<List<User>> responseEntity =
restTemplate.exchange(
BASE_URL,
HttpMethod.GET,
null,
new ParameterizedTypeReference<List<User>>() {}
);
List<User> users = responseEntity.getBody();
return users.stream()
.map(User::getName)
.collect(Collectors.toList());
This produces the List that we want to use.
Let’s have a closer look into why we need to use the ParameterizedTypeReference.
In the first two examples, Spring can easily deserialize the JSON into a User.class type token where the type information is fully available at runtime.
With generics, however, type erasure occurs if we try to use List
We can overcome this by using a super type token called ParameterizedTypeReference. Instantiating it as an anonymous inner class — new ParameterizedTypeReference<List
4. Conclusion
In this article, we saw three different ways of processing JSON objects using RestTemplate. We saw how to specify the types of arrays of Object and our own custom classes.
Then we learned how we provide the type information to produce a List by using the ParameterizedTypeReference.
As always, the code for this article is available over on GitHub.