我收到来自服务器的以下回复
{
"count":2,
"data":[
{
"promotionId":"5460d40e3956e9b365e24864",
"promotionName":"happy diwali",
"description":"happy diwali",
"startDate":null,
"endDate":null,
"createdDate":null,
"modifiedDate":null,
"creatorUserId":null,
"brandId":"545a07e1eb013d0eeb8ad282"
},
{
"promotionId":"5460d45d3956e9b365e24865",
"promotionName":"happy new YEAR",
"description":"happy new YEAR",
"startDate":null,
"endDate":null,
"createdDate":null,
"modifiedDate":null,
"creatorUserId":null,
"brandId":"545a07e1eb013d0eeb8ad282"
}
]
}
所以我想将这个响应绑定到一个常见的util类,然后绑定到实际的 Promotion 类。
我有PaginationUtil类,如下所示
import java.util.List;
import com.fasterxml.jackson.annotation.JsonProperty;
public class PaginationUtil {
private List<Object> Data;
private int count;
@JsonProperty("data")
public List<Object> getData() {
return Data;
}
public void setData(List<Object> data) {
Data = data;
}
@JsonProperty("count")
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
促销课
import java.util.Date;
public class Promotion {
private String promotionId;
private String promotionName;
private String description;
private Date startDate;
private Date endDate;
private Date createdDate;
private Date modifiedDate;
private String creatorUserId;
private String brandId;
public String getPromotionId() {
return promotionId;
}
public void setPromotionId(String promotionId) {
this.promotionId = promotionId;
}
public String getPromotionName() {
return promotionName;
}
public void setPromotionName(String promotionName) {
this.promotionName = promotionName;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public Date getEndDate() {
return endDate;
}
public void setEndDate(Date endDate) {
this.endDate = endDate;
}
public Date getCreatedDate() {
return createdDate;
}
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
public Date getModifiedDate() {
return modifiedDate;
}
public void setModifiedDate(Date modifiedDate) {
this.modifiedDate = modifiedDate;
}
public String getCreatorUserId() {
return creatorUserId;
}
public void setCreatorUserId(String creatorUserId) {
this.creatorUserId = creatorUserId;
}
public String getBrandId() {
return brandId;
}
public void setBrandId(String brandId) {
this.brandId = brandId;
}
}
我的通话类是
import java.io.IOException;
import java.util.List;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.test.rest.model.Promotion;
import com.test.rest.utility.PaginationUtil;
public class Test2 {
public static void main(String[] args) throws JsonParseException, IOException{
RestTemplate restTemplate = new RestTemplate();
String url = url here
HttpHeaders headers = new HttpHeaders();
HttpEntity<String> request1 = new HttpEntity<String>(headers);
ResponseEntity<String> response1 = restTemplate.exchange(url,HttpMethod.GET, request1, String.class);
String account = response1.getBody();
System.out.println(account);
ObjectMapper objectMapper = new ObjectMapper();
PaginationUtil paginationUtil = objectMapper.readValue(
account,PaginationUtil.class);
System.out.println(paginationUtil.getCount());
for(Promotion p : paginationUtil.getData()){
System.out.println(p.getPromotionId());
System.out.println(p.getPromotionName());;
}
}
}
我得到了以下json和错误
{"count":2,"data":[{"promotionId":"5460d40e3956e9b365e24864","promotionName":
"happy diwali","description":"happy diwali","startDate":null,"endDate":null,
"createdDate":null,"modifiedDate":null,"creatorUserId":null,
"brandId":"545a07e1eb013d0eeb8ad282"},
{"promotionId":"5460d45d3956e9b365e24865","promotionName":"happy new YEAR",
"description":"happy new YEAR","startDate":null,"endDate":null,
"createdDate":null,"modifiedDate":null,"creatorUserId":null,
"brandId":"545a07e1eb013d0eeb8ad282"}]}
Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException:
Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
at [Source: {"count":2,"data": [{"promotionId":"5460d40e3956e9b365e24864",
"promotionName":"happy diwali","description":"happy diwali","startDate":null,
"endDate":null,"createdDate":null,"modifiedDate":null,
"creatorUserId":null,"brandId":"545a07e1eb013d0eeb8ad282"},
{"promotionId":"5460d45d3956e9b365e24865","promotionName":"happy new YEAR",
"description":"happy new YEAR","startDate":null,"endDate":null,"createdDate":null,
"modifiedDate":null,"creatorUserId":null,"brandId":"545a07e1eb013d0eeb8ad282"}]};
line: 1, column: 1]
at com.fasterxml.jackson.databind.JsonMappingException.from
(JsonMappingException.java:148)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException
(DeserializationContext.java:749)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException
(DeserializationContext.java:745)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.handleNonArray
(CollectionDeserializer.java:275)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize
(CollectionDeserializer.java:216)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize
(CollectionDeserializer.java:206)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize
(CollectionDeserializer.java:25)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3051)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2153)
at Test2.main(Test2.java:31)
有人能说出问题是什么吗?