[英]Spring RestTemplate returns null object when trying to deserialize nested list of object
So this is the Json I am trying to convert to a Java bean 这就是我要转换为Java bean的Json
I am using jackson to bind JSON to my data objects 我正在使用杰克逊将JSON绑定到我的数据对象
{
"legend": {
"id": "379ec5d8c",
"name": "Stabil Koos",
"payers": [{
"id": "ab1651df-b835-495558-a2a5-2e6d42f7a41e",
"ranking": 1,
"buyer": {
"id": "67a6359838-0fda-43a6-9e2b-51a7d399b6a1",
"nationality": "en",
"stats": {
"gameeCount": 16581,
"count": 99098
}
}
},
{
"id": "379ecw555d8c",
"ranking": 2,
"buyer": {
"id": "2b451d0eda-ab0c-4345660-be3f-6ba3bebf1f81",
"nationality": "no",
"stats": {
"gamerCount": 1182,
"count": 7113
}
}
}
]
}
}
My beans look like this ; 我的豆看起来像这样;
public class League implements Serializable {
private String id;
private String name;
@JsonUnwrapped
private List<Payer> payers;
// getters and setters
Payers bean : 付款者豆:
public class Payers implements Serializable {
private String id;
private long ranking;
private Buyer buyer;
// getters and setters
I am using Rest Template and postForObject in Junit 我在Junit中使用Rest Template和postForObject
@Before
public void beforeTest() {
restTemplate = new RestTemplate();
headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
entity = new HttpEntity(REQUEST, headers);
}
And my final code to retrieve the object is : 我检索对象的最终代码是:
@Test
public void retrieveData() {
League league = restTemplate.postForObject(ENDPOINT_URL, entity, League.class);
System.out.println(legend);
}
The JSON you show is for an object that has a league
property which is a League
object and not a league object itself. 告诉你的JSON是具有对象
league
属性,它是一个League
对象,而不是一个联盟对象本身。 You need an additional response class: 您需要一个附加的响应类:
class LeagueResponse {
private League league;
League getLeague() { return league; }
}
and: 和:
LeagueResponse leagueResponse = restTemplate.postForObject(ENDPOINT_URL, entity, LeagueResponse.class);
League league = leagueResponse.getLeague();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.