Java 春堂';t解析@RequestBody?
我犯了这个错误。我尝试在代码中添加属性:“spring.jackson.deserialization.accept single value as array=true”,但我无法解决这个问题 org.springframework.http.converter.httpmessagenoteradableexception:JSON解析错误:无法反序列化START_对象令牌之外的java.util.ArrayList实例;嵌套异常为com.fasterxml.jackson.databind.JsonMappingException:无法从START\u对象标记中反序列化java.util.ArrayList的实例 在[来源:java.io。PushbackInputStream@2e64d73;行:1,列:145](通过引用链:com.test.mobil.viewmodel.CompanyViewModel[“customerList”]) 客户服务模型Java 春堂';t解析@RequestBody?,java,arrays,json,spring,spring-boot,Java,Arrays,Json,Spring,Spring Boot,我犯了这个错误。我尝试在代码中添加属性:“spring.jackson.deserialization.accept single value as array=true”,但我无法解决这个问题 org.springframework.http.converter.httpmessagenoteradableexception:JSON解析错误:无法反序列化START_对象令牌之外的java.util.ArrayList实例;嵌套异常为com.fasterxml.jackson.databind
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;
}
}
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;
}
}
公司视图模型
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;
}
}
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;
}
}
JSON
page = {
company: "Facebook",
customerList = [
{
name: "Test1",
surname: "Test2",
birthDate: 1987
},
{
name: "Test3",
surname: "Test4",
birthDate: 1988
}
]
}
您误解了这个问题:数组不能反序列化,因为它不是有效的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"])
非常感谢你们。我检查了前面的JSON值,我看到了这样的情况。我的JSON不包括数组,因为Angular误导了我。我将列表更改为Map,问题解决了
{
"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
}
},
}
您的JSON似乎是错误的!!将
customerList=
替换为customerList:
,除非您正确配置了使用的ObjectMapper
,否则您还必须将getter/setter添加到对象的私有字段中,以使其可序列化/反序列化。您应该将JSON示例更改为{company:“Facebook”,customerList=[{name:“Test1”,姓氏:“Test2”,生日:1987},{name:“Test3”,姓:“Test4”,出生日期:1988}]}
没有页面=
也可以使用客户列表:
而不是客户列表=
@Abdullah是否有规则规定例外必须格式化为引用文本?我更喜欢在代码
块中使用语言颜色。@Downvoter为什么有人刚刚对3个给定的答案投了否决票?解释如下:eat。不,没有规则。只是异常更具可读性,代码格式不会弄乱异常。@AbdullahKhan好吧,至少对我来说,以代码格式读取异常实际上更容易^^^我想这是一个品味问题。这只是json的浏览器输出。如果这真的是问题,错误将那就完全不同了。