Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring MVC-仅从外部json获取单个对象_Json_Spring - Fatal编程技术网

Spring MVC-仅从外部json获取单个对象

Spring MVC-仅从外部json获取单个对象,json,spring,Json,Spring,我需要使用RESTApi,它会给我一个JSON输出,如下所示 { "id": "e5d5ccc0-8da4-430d-b0ec-096d17ae2af8", "car": [ { "identifier": "XX000YY", "formattedAddress": "Address 1", "lat": 45.841664, "lng": 18.199905, "isOn": false, "odometer": 763.4, } ], "l

我需要使用RESTApi,它会给我一个JSON输出,如下所示

{
"id": "e5d5ccc0-8da4-430d-b0ec-096d17ae2af8",
"car": [
{
    "identifier": "XX000YY",
    "formattedAddress": "Address 1",
    "lat": 45.841664,
    "lng": 18.199905,
    "isOn": false,
    "odometer": 763.4,

}
],
"location": "92589f4a-8c6e-4494-8548-b5428f8fa598"
}
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<Wrapper> response = restTemplate.exchange(url, HttpMethod.POST, httpEntity,wrapper);
通常我会创建一个包装器对象

public class Wrapper{

private String id;

private Car car;

private String location;

//getters and setters

}
然后在我的控制器中,我会这样做

{
"id": "e5d5ccc0-8da4-430d-b0ec-096d17ae2af8",
"car": [
{
    "identifier": "XX000YY",
    "formattedAddress": "Address 1",
    "lat": 45.841664,
    "lng": 18.199905,
    "isOn": false,
    "odometer": 763.4,

}
],
"location": "92589f4a-8c6e-4494-8548-b5428f8fa598"
}
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<Wrapper> response = restTemplate.exchange(url, HttpMethod.POST, httpEntity,wrapper);
RestTemplate RestTemplate=new RestTemplate();
ResponseEntity response=restemplate.exchange(url、HttpMethod.POST、httpEntity、包装器);
得到回应


但基本上我只需要
Car
对象,所以我想如果有办法只返回it而不是整个包装器,并从中获取
Car
对象,那么首先,您必须创建一个可序列化的对象,如下所示:

Car.java

package com.wrapper;


public class Car implements Serializable{

public String identifier;
public String formattedAddress;
public Float lat;
public Float lng;
public Boolean isOn;
public Float odometer;

}
package com.wrapper;

import java.util.List;

public class Wrapper implements Serializable{

public String id;
public List<Car> car = null;
public String location;

}
Wrapper.java

package com.wrapper;


public class Car implements Serializable{

public String identifier;
public String formattedAddress;
public Float lat;
public Float lng;
public Boolean isOn;
public Float odometer;

}
package com.wrapper;

import java.util.List;

public class Wrapper implements Serializable{

public String id;
public List<Car> car = null;
public String location;

}