我有一个标准的Spring数据JPA和Spring数据Rest设置,它正确地返回关联作为正确资源的链接.
{
"id": 1,
"version": 2,
"date": "2011-11-22",
"description": "XPTO",
"_links": {
"self": {
"href": "http://localhost:8000/api/domain/1"
},
"otherDomain": {
"href": "http://localhost:8000/api/domain/1/otherDomain"
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是在某些请求中,我希望扩展与"otherDomain"的关联(因此客户端不必执行N + 1个请求来获取完整数据).
是否可以配置Spring Data Rest以这种方式处理响应?
我有两个这样的课程:
public class A {
String aProp = "aProp";
public String getAProp() {
return aProp;
}
}
public class B {
String bProp = "bProp";
A a = new A();
@JsonProperty("bProp")
public String getBProp() {
return bProp;
}
@JsonSerialize(using = CustomSerializer.class)
public A getA() {
return a;
}
}
Run Code Online (Sandbox Code Playgroud)
我期待得到像这样的JSON:
{
"bProp": "bProp", // just serizlised bProp
"sProp1": "sProp1_aProp", // computed using aProp
"sProp2": "sProp2_aProp" // computed another way
}
Run Code Online (Sandbox Code Playgroud)
所以我写了这样的习惯JsonSerializer:
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
public …Run Code Online (Sandbox Code Playgroud)