【问题标题】:Spring post rest cant parse objects in objectSpring post rest无法解析对象中的对象
【发布时间】:2019-12-25 13:52:52
【问题描述】:

我正在尝试对我的 Spring Boot 后端进行后期调用,以便我可以存储一些物品。我要存储的对象中有其他对象,这是数据库的当前设计。

现在我想做一个 post call 来在数据库中创建一个 partused 对象。我将此 Json 对象发送到 Spring Boot,但收到 amount_type_id 不能为空的错误。所以我认为它无法解析对象,任何人都可以看到我的类/json对象有什么问题,因为我找不到问题

{
    "amount":"2",
    "specification":"test",
    "partType":{
        "idPartType":4,
        "type":"leveringen"},
    "amountType":{
        "idAmountType":5,
        "type":"m3"},
    "project":{
        "idProject":4,
        "description":"test",
        "isFinished":false,
        "startDate":"2019-12-11",
        "finishDate":null,
        "name":"test2"
    }
}

我的零件使用类

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@JsonSerialize
private Long idPartsUsed;
private int amount;
private String specification;

@OneToOne
@JoinColumn(name = "part_type_id", referencedColumnName = "idPartType")
private PartType partType;

@OneToOne
@JoinColumn(name = "amount_type_id", referencedColumnName = "idAmountType")
private AmountType amountType;

@OneToOne
@JoinColumn(name = "project_id", referencedColumnName = "idProject")
private Project project;

我的金额类型类

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@JsonSerialize
private Long idAmountType;
private String type;

错误信息

{
    "cause": {
        "cause": {
            "cause": null,
            "message": "Column 'amount_type_id' cannot be null"
        },
        "message": "could not execute statement"
    },
    "message": "could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement"
}

【问题讨论】:

  • 请发布更多信息,例如您遇到的错误、API 代码等
  • @SurajGautam 我已经为你添加了错误信息,我刚刚得到一个基本的 400 代码
  • 您好,为什么要自动递增 AmountType 类中 idAmountType 的主键并以 json 格式发送?
  • @SurajGautam 我认为我需要为 post 调用指定 id 否则它会在数据库中创建一个我不想要的新 AmountType,我希望它链接到现有的 AmountType
  • 在您的请求中,“amount”:“2”,但是在partsused类中数量数据类型是int,您可以在请求中尝试使用“amount”:2吗?

标签: java json spring spring-boot


【解决方案1】:

在您的子实体中,您通过在 AmountTypePartTypeProject 类中执行 @GeneratedValue(strategy = GenerationType.IDENTITY) 来生成 id。但是,您还要从 JSON 本身发送 id 的值。这就是问题所在。

因此,首先,从子实体中删除 @GeneratedValue(strategy = GenerationType.IDENTITY) 代码。

添加第二个,将CascadeType.ALL 添加到您的映射中。

我在我的机器上试了一下,效果很好。

我尝试过的代码如下。

AmountType.java

@Entity
@Getter
@Setter
@Table(name="amount_type")
public class AmountType {
@Id
@JsonSerialize
private Long idAmountType;
private String type;
}

PartType.java

@Data
@Entity
@Table(name="part_type")
public class PartType {
@Id
@JsonSerialize
private Long idPartType;
private String type;

}

Project.java

@Data
@Entity
@Table(name="project")
public class Project {

@Id
private Long idProject;
private String description;
private boolean isFinished;
private String startDate;
private String finishDate;
private String name;
}

PartsUsed.java

@Entity
@Table(name = "parts_used")
@Getter
@Setter
@NoArgsConstructor
public class PartsUsed {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@JsonSerialize
private Long idPartsUsed;
private int amount;
private String specification;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "part_type_id", referencedColumnName = "idPartType")
private PartType partType;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "amount_type_id", referencedColumnName = "idAmountType")
private AmountType amountType;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "project_id", referencedColumnName = "idProject")
private Project project;


}

这是接收 POST 请求以创建部件的控制器。

@PostMapping(value = "/test")
    public String test(@RequestBody PartsUsed partsUsed) {
        jpaRepositoryTest.save(partsUsed);
        return "test";
    }

这就是我使用您的有效负载发送请求的方式。

这就是将数据插入到表中的方式。我用的是 h2 数据库。

希望对你有帮助:)

【讨论】:

    【解决方案2】:

    找到问题了!

    我认为当 spring 使用 @RepositoryRestResource 创建自己的端点时,它的功能与使用 @RestController + @PostMapping 创建自己的端点时不同。当我使用我自己的休息控制器时,它得到了数量类型和其他对象的 id。

    这是我的存储库,我不知道我设置它的方式是否有任何问题,但现在它可以工作了

    @Repository("PartsUsedRepository")
    @RepositoryRestResource(collectionResourceRel = "results", path = "partsused")
    public interface PartsUsedRepository extends PagingAndSortingRepository<PartsUsed, Long> {
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-21
      • 1970-01-01
      • 1970-01-01
      • 2023-02-05
      • 2015-01-25
      相关资源
      最近更新 更多