【发布时间】:2016-06-17 11:01:30
【问题描述】:
我有两个班级,一个是Income,另一个是Salary。我有一个看起来像这样的 json。
"income" : [
{
"_id" : 271234.0,
"type" : "salary",
"amount" : 100000.0,
"inception" : "11/8/1986",
"endsOn" : "11/8/2030",
"salary" : {
"ctc" : 200000.0,
"basic" : 32000.0,
"pf" : 14000.0,
"gratuity" : 55000.0,
"paci" : 5000.0,
"sa" : 50000.0,
"mediclaim" : 50000.0
}
}
]
班级Income:
public class Income {
public static final String INCOME = "income";
private Long id;
@Size(max = 30)
private String type;
@Size(max = 10)
private int amount;
// @Size(max = 50)
private String inception =null;
// @Size(max = 10)
private String endsOn =null;
private Salary salary;
}
班级Salary:
public class Salary {
private int ctc;
private int basic;
private int pf;
private int gratuity;
private int paci;
private int sa;
private int mediclaim;
}
我隐藏了 Stackoverflow 的所有构造函数、setter 和 getter,但它们在我的代码中。我想使用此代码从 URL 中使用 ObjectMapper 将 Json 解析为 Object:
@RequestMapping(value = "/user/{cUsrId}/lc/{lcId}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public ResponseEntity<?> addNewIncome( @PathVariable Long cUsrId, @PathVariable Long lcId,@RequestBody List<Income> income) {
log.debug("REST request to save Profile by id: {}, lcId: {},lcId: {}, income: {}", cUsrId,lcId,income);
int success = engagementService.saveNewIncome(cUsrId, lcId, income);
if(success ==0){
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}else{
return new ResponseEntity<>(HttpStatus.OK);
}
}
但我收到此错误:
“无法读取文档:无法反序列化 com.advice.domain.family.Income out of START_ARRAY token↵ at [来源: java.io.PushbackInputStream@3d3298f9;行:1,列:2](通过 参考链:java.util.ArrayList[0]);嵌套异常是 com.fasterxml.jackson.databind.JsonMappingException:不能 反序列化 com.advice.domain.family.Income 的实例 [来源:java.io.PushbackInputStream@3d3298f9;START_ARRAY 令牌↵; 行:1,列:2](通过引用链:java.util.ArrayList[0])"
【问题讨论】:
标签: angularjs mongodb spring-boot