Spring Top

Get started with Spring 5 and Spring Boot 2, through the reference Learn Spring course:

>> LEARN SPRING
Jackson Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE

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 are going to explore how we can convert a JSON Array into three different object structures in Java: Array of ObjectArray 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. However, 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 ParameterisedTypeReference.

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<User>.class. So, Jackson would not be able to determine the type inside the <>.

We can overcome this by using a super type token called ParameterizedTypeReference. Instantiating it as an anonymous inner class – new ParameterizedTypeReference<List<User>>() {} – exploits the fact that subclasses of generic classes contain compile-time type information that is not subject to type erasure and can be consumed through reflection.

4. Summary

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.

Spring bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> THE COURSE
Jackson bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
HTTPClient footer
Comments are closed on this article!