Partner – Microsoft – NPI (cat= Spring)
announcement - icon

Azure Spring Apps is a fully managed service from Microsoft (built in collaboration with VMware), focused on building and deploying Spring Boot applications on Azure Cloud without worrying about Kubernetes.

And, the Enterprise plan comes with some interesting features, such as commercial Spring runtime support, a 99.95% SLA and some deep discounts (up to 47%) when you are ready for production.

>> Learn more and deploy your first Spring Boot app to Azure.

You can also ask questions and leave feedback on the Azure Spring Apps GitHub page.

Course – LS (cat=JSON/Jackson)

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

Course – LS (cat=HTTP Client-Side)

Get started with Spring and Spring Boot, through the Learn Spring 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’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:

  .map(object -> mapper.convertValue(object, User.class))

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();

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

This method takes a ParameterizedTypeReference produced by an anonymous inner class:

ResponseEntity<List<User>> responseEntity =
    new ParameterizedTypeReference<List<User>>() {}
List<User> users = responseEntity.getBody();

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<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. 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.

Course – LS (cat=Spring)

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

Course – LS (cat=JSON/Jackson)

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

Course – LS (cat=HTTP Client-Side)

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

res – HTTP Client (eBook) (cat=Http Client-Side)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.