【问题标题】:Spring don't parse @RequestBody?Spring不解析@RequestBody?
【发布时间】:2017-12-17 01:10:14
【问题描述】:

我收到了这个错误。我尝试将此代码添加到属性中:“spring.jackson.deserialization.accept-single-value-as-array=true”但我无法解决这个问题?

org.springframework.http.converter.HttpMessageNotReadableException:JSON 解析错误:无法从 START_OBJECT 令牌中反序列化 java.util.ArrayList 的实例;嵌套异常是 com.fasterxml.jackson.databind.JsonMappingException:无法从 START_OBJECT 令牌中反序列化 java.util.ArrayList 的实例 在 [来源:java.io.PushbackInputStream@2e64d73;行:1,列:145](通过引用链:com.test.mobil.viewmodel.CompanyViewModel ["customerList"])

客户视图模型

public class CustomerViewModel {

private String name;
private String surname;
private int birthDate;

public CustomerViewModel(){}

public CustomerViewModel(String name, String surname, int birthDate) {
    this.name = name;
    this.surname = surname;
    this.birthDate = birthDate;
}
}

CompanyViewModel

public class CompanyViewModel {

private String company;
private List<CustomerViewModel> customerList;

public CompanyViewModel(){}

public CompanyViewModel(String company, List<CustomerViewModel> customerList) {
    this.company = company;
    this.customerList = customerList;
}
}

客户控制器

@Controller
public class CustomerController {

@PostMapping("/customer")
public void setCustomer(@RequestBody CompanyViewModel companyViewModel){
    System.out.println(companyViewModel);
}
}

JSON

page = {
    company: "Facebook",
    customerList = [
        {
            name: "Test1",
            surname: "Test2",
            birthDate: 1987
        },
        {
            name: "Test3",
            surname: "Test4",
            birthDate: 1988
        }

    ]
}

【问题讨论】:

  • 你的 JSON 好像错了!!将customerList = 替换为customerList :
  • 除非您已正确配置使用的 ObjectMapper,否则您还必须将 getter/setter 添加到您希望使其可序列化/可反序列化的对象的私有字段中。
  • 您应该将您的 JSON 示例更改为 customerList 实际上只是一个 JSON 对象(而不是 JSON 数组)以匹配您的异常。
  • 您实际发送的是什么 JSON。您应该发送:{ company: "Facebook", customerList = [ { name: "Test1", surname: "Test2", birthDate: 1987 }, { name: "Test3", surname: "Test4", birthDate: 1988 } ] } 也不要使用 page=,请使用 customerList: 而不是 customerList=

标签: java arrays json spring spring-boot


【解决方案1】:

您误解了问题:无法反序列化该数组,因为它不是有效的 JSON!你有一个=customerList = [,正如这里所说:

JsonMappingException: Can not deserialize instance of java.util.ArrayList
                          out of START_OBJECT token
    at [Source: java.io.PushbackInputStream@2e64d73; line: 1, column: 145]
        (through reference chain: com.test.mobil.viewmodel.CompanyViewModel ["customerList"])

【讨论】:

  • @Abdullah 是否有异常必须格式化为引用文本的规则?我更喜欢它们在 code 块中具有语言颜色。
  • @Downvoter 为什么有人对给出的 3 个答案投了反对票?一个解释会很棒。
  • 不,没有规则。只是异常可读性更强,代码格式不乱异常。
  • @AbdullahKhan 好吧,至少对我来说,代码格式的异常实际上更容易阅读^^我想这是一个品味问题。
  • 这只是json的浏览器输出。如果这真的是问题所在,那么错误将完全不同。
【解决方案2】:

非常感谢你们。我在前端检查了 JSON 值,我看到了这样的结果。我的 JSON 不包括 Array,因为 Angular 误导了我。 我将列表更改为地图,问题已解决!

{  
"customer":{  
  "name":"name1",
  "surname":"sur1",
  "company":"firm",
  "address":"adres",
  "phone":"tel",
  "fax":"faxx",
  "type":"real",
  "taxNo":"2398"
},
"products":{  
  "0":{  
     "company":"es",
     "product":"a",
     "detail":"a",
     "value":2
  },
  "1":{  
     "company":"we",
     "product":"qwe",
     "detail":"qwe",
     "value":1
  }
},
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-02
    • 1970-01-01
    • 1970-01-01
    • 2018-12-14
    • 1970-01-01
    • 2012-07-02
    • 1970-01-01
    相关资源
    最近更新 更多