Spring HATEOAS。嵌入式资源+增删改查能力吗?
Spring HATEOAS. Embedded resource + ability to CRUD it?
在 embedded resources 的典型情况下:
@Entity @Data
class Item {
id, name
...
@ManyToOne
@JoinColumn(name="status", referencedColumnName="ID")
private Status status;
}
@Entity @Data
class Status {
id, name
...
@JsonIgnore //break infinite reference loop during serialization
@OneToMany(mappedBy="status")
private List<Item> items;
}
我想在 Item JSON
中包含指向项目 JSON 中状态 ID 的链接,而不是
{
"itemName": "abc",
... ,
"status": {
"statusName":"ACTIVE",
...
}
"_links": {
...
}
}
我通过以下 任何 实现了嵌入:
- 标记项目 class 状态 属性 为@RestResource(exported=false)
@Entity @Data
class Item {
...
@RestResource(exported=false) // <-- HERE
@ManyToOne
@JoinColumn(name="status", referencedColumnName="ID")
private Status status;
- 标记状态回购接口@RepositoryRestResource(..., exported=false)
@RepositoryRestResource(collectionResourceRel="statuses", path="status", exported=false)
public interface StatusRepository extends JpaRepository<Status, String>
- 正在删除 Status 实体的存储库
// DELETED
@RepositoryRestResource
public interface StatusRepository extends JpaRepository<Status, String>{}
问题:
其中任何一个都像我想要的那样将 Status 嵌入到项目 JSON 中,但我无法再访问 Status Repository 以通过其 ID 获取 Status 对象或对其执行任何 CRUD。
如何在父项 JSON 中嵌入状态并通过 url 仍然是 CRUD 状态?
我是这样实现的:
@Entity @Data
class Item {
@Id
@GeneratedValue(generator = "uuid2")
@GenericGenerator(name = "uuid2", strategy = "uuid2")
String id;
String name
@RestResource(exported = false) // <---- ADDED
@ManyToOne
@JoinColumn(name="status", referencedColumnName="ID")
private Status status;
}
@Entity @Data
class Status {
@Id
@GeneratedValue(generator = "uuid2")
@GenericGenerator(name = "uuid2", strategy = "uuid2")
String id
String name
// <-------- DELETED REFERENCE TO PARENT OBJECT
//@OneToMany(mappedBy="status")
//private List<Item> items;
}
两个实体的存储库都存在,因此也可以对 Status 实体进行 CRUD
@RepositoryRestResource(collectionResourceRel="items", path="items")
public interface StatusRepository extends JpaRepository<Item, String>
{}
@RepositoryRestResource(collectionResourceRel="statuses", path="statuses")
public interface StatusRepository extends JpaRepository<Status, String>
{}
留给子孙后代。
为方便起见:
1) JSON
中实体的公开 ID
/**
* Exposing Ids as properties for entities specified
*/
@Configuration
public class ExposeIdConfig extends RepositoryRestMvcConfiguration {
@Override
protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.exposeIdsFor(Item.class, Status.class);
}
}
因此获取项目列表如下所示:
{
"_links": {
"self": {
"href": "http://localhost:8080/app/items{?page,size,sort}",
"templated": true
}
},
"_embedded": {
"as": [
{
"id": "29117425-f011-4ff9-8952-38b05d3df7f0",
"name": "item 1",
"status": {
"id": "e9192ae7-29f8-4d5e-ad62-cad8d87de9e2",
"name": "ACTIVE"
},
"_links": {
"self": {
"href": "http://localhost:8080/app/items/29117425-f011-4ff9-8952-38b05d3df7f0"
}
},
...
}
在 embedded resources 的典型情况下:
@Entity @Data
class Item {
id, name
...
@ManyToOne
@JoinColumn(name="status", referencedColumnName="ID")
private Status status;
}
@Entity @Data
class Status {
id, name
...
@JsonIgnore //break infinite reference loop during serialization
@OneToMany(mappedBy="status")
private List<Item> items;
}
我想在 Item JSON
中包含指向项目 JSON 中状态 ID 的链接,而不是 {
"itemName": "abc",
... ,
"status": {
"statusName":"ACTIVE",
...
}
"_links": {
...
}
}
我通过以下 任何 实现了嵌入:
- 标记项目 class 状态 属性 为@RestResource(exported=false)
@Entity @Data
class Item {
...
@RestResource(exported=false) // <-- HERE
@ManyToOne
@JoinColumn(name="status", referencedColumnName="ID")
private Status status;
- 标记状态回购接口@RepositoryRestResource(..., exported=false)
@RepositoryRestResource(collectionResourceRel="statuses", path="status", exported=false)
public interface StatusRepository extends JpaRepository<Status, String>
- 正在删除 Status 实体的存储库
// DELETED
@RepositoryRestResource
public interface StatusRepository extends JpaRepository<Status, String>{}
问题:
其中任何一个都像我想要的那样将 Status 嵌入到项目 JSON 中,但我无法再访问 Status Repository 以通过其 ID 获取 Status 对象或对其执行任何 CRUD。
如何在父项 JSON 中嵌入状态并通过 url 仍然是 CRUD 状态?
我是这样实现的:
@Entity @Data
class Item {
@Id
@GeneratedValue(generator = "uuid2")
@GenericGenerator(name = "uuid2", strategy = "uuid2")
String id;
String name
@RestResource(exported = false) // <---- ADDED
@ManyToOne
@JoinColumn(name="status", referencedColumnName="ID")
private Status status;
}
@Entity @Data
class Status {
@Id
@GeneratedValue(generator = "uuid2")
@GenericGenerator(name = "uuid2", strategy = "uuid2")
String id
String name
// <-------- DELETED REFERENCE TO PARENT OBJECT
//@OneToMany(mappedBy="status")
//private List<Item> items;
}
两个实体的存储库都存在,因此也可以对 Status 实体进行 CRUD
@RepositoryRestResource(collectionResourceRel="items", path="items")
public interface StatusRepository extends JpaRepository<Item, String>
{}
@RepositoryRestResource(collectionResourceRel="statuses", path="statuses")
public interface StatusRepository extends JpaRepository<Status, String>
{}
留给子孙后代。
为方便起见:
1) JSON
中实体的公开 ID/**
* Exposing Ids as properties for entities specified
*/
@Configuration
public class ExposeIdConfig extends RepositoryRestMvcConfiguration {
@Override
protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.exposeIdsFor(Item.class, Status.class);
}
}
因此获取项目列表如下所示:
{
"_links": {
"self": {
"href": "http://localhost:8080/app/items{?page,size,sort}",
"templated": true
}
},
"_embedded": {
"as": [
{
"id": "29117425-f011-4ff9-8952-38b05d3df7f0",
"name": "item 1",
"status": {
"id": "e9192ae7-29f8-4d5e-ad62-cad8d87de9e2",
"name": "ACTIVE"
},
"_links": {
"self": {
"href": "http://localhost:8080/app/items/29117425-f011-4ff9-8952-38b05d3df7f0"
}
},
...
}