gpt4 book ai didi

java - 创建一个Post方法来保存具有多个id的对象

转载 作者:行者123 更新时间:2023-11-30 02:16:07 25 4
gpt4 key购买 nike

我的 Controller 中有:

@RestController
public class OneTwoController {
private OnTwoService _service;
//... more code
@PostMapping("/api/one-two")
@CrossOrigin
public ResponseEntity<ServiceResponse> save(@RequestBody OneTwo model) {
return ResponseEntity.ok().body( _service.Save(model));
}

在我的实体中:

@Entity(name = "OneTwo")
@Where (clause = "deleted='false'")
public class OneTwo{
@EmbeddedId
private OneTwoKey_id;

public OneTwo(OneTwoKey id) {
this._id = id;
}

@JsonProperty("oneTwo")
public void setId(OneTwoKey value) {
this._id = value;
}

OneTwoKey 类:

public class OneTwoKey implements Serializable {
@Column(name = "OneID")
private int _oneID;

@Column(name = "TwoID")
private int _twoID;

public OneTwoKey(int oneID, int twoID) {
this._oneID = oneID;
this._twoID = twoID;
}

}

我发送到 Rest API 的 json:

{
"oneTwo": {
"oneID": 83,
"twoID": 69
},
"deleted": true
}

问题是两个 ID 都为空,因此服务无法在数据库上执行插入操作。

当 id 超过一个时,我该如何处理这些情况?

最佳答案

尝试在 OneTwoKey 类中添加 setter ,以使 JSON 反序列化器更容易:

@JsonProperty("oneID")
public void setOneID(int oneID) {
this._oneID = oneID;
}

@JsonProperty("twoID")
public void setTwoID(int twoID) {
this._twoID = twoID;
}

另一个解决方案是创建一个 DTO,用它来接收 Controller 中的数据,然后将其转换为您的实体:

public class OneTwoDTO {
private Map<String, Int> oneTwo;
private boolean deleted;
// setters & getters
}

关于java - 创建一个Post方法来保存具有多个id的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48363251/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com