Spring Data Rest - 如何更改 JSON 摘要集合的序列化 类
Spring Data Rest - How to change the JSON serialization of a Collection of Abstract Classes
我使用 Spring Boot v1.2.5 并具有以下单个 table 继承实体:
@Entity
@table(name="view_items")
@DiscriminatorColumn(name="type")
public abstract class ViewItem {
private int position;
}
@Entity
@DiscriminatorValue(value="table")
public class Table extends ViewItem {}
@Entity
@DiscriminatorValue(value="chart")
public class Chart extends ViewItem {}
public interface ViewItemRepository
extends JpaRepository<ViewItem, Integer>{}
以及以下关联:
@Entity
@Table(name="views")
public class View {
@OneToMany
@JoinColumn(name="view_id")
@OrderBy("position")
private List<ViewItem> viewItems = new ArrayList<ViewItem>();
...
}
调用 GET /views/{id}/viewItems
return 按子类类型排序的集合(为了便于阅读,一些部分已被删除):
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: application/hal+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Wed, 23 Sep 2015 07:41:13 GMT
{
"_embedded" : {
"tables" : [ {
"position": 4,
...
"_links" : {
"self" : {
"href" : "http://localhost:8080/viewItems/3"
}
}
} ],
"charts" : [ {
"position": 2,
...
"_links" : {
"self" : {
"href" : "http://localhost:8080/viewItems/2"
}
}
} ]
}
是否有一种简单的方法来更改 JSON 序列化 view_items
像这样按位置排序? :
{
"_embedded" : {
"2": {
"type": "chart",
...
"_links" : {
"self" : {
"href" : "http://localhost:8080/viewItems/2"
}
},
"4": {
type: "table",
...
"_links" : {
"self" : {
"href" : "http://localhost:8080/viewItems/3"
}
}
所以我找到了答案。
将 View
实体的 ViewItems
集合映射类型从 List
替换为 Map
并在 [=] 上添加只读 type
字段16=] 实体成功了:
@Entity
@Table(name="views")
public class View {
@OneToMany
@JoinColumn(name="view_id")
@MapKey("position")
private Map<Integer,ViewItem> viewItems = new HashMap<Integer,ViewItem>();
...
}
@Entity
@table(name="view_items")
@DiscriminatorColumn(name="type")
public abstract class ViewItem {
@Column(insertable = false, updatable = false)
public String type;
...
}
编辑
几个月后,感谢@aux(参见here) here is the solution. A RelProvider class must be implemented, as explained here。
我使用 Spring Boot v1.2.5 并具有以下单个 table 继承实体:
@Entity
@table(name="view_items")
@DiscriminatorColumn(name="type")
public abstract class ViewItem {
private int position;
}
@Entity
@DiscriminatorValue(value="table")
public class Table extends ViewItem {}
@Entity
@DiscriminatorValue(value="chart")
public class Chart extends ViewItem {}
public interface ViewItemRepository
extends JpaRepository<ViewItem, Integer>{}
以及以下关联:
@Entity
@Table(name="views")
public class View {
@OneToMany
@JoinColumn(name="view_id")
@OrderBy("position")
private List<ViewItem> viewItems = new ArrayList<ViewItem>();
...
}
调用 GET /views/{id}/viewItems
return 按子类类型排序的集合(为了便于阅读,一些部分已被删除):
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: application/hal+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Wed, 23 Sep 2015 07:41:13 GMT
{
"_embedded" : {
"tables" : [ {
"position": 4,
...
"_links" : {
"self" : {
"href" : "http://localhost:8080/viewItems/3"
}
}
} ],
"charts" : [ {
"position": 2,
...
"_links" : {
"self" : {
"href" : "http://localhost:8080/viewItems/2"
}
}
} ]
}
是否有一种简单的方法来更改 JSON 序列化 view_items
像这样按位置排序? :
{
"_embedded" : {
"2": {
"type": "chart",
...
"_links" : {
"self" : {
"href" : "http://localhost:8080/viewItems/2"
}
},
"4": {
type: "table",
...
"_links" : {
"self" : {
"href" : "http://localhost:8080/viewItems/3"
}
}
所以我找到了答案。
将 View
实体的 ViewItems
集合映射类型从 List
替换为 Map
并在 [=] 上添加只读 type
字段16=] 实体成功了:
@Entity
@Table(name="views")
public class View {
@OneToMany
@JoinColumn(name="view_id")
@MapKey("position")
private Map<Integer,ViewItem> viewItems = new HashMap<Integer,ViewItem>();
...
}
@Entity
@table(name="view_items")
@DiscriminatorColumn(name="type")
public abstract class ViewItem {
@Column(insertable = false, updatable = false)
public String type;
...
}
编辑
几个月后,感谢@aux(参见here) here is the solution. A RelProvider class must be implemented, as explained here。