无法反序列化com.advice.domain.family的实例。从START_ARRAY令牌中取出

时间:2016-06-17 11:01:31

标签: angularjs mongodb spring-boot

我有两个班,一个是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。收到START_ARRAY令牌↵在[来源:   java.io.PushbackInputStream@3d3298f9; line:1,column:2](通过   reference chain:java.util.ArrayList [0]);嵌套异常是   com.fasterxml.jackson.databind.JsonMappingException:不能   反序列化com.advice.domain.family.Income的实例   START_ARRAY令牌↵在[来源:java.io.PushbackInputStream@3d3298f9;   line:1,column:2](通过引用链:java.util.ArrayList [0])“

1 个答案:

答案 0 :(得分:0)

你有轻微的脱节。您呈现的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>