gpt4 book ai didi

json - 保存相关实体spring数据rest HAL-JSON

转载 作者:行者123 更新时间:2023-12-03 06:50:19 25 4
gpt4 key购买 nike

我有以下问题

我有 Spring 数据休息的基本配置(没什么特别的,没什么自定义的)。

使用 spring-data-rest-webmvc 2.0.0 RELEASE 和 spring-data-jpa 1.5.0 RELEASE

A级

@Entity
public class A {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id

private String name;

@ManyToMany
private List<B> b;

// getters setters
}

B级
@Entity
public class B {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id

private String nameb;

@ManyToMany(mappedBy = "b")
private List<A> a;

// getters setters
}

存储库 A
@Repository
@RestResource(rel = "a", path = "a")
public interface ARepository extends PagingAndSortingRepository<A, Integer> {

}

存储库 B
@Repository
@RestResource(rel = "b", path = "b")
public interface BRepository extends PagingAndSortingRepository<B, Integer> {

}

当我保存实体时工作正常,但我不知道如何保存关系

例如使用 http 在“B”中保存“A”

这是我从这个答案中尝试的最后一件事 https://stackoverflow.com/a/13031580/651948
POST http://localhost:8080/api/a

{
"name": "Name of A",
"b": {
"rel": "b",
"href": "http://localhost:8080/api/b/1"
}
}

我得到一个 201 http 代码,但没有保存实体。

有人已经试过了吗?

最佳答案

尝试仅使用 URL。

POST http://localhost:8080/api/a
Content-Type: application/json

{
"name" : "Name of A",
"b": "http://localhost:8080/api/b/1"
}

或者,就您而言,可能是
"b" :  ["http://localhost:8080/api/b/1"]

因为 A.b 是一个列表,因此您提交了一个数组。不过没有测试这个。

这应该是 Spring 2.0 以来的有效方式(参见 Spring Data Rest 2.0.0.RELEASE Breaks Code Working Previously With RC1 )并且它对我很有效。

关于json - 保存相关实体spring数据rest HAL-JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22676452/

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