我目前正在发送一个在响应中返回null正文的GET request。
@Service
public class CarService {
private RestTemplate restTemplate;
private final String url = "url";
private final String accessToken = "x";
@Autowired
public CarService () throws URISyntaxException {
restTemplate = new RestTemplate();
}
public void fetchCars() throws URISyntaxException {
HttpHeaders headers = new HttpHeaders();
headers.setBearerAuth(accessToken);
headers.setAccept(List.of(MediaType.APPLICATION_JSON));
ResponseEntity<CarList> carList = restTemplate.exchange
(RequestEntity.get(new URI(url)).headers(headers).build(), CarList.class);
}
}CarList.class如下所示:
@JsonIgnoreProperties(ignoreUnknown = true)
public class CarList{
private List<Car> carList;
public CarList(List<Car> carList) {
this.carList= carList;
}
public CarList() {
}
public List<Car> getCarList() {
return carList;
}
@Override
public String toString() {
return "CarList{" +
"carList=" + carList +
'}';
}
}Postman中的响应如下所示:
{
"cars": [
{
"carUid": "aaaa-ccc-dd-cc-ee",
"model": "hyundai",
"price": 20000,
"soldAt": "2021-09-24T22:10:15.307Z"
}
]
}我是不是遗漏了什么?这是我第一次尝试从客户端消费,所以考虑到我可能会错过的最基本的东西。
我已经用给定的accessToken在Postman中测试了GET请求,它工作得很好。
发布于 2021-09-25 18:09:02
您的carList对象是私有的,请尝试为该对象提供一个setter方法,如果约定良好,反序列化将正常工作。
@JsonIgnoreProperties(ignoreUnknown = true)
public class CarList{
private List<Car> carList;
public CarList(List<Car> carList) {
this.carList= carList;
}
public CarList() {
}
public List<Car> getCarList() {
return carList;
}
public setCarList(List<Car> carList) {
this.carList= carList;
}
@Override
public String toString() {
return "CarList{" +
"carList=" + carList +
'}';
}
}https://stackoverflow.com/questions/69328795
复制相似问题