当前位置: 首页 > 知识库问答 >
问题:

无法将com.advice.domain.family.income实例反序列化出START_ARRAY标记

徐景明
2023-03-14
"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实例,从

共有1个答案

公孙成仁
2023-03-14

你有点脱节。您的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>
 类似资料: