Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

NPI – Lightrun – Spring (partner)

We rely on other people’s code in our own work. Every day. It might be the language you’re writing in, the framework you’re building on, or some esoteric piece of software that does one thing so well you never found the need to implement it yourself.

The problem is, of course, when things fall apart in production - debugging the implementation of a 3rd party library you have no intimate knowledge of is, to say the least, tricky. It’s difficult to understand what talks to what and, specifically, which part of the underlying library is at fault.

Lightrun is a new kind of debugger.

It's one geared specifically towards real-life production environments. Using Lightrun, you can drill down into running applications, including 3rd party dependencies, with real-time logs, snapshots, and metrics. No hotfixes, redeployments, or restarts required.

Learn more in this quick, 5-minute Lightrun tutorial:

>> The Essential List of Spring Boot Annotations and Their Use Cases

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

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

HTTPClient footer
Comments are closed on this article!