【问题标题】:How do I get a JSON object with Spring RestTemplate?如何使用 Spring RestTemplate 获取 JSON 对象?
【发布时间】:2021-08-10 10:34:12
【问题描述】:

我正在使用swapi.dev API 将数据获取到我在 Spring Boot 中的应用程序。我需要使用其名称获取有关行星的信息。因此,我使用下一个网址:https://swapi.dev/api/planets/?search=Tatooine。 JSON 结果如下:

{
    "count": 1, 
    "next": null, 
    "previous": null, 
    "results": [
        {
            "name": "Tatooine", 
            "rotation_period": "23", 
            "orbital_period": "304", 
            "diameter": "10465", 
            "climate": "arid", 
            "gravity": "1 standard", 
            "terrain": "desert", 
            "surface_water": "1", 
            "population": "200000", 
            "residents": [
                "http://swapi.dev/api/people/1/", 
                "http://swapi.dev/api/people/2/", 
                "http://swapi.dev/api/people/4/", 
                "http://swapi.dev/api/people/6/", 
                "http://swapi.dev/api/people/7/", 
                "http://swapi.dev/api/people/8/", 
                "http://swapi.dev/api/people/9/", 
                "http://swapi.dev/api/people/11/", 
                "http://swapi.dev/api/people/43/", 
                "http://swapi.dev/api/people/62/"
            ], 
            "films": [
                "http://swapi.dev/api/films/1/", 
                "http://swapi.dev/api/films/3/", 
                "http://swapi.dev/api/films/4/", 
                "http://swapi.dev/api/films/5/", 
                "http://swapi.dev/api/films/6/"
            ], 
            "created": "2014-12-09T13:50:49.641000Z", 
            "edited": "2014-12-20T20:58:18.411000Z", 
            "url": "http://swapi.dev/api/planets/1/"
        }
    ]
}

现在,在 Java 中,我使用服务中的下一个代码:

public PlanetDTO getPlanetByName(String name){
   String url = "https://swapi.dev/api/planets/?search=Tatooine";
   RestTemplate restTemplate = new RestTemplate();
   Object object = restTemplate.getForObject(url, Object.class);
   // I don't know how to get the array of results
}

我只需要获取结果数组,但是,如何从对象中获取结果数组?

【问题讨论】:

  • 如果我没记错的话,你唯一需要做的就是在课堂上指出,你要映射,它不是一个单一的,它是一个数组。您可以使用对象 来实现。如果您需要列表,则使用 ParameterizedTypeReference 类或类。它会类似于 ....(getForObject(url, new ParameterizedTypeReference())
  • 但是 url 返回一个 JSON 对象。我想要得到的是该对象@AL 的数组

标签: java spring-boot rest resttemplate


【解决方案1】:

由于您使用的是 Spring Boot,因此它通常捆绑了方便的 JSON 解析工具。 Spring Boot 将每个默认 jackson 连接到您的应用程序中。

首先,您需要的是响应的(简化的)POJO 模型。

@JsonIgnoreProperties(ignoreUnknown = true)
public class ResponsePojo {
   @JsonProperty("<jsonFieldName>") // only required, if fieldName != jsonFieldName
   private List<String> residents; 

/* getter & setter ommitted */
}

在你的调用代码中,使用类似

ResponsePojo response = restTemplate.getForObject(url, ResponsePojo.class);
response.getResidents() gives you access to the contents of 'resident' array

幕后发生了什么?

RestTemplate 发送您的请求并尝试将响应解析为您的 ResponsePojo 对象。 由于 pojo 是响应的简化表示,我们提供了注释 @JsonIgnoreProperties(ignoreUnknown = true)。 这告诉解析器,它应该简单地忽略 json 中的任何字段,这些字段不能映射到您的 pojo。由于提供了一个字段,其名称与 json 中的确切名称类似,因此解析器能够相应地识别和映射它们。

【讨论】:

  • 是的。我使用 POJO 来获取结果数组。谢谢你的解释!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-19
  • 2021-11-07
  • 1970-01-01
  • 1970-01-01
  • 2017-02-20
  • 2017-06-30
  • 1970-01-01
相关资源
最近更新 更多