“无法读取JSON文档:无法从START_OBJECT标记中反序列化java.lang.String的实例↵

时间:2017-05-20 19:42:26

标签: javascript java angularjs arrays java-ee

尝试将值发布到控制器时出现以下错误。

Failed to read HTTP message: 
org.springframework.http.converter.HttpMessageNotReadableException: Could 
not read JSON document: Can not deserialize instance of java.lang.String out 
of START_OBJECT token
at [Source: java.io.PushbackInputStream@355134ca; line: 1, column: 655] 
(through reference chain: com.csps.gabriel.entities.Policy["goodsList"]-
>java.util.ArrayList[0]->com.csps.gabriel.dtos.GoodsDto["type"]); nested 
exception is com.fasterxml.jackson.databind.JsonMappingException: Can not 
deserialize instance of java.lang.String out of START_OBJECT token
at [Source: java.io.PushbackInputStream@355134ca; line: 1, column: 655] 
(through reference chain: com.csps.gabriel.entities.Policy["goodsList"]-
>java.util.ArrayList[0]->com.csps.gabriel.dtos.GoodsDto["type"])

这是json发布的。

{
"client": {
    "clientId": 1000002,
    "firstName": "Jose Anibal",
    "lastName": "Rodriguez Lopez",
    "idCard": "07200140809",
    "enterprise": "",
    "contactBus": "",
    "address": "Jardín De Pekes, Calle Lorenzo",
    "sector": "Los Prados",
    "city": "2",
    "phoneNumber1": "8099803135",
    "phoneNumber2": "8099803135",
    "phoneNumber3": "8099803135",
    "email": "joseanibalrl76@gmail.com",
    "clientContact": "",
    "clientType": "Persona Fisica",
    "notes": "",
    "status": "Activo",
    "phoneNumber": "8099803135",
    "fullName": "Jose Anibal Rodriguez Lopez",
    "rnc": ""
},
"policyNumber": "847474141",
"branch": "2",
"policer": "2",
"prime": 48484,
"startDate": "2017-05-09",
"endDate": "2017-05-10",
"status": "1",
"itbis": "2",
"goodsList": [{
    "type": {
        "name": "Jeep",
        "index": "1"
    },
    "year": 2000,
    "brand": "toyota",
    "model": "corolla",
    "chassisSerial": "441147",
    "covertAmount": 10000,
    "color": "rojo",
    "cylinder": 6,
    "passengerAmount": "6",
    "weight": 6
}]
}

这是映射请求正文的实体。

@Entity
@Table(name = "policy")
public class Policy {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "policy_id")
private Long policyId;

@Column(name = "client")
@Type(type = "serializable")
private Client client;

@Column(name = "policy_number")
private String policyNumber;

@Column(name = "branch_name")
private String branch;

@Column(name = "policer")
private String policer;

@Column(name = "prime")
private String prime;

@Column(name = "currency")
private String currency;

@Column(name = "start_date")
private Date startDate;

@Column(name = "end_date")
private Date endDate;

@Column(name = "status")
private String status;

@Column(name = "itbis")
private String itbis;

@Column(name = "good_list")
@Type(type = "serializable")
@JsonProperty
private List<GoodsDto> goodsList;


public Long getPolicyId() {
    return policyId;
}

public void setPolicyId(Long policyId) {
    this.policyId = policyId;
}

public Client getClient() {
    return client;
}

public void setClient(Client client) {
    this.client = client;
}

public String getPolicyNumber() {
    return policyNumber;
}

public void setPolicyNumber(String policyNumber) {
    this.policyNumber = policyNumber;
}

public String getBranch() {
    return branch;
}

public void setBranch(String branch) {
    this.branch = branch;
}

public String getPolicer() {
    return policer;
}

public void setPolicer(String policer) {
    this.policer = policer;
}

public String getPrime() {
    return prime;
}

public void setPrime(String prime) {
    this.prime = prime;
}

public String getCoin() {
    return currency;
}

public void setCoin(String currency) {
    this.currency = currency;
}

public Date getStartDate() {
    return startDate;
}

public void setStartDate(Date startDate) {
    this.startDate = startDate;
}

public Date getEndDate() {
    return endDate;
}

public void setEndDate(Date endDate) {
    this.endDate = endDate;
}

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

public String getItbis() {
    return itbis;
}

public void setItbis(String itbis) {
    this.itbis = itbis;
}

public List<GoodsDto> getGoodsList() {
    return goodsList;
}

public void setGoodsList(List<GoodsDto> goodsList) {
    this.goodsList = goodsList;
}
}

这是我的控制者:

@RestController
public class PolicyController {

@Autowired
PolicyRepository policyRepository;

@RequestMapping(value = "/save-policy", method = RequestMethod.POST, 
consumes = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody
Response<Policy> saveClient(@RequestBody Policy policy) {
policy = this.policyRepository.save(policy);
    return new Response<Policy>(policy, true, "Successful");
}
}

这是GoodsList类:

package com.csps.gabriel.dtos;

import java.io.Serializable;

/**
Created by Jose A Rodriguez on 5/20/2017.
*/
public class GoodsDto implements Serializable{

private static final long serialVersionUID = 1L;

private String type;
private Long year;
private String brand;
private String model;
private String chassisSerial;
private Double covertAmount;
private String color;
private Long cylinder;
private Long passengerAmount;
private Long weight;

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public Long getYear() {
    return year;
}

public void setYear(Long year) {
    this.year = year;
}

public String getBrand() {
    return brand;
}

public void setBrand(String brand) {
    this.brand = brand;
}

public String getModel() {
    return model;
}

public void setModel(String model) {
    this.model = model;
}

public String getChassisSerial() {
    return chassisSerial;
}

public void setChassisSerial(String chassisSerial) {
    this.chassisSerial = chassisSerial;
}

public Double getCovertAmount() {
    return covertAmount;
}

public void setCovertAmount(Double covertAmount) {
    this.covertAmount = covertAmount;
}

public String getColor() {
    return color;
}

public void setColor(String color) {
    this.color = color;
}

public Long getCylinder() {
    return cylinder;
}

public void setCylinder(Long cylinder) {
    this.cylinder = cylinder;
}

public Long getPassengerAmount() {
    return passengerAmount;
}

public void setPassengerAmount(Long passengerAmount) {
    this.passengerAmount = passengerAmount;
}

public Long getWeight() {
    return weight;
}

public void setWeight(Long weight) {
    this.weight = weight;
}
}

有谁知道为什么它会给我这个错误。请。

1 个答案:

答案 0 :(得分:1)

您的问题出在会员GoodsDto.type中。它在你的String类中被声明为GoodsDto,但是你试图在它的Json中传递一个2字段结构:

"goodsList": [{
    "type": {
        "name": "Jeep",
        "index": "1"
}