【问题标题】:JSON parse error: Cannot deserialize instance of java.lang.Long out of START ARRAY tokenJSON 解析错误:无法反序列化 java.lang.Long 的实例超出 START ARRAY 令牌
【发布时间】:2021-07-07 15:49:08
【问题描述】:

我一直在研究这个问题,但找不到适合我的情况的解决方案...

我尝试发送 JSON 数组,但收到此错误,我已调查此问题,但我 100% 无法理解问题,因此我不知道从哪里攻击它。

详细错误

{
  "type" : "https://www.jhipster.tech/problem/problem-with-message"

  "title" : "Bad Request",

  "status" : 400,

  "detail" : "JSON parse error: Cannot deserialize instance of `java.lang.Long` out of START_ARRAY token; 
nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.Long` out of START_ARRAY token\n at [Source: (PushbackInputStream); line: 1, column: 74] (through reference chain: com.curtipieles.app.service.dto.LeatherDTO[\"usageId\"])",
  "path" : "/api/leathers",
  "message" : "error.http.400"

}

PD:后端是在 JHIPSTER

中生成的

这是我的 DTO:

private Long usageId;
private Long colorId;
public Long getColorId() {
    return colorId;
}

public void setColorId(Long colorId) {
    this.colorId = colorId;
}

public String getColorName() {
    return colorName;
}

public void setColorName(String colorName) {
    this.colorName = colorName;
}


public Long getUsageId() {
    return usageId;
}

public void setUsageId(Long usageId) {
    this.usageId = usageId;
}

public String getUsageUsage() {
    return usageUsage;
}

public void setUsageUsage(String usageUsage) {
    this.usageUsage = usageUsage;
}

JSON array I am trying to send

【问题讨论】:

    标签: angular spring-boot jhipster


    【解决方案1】:

    您正在为usageId 字段传递一个数字数组,而它被定义为Long。您必须选择谁是正确的:JSON 有效负载(前端)或 Java 代码(后端)。 colorId 也一样。

    如果前端是正确的,那么在java后端更改你的字段声明:

    private List<Long> colorId;
    

    如果后端正确,请更改您的前端代码,以便它发送此 JSON 有效负载:

    "colorId": 1234,
    

    【讨论】:

    • 如果问题在前面,应该如何解决?因为我通过表格发送所有内容
    • 嗯,JHipster 在你生成的项目中有很多例子。你是从 Angular 开始的吗?
    • 编译失败[错误] /C:/Users/user/Desktop/Devs/CURTIPIELES/curti-back/src/main/java/com/curtipieles/app/service/mapper/LeatherMapper.java :[26,13] 无法将属性“java.util.List colorId”映射到“com.curtipieles.app.domain.Color color”。考虑声明/实现一个映射方法:“com.curtipieles.app.domain.Color map(java.util.List value)”。在后端进行更改时出现此错误...
    • 您应该编辑您的问题,而不是发布难以阅读的长 cmets。在我看来,您对 JHipster 背后的所有技术(Spring、JPA、Angular)没有很好的理解。此错误表明您仅在 DTO 中进行了更改,因此应用程序在保存到数据库时失败,因为 MapStruct 不知道如何将 LeatherDTO 映射到 Leather 实体,如果您修复了这个问题,那么保存到数据库时它仍然会失败因为您没有修改 Liquibase 迁移。为什么不更新您的 JDL 并重新生成实体?
    猜你喜欢
    • 2020-07-14
    • 2020-04-13
    • 1970-01-01
    • 2020-04-03
    • 2020-04-18
    • 2018-01-21
    • 2020-01-14
    • 2019-04-28
    • 2014-04-16
    相关资源
    最近更新 更多