默认情况下,Spring Data REST 不显示关联资源,除了链接。如果您希望,您必须定义描述您想要查看的字段的投影,无论它们是您描述的简单字段还是相关资源。见
http://docs.spring.io/spring-data/rest/docs/current/reference/html/#projections-excerpts
例如,假设您有一个 Service 资源,该资源与 serviceType、serviceGroup、owner、serviceInstances 和 docLinks 等资源相关联。如果您希望这些显示在响应正文中,您可以创建一个投影:
package my.app.entity.projection;
import org.springframework.data.rest.core.config.Projection;
...
@Projection(name = "serviceDetails", types = Service.class)
public interface ServiceDetails {
String getKey();
String getName();
ServiceType getType();
ServiceGroup getGroup();
Person getOwner();
List<ServiceInstance> getServiceInstances();
List<DocLink> getDocLinks();
String getPlatform();
}
然后获取带有投影的 URL:
http://localhost:8080/api/services/15?projection=serviceDetails
结果将包括投影属性:
{
"name" : "MegaphoneService",
"key" : "megaphone",
"type" : {
"key" : "application",
"name" : "User Application",
"description" : "A service that allows users to use a megaphone."
},
"owner" : null,
"serviceInstances" : [ {
"key" : "megaphone-a-dr",
"description" : null,
"loadBalanced" : true,
"minCapacityDeploy" : null,
"minCapacityOps" : 50
}, ... ],
...
}