如何使用 Spring RestTemplate 获取 JSON 对象?
How do I get a JSON object with Spring RestTemplate?
我正在使用 swapi.dev API to get the data to my application in Spring Boot. I need to get information on a planet using its name. Therefore, I use the next url: 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
}
我只需要获取结果数组,但是,如何从对象获取结果数组?
由于您使用的是 Spring Boot,因此它通常与用于 JSON 解析的方便工具捆绑在一起。
Spring 将每个默认 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 中的确切名称,解析器能够相应地识别和映射它们。
我正在使用 swapi.dev API to get the data to my application in Spring Boot. I need to get information on a planet using its name. Therefore, I use the next url: 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
}
我只需要获取结果数组,但是,如何从对象获取结果数组?
由于您使用的是 Spring Boot,因此它通常与用于 JSON 解析的方便工具捆绑在一起。 Spring 将每个默认 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 中的确切名称,解析器能够相应地识别和映射它们。