简体   繁体   English

如何使用 Spring RestTemplate 获得 JSON object?

[英]How do I get a JSON object with Spring RestTemplate?

I'm using swapi.dev API to get the data to my application in Spring Boot.我正在使用swapi.dev API 在 Spring 引导中将数据获取到我的应用程序。 I need to get information on a planet using its name.我需要使用其名称获取有关行星的信息。 Therefore, I use the next url: https://swapi.dev/api/planets/?search=Tatooine .因此,我使用下一个 url: https://swapi.dev/api/planets/?search=Tatooine The JSON result is writing below: 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/"
        }
    ]
}

Now, in Java, I use the next code in the service:现在,在 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
}

I only need to get the array of results, but, how do I get the array of results from a Object?我只需要获取结果数组,但是,如何从 Object 中获取结果数组?

Since you are using Spring Boot, it usually comes bundled with handy tools for JSON parsing.由于您使用的是 Spring 引导,它通常附带用于 JSON 解析的便捷工具。 Spring Boot wires per default jackson into your application. Spring 默认启动线 jackson 进入您的应用程序。

The first thing, you'll need is a (reduced) POJO model of the response.首先,您需要的是响应的(减少的)POJO model。

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

/* getter & setter ommitted */
}

In your calling code, use something like在您的调用代码中,使用类似

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

What happens behind the scenes?幕后会发生什么?

RestTemplate sends your request and tries to parse the response into your ResponsePojo object. RestTemplate 发送您的请求并尝试将响应解析为您的 ResponsePojo object。 Since the pojo is a reduced representation of the response, we provided the annotation @JsonIgnoreProperties(ignoreUnknown = true) .由于 pojo 是响应的简化表示,我们提供了注释@JsonIgnoreProperties(ignoreUnknown = true) This tells the parser, that it should simple ignore any field in the json, which cannot be mapped to your pojo.这告诉解析器,它应该简单地忽略 json 中的任何字段,这些字段无法映射到您的 pojo。 Since a field is provided, with the exact name like in json, the parser is able to identify and map them accordingly.由于提供了一个字段,其确切名称类似于 json,因此解析器能够相应地识别和 map 它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM