Get list of JSON objects with Spring RestTemplate – 用Spring RestTemplate获取JSON对象的列表

最后修改: 2020年 12月 18日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Our services often have to communicate with other REST services in order to fetch information.

我们的服务经常需要与其他REST服务进行通信,以获取信息。

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.

在Spring中,我们可以使用RestTemplate来执行同步的HTTP请求。数据通常以JSON形式返回,而RestTemplate可以为我们转换它。

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.

在本教程中,我们将探讨如何在Java中把一个JSON数组转换成三种不同的对象结构。Array of ObjectArray of POJOList of POJO。

2. JSON, POJO and Service

2.JSON、POJO和服务

Let’s imagine that we have an endpoint http://localhost:8080/users returning a list of users as the following JSON:

让我们想象一下,我们有一个端点http://localhost:8080/users,以下列JSON格式返回一个用户列表。

[{
  "id": 1,
  "name": "user1",
}, {
  "id": 2,
  "name": "user2"
}]

We’ll require the corresponding User class to process data:

我们将需要相应的User 类来处理数据。

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:

对于我们的接口实现,我们编写了一个UserConsumerServiceImpl ,将RestTemplate 作为其依赖。

public class UserConsumerServiceImpl implements UserConsumerService {

    private final RestTemplate restTemplate;

    public UserConsumerServiceImpl(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

...
}

3. Mapping a List of JSON Objects

3.映射一个JSON对象的列表

When the response to a REST request is a JSON array, there are a few ways we can convert it to a Java collection.

当REST请求的响应是一个JSON数组时,我们有几种方法可以将其转换为一个Java集合。

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.

让我们看看这些选项,看看它们是如何轻松地让我们处理返回的数据的。我们将看看如何提取由REST服务返回的一些用户对象的用户名。

3.1. RestTemplate With Object Array

3.1.RestTemplate与对象阵列

First, let’s make the call with RestTemplate.getForEntity and use a ResponseEntity of type Object[] to collect the response:

首先,让我们用RestTemplate.getForEntity进行调用,并使用Object[]类型的 ResponseEntity来收集响应。

ResponseEntity<Object[]> responseEntity =
   restTemplate.getForEntity(BASE_URL, Object[].class);

Next, we can extract the body into our array of Object:

接下来,我们可以将主体提取到我们的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.

这里实际的Object只是一些任意的结构,包含了我们的数据,但没有使用我们的User类型。让我们把它转换成我们的User对象。

For this, we’ll need an ObjectMapper:

为此,我们将需要一个ObjectMapper

ObjectMapper mapper = new ObjectMapper();

We can declare it inline, though this is usually done as a private static final member of the class.

我们可以内联声明,尽管这通常是作为类的private static final成员进行的。

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.

通过这个方法,我们基本上可以将一个anything数组读成Java中的Object数组。例如,如果我们只想计算结果,这就很方便了。

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.

Jackson反序列化器实际上是将JSON反序列化为一系列LinkedHashMap对象,当我们要求它产生Object作为目标类型。使用convertValue进行后处理是一种低效的开销。

We can avoid it if we provide our desired type to Jackson in the first place.

如果我们一开始就向杰克逊提供我们想要的类型,我们就可以避免它。

3.2. RestTemplate With User Array

3.2.RestTemplate与用户阵列

We can provide User[]  to RestTemplate, instead of Object[]:

我们可以向RestTemplate提供User[] ,而不是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.

我们可以看到,我们不再需要ObjectMapper.convertValueResponseEntity里面有User对象。但是我们仍然需要做一些额外的转换,以使用Java Stream API,并使我们的代码能够与List一起工作。

3.3. RestTemplate With User List and ParameterizedTypeReference

3.3.RestTemplate带有用户列表和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.

如果我们需要Jackson产生一个List of Users而不是一个数组的便利,我们需要描述我们想要创建的List。要做到这一点,我们必须使用RestTemplate.exchange

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

这个方法接收一个由匿名内部类产生的ParameterizedTypeReference

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.

这将产生我们想要使用的List

Let’s have a closer look into why we need to use the ParameterizedTypeReference.

让我们仔细看看为什么我们需要使用 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.

在前两个例子中,Spring可以轻松地将JSON反序列化为User.class类型标记,其中类型信息在运行时完全可用。

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

然而,有了泛型,如果我们试图使用List<User>.class,就会发生类型抹杀。所以,Jackson将无法确定<>里面的类型。

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.

我们可以通过使用一个名为ParameterizedTypeReference的超级类型标记来克服这个问题。将其实例化为匿名内类–new ParameterizedTypeReference<List<User>>() {}–利用了泛型类的子类包含编译时类型信息的事实,这些信息不受类型清除的影响,可以通过反射消耗。

4. Conclusion

4.总结

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.

在这篇文章中,我们看到了使用RestTemplate处理JSON对象的三种不同方式。我们看到了如何指定Object和我们自己的自定义类的数组的类型。

Then we learned how we provide the type information to produce a List by using the ParameterizedTypeReference.

然后我们学习了如何通过使用ParameterizedTypeReference来提供类型信息以产生一个List

As always, the code for this article is available over on GitHub.

一如既往,本文的代码可在GitHub上获得over