Jakson 无法从 START_OBJECT 令牌反序列化 java.util.ArrayList 的实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45887741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:58:08  来源:igfitidea点击:

Jakson can not deserialize instance of java.util.ArrayList out of START_OBJECT token

javaspringhibernatespring-mvcHymanson

提问by Yiour

After having sent a POST I receive a 400 error curl localhost:8888/bills/addbill -H "Content-Type: application/json" -X POST -d '{"number":"111A111", "customer":"Customer Cuustomer Rrrr", "phone":"1 800 5551212", "manager":"Manager Manager Manager", "date":"2012-09-17", "curId":{"id":"1"}, "payments":{["id":"1"]}}'

发送 POST 后,我收到 400 错误 curl localhost:8888/bills/addbill -H "Content-Type: application/json" -X POST -d '{"number":"111A111", "customer":"Customer Cuustomer Rrrr", "phone":"1 800 5551212", "manager":"Manager Manager Manager", "date":"2012-09-17", "curId":{"id":"1"}, "payments":{["id":"1"]}}'

The response is : {"timestamp":1503684882518,"status":400,"error":"Bad Request","exception":"org.springframework.http.converter.HttpMessageNotReadableException","message":"Bad Request","path":"/bills/addbill"}

回应是: {"timestamp":1503684882518,"status":400,"error":"Bad Request","exception":"org.springframework.http.converter.HttpMessageNotReadableException","message":"Bad Request","path":"/bills/addbill"}

I am getting an exception when I try to deserialize the my json data. How can I deserialize JSON properly?

当我尝试反序列化我的 json 数据时出现异常。如何正确反序列化 JSON?

Bill enntity file is:

账单实体文件是:

import com.fasterxml.Hymanson.annotation.JsonIgnoreProperties;
import ru.test.practice.view.PaymentView;
import javax.persistence.*;
import javax.validation.constraints.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@Entity
@Table(name = "Bill")
public class Bill {

    @GeneratedValue
    @Id
    @Column(name = "id")
    private Integer id;

    @Version
    private int version;

    @Column(name = "number")
    @NotNull(message = "Num should be set")
    @Size(min = 6, max = 10)
    @Pattern(regexp = "^[^\W_]+$")
    private String number;

    @Column(name = "customer")
    @NotNull
    @Size(max = 256)
    @Pattern(regexp = "^[a-zA-Z\s]*$")
    private String customer;

    @Column(name = "phone")
    @NotNull
    @Size(max = 20)
    @Pattern(regexp = "(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]\u200C\u200B)\s*)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)([2-9]1[02-9]\u200C\u200B|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})\s*(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+)\s*)?$")
    private String phone;

    @Column(name = "manager")
    @Pattern(regexp = "^[a-zA-Z\s]*$")
    @Size(max = 256)
    @NotNull
    private String manager;

    @Column(name = "date")
    @NotNull
    private Date date;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "cur_id")
    private Currency curId;

    @ManyToMany(targetEntity = ru.test.practice.model.Payment.class)
    public List<PaymentView> payments = new ArrayList<>();

    public List<PaymentView> getPayments() {
        return payments;
    }

    public void setPayments(List<PaymentView> payments) {
        this.payments = payments;
    }

    public Bill(Integer id, String number, String customer, String phone, String manager, Date date, Currency curId, List<PaymentView> payments) {
        this.id = id;
        this.number = number;
        this.customer = customer;
        this.phone = phone;
        this.manager = manager;
        this.date = date;
        this.curId = curId;
        this.payments = payments;
    }

    public Bill() {
        this.id = null;
        this.number = null;
        this.customer = null;
        this.phone = null;
        this.manager = null;
        this.date = null;
        this.curId = null;
        this.payments = null;
    }


    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public Integer getId() {
        return id;
    }

    public int getVersion() {
        return version;
    }

    public void setVersion(int version) {
        this.version = version;
    }

    public String getCustomer() {
        return customer;
    }

    public void setCustomer(String customer) {
        this.customer = customer;
    }

    public String getManager() {
        return manager;
    }

    public void setManager(String manager) {
        this.manager = manager;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public Currency getCurId() {
        return curId;
    }

    public void setCurId(Currency curId) {
        this.curId = curId;
    }
}

BillView file is

BillView 文件是

package ru.test.practice.view;
import io.swagger.annotations.ApiModelProperty;
import ru.bellintegrator.practice.model.Currency;
import java.util.Date;
import java.util.List;

public class BillView {

    @ApiModelProperty(hidden = true)
    public Integer id;

    public String customer;

    public String phone;

    public String manager;

    public String number;

    public Date date;

    public Currency curId;

    public List<PaymentView> payments;

    //для Hymanson
    public BillView() {
    }

    public BillView(Integer id, String number, String customer, String phone, String manager, Date date, Currency curId, List<PaymentView> payments) {
        this.id = id;
        this.number = number;
        this.customer = customer;
        this.phone = phone;
        this.manager = manager;
        this.date = date;
        this.curId = curId;
        this.payments = payments;
    }

    @Override
    public String toString() {
        return "{id:" + id +
                "billNumber:" + number +
                ";customer:" + customer +
                ";phone:" + phone +
                ";manager:" + manager +
                ";date:" + date +
                ";currencyId:" + curId +
                ";payments:" + payments +
                "}";
    }
}

Controller in BillController class that handles a POST request

BillController 类中处理 POST 请求的控制器

 @Override
    @ApiOperation(value = "addBill", nickname = "addBill", httpMethod = "POST")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "Success", response = String.class),
            @ApiResponse(code = 404, message = "Not Found"),
            @ApiResponse(code = 500, message = "Failure")})
    @RequestMapping(value = "/addbill", method = {POST})
    public void bill(@RequestBody BillView bill) {
        billService.add(bill);
    }

    @Override
    @ApiOperation(value = "getBills", nickname = "getBills", httpMethod = "GET")
    @RequestMapping(value = "/list", method = {GET})
    public List<BillView> bills() {
        return billService.bills();
    }

Finally my Payments Entity class:

最后我的支付实体类:

import com.fasterxml.Hymanson.annotation.JsonIgnoreProperties;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@Entity
@Table(name = "Payments")
public class Payment {

    @Id
    @GeneratedValue
    @Column(name = "id")
    private Integer id;

    @Version
    private Integer version;

    @Column(name = "name", nullable = false, length = 256)
    private String name;

    @Column(name = "price", nullable = false)
    private float price;

    @ManyToMany(targetEntity = ru.bellintegrator.practice.model.Bill.class, mappedBy = "curId")
    private List<Bill> bills = new ArrayList<>();

    public List<Bill> getBills() {
        return bills;
    }

    public void setBills(List<Bill> bills) {
        this.bills = bills;
    }

    public Integer getId() {
        return id;
    }

    public int getVersion() {
        return version;
    }

    public void setVersion(int version) {
        this.version = version;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }
}

采纳答案by Amer Qarabsa

you have problem in your json

你的json有问题

"payments":{["id":"1"]}

since you are trying to deserialize list of type PaymentView this should be, i suppose PaymentView contains id attribute

由于您正在尝试反序列化 PaymentView 类型列表,因此我认为 PaymentView 包含 id 属性

 "payments":[{"id":"1"}]