"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
}
}
]
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;
}
public class Salary {
private int ctc;
private int basic;
private int pf;
private int gratuity;
private int paci;
private int sa;
private int mediclaim;
}
@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实例,从[source:java.io.pushbackInputStream@3d3298f9;行:1,列:2]处的START_ARRAY令牌中取出(通过引用链:java.util.ArrayList[0]);嵌套异常是com.fasterxml.jackson.databind.jsonMappingException:无法反序列化com.advice.domain.family.income实例,从
你有点脱节。您的JSON实际上不是一个收入对象,因此它对您大喊大叫是正确的。收入对象只是JSON字符串的这一部分:
{
"_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
}
}
如果您只发布以下内容,您应该避免这个问题,因为它实际上是一个收入对象数组:
[
{
"_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
}
}
]
您可以在单元测试中使用Jackson ObjectMapper进行测试:
Income[] incomes = new ObjectMapper().readValue(jsonAsString,Income[].class);
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
下面提到的是JSON字符串ResultString: 我想将相同的内容转换为A类的对象: B类代码为: 但是获取异常: JSONMappingException:无法从START_ARRAY标记反序列化java.util.LinkedHashMap实例
给定以下JSON文件: 我试图使用Jackson对象映射器访问JSON文件中的值。具体如下:
我需要向网络服务发送帖子请求,但它在异常后返回。 实际服务器响应如下: 我根据以下内容发送请求 我的代码