Spring Boot REST 资源未显示链接对象(集) [英] Spring Boot REST Resource not showing linked objects (sets)

查看:27
本文介绍了Spring Boot REST 资源未显示链接对象(集)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库和一些类.这些类与 OneToMany 等相关联.

I've a database and some classes. These classes are linked with OneToMany, and so on.

如果我用 spring 打印对象本身,它包含所有内容.但是,如果我使用 Resource 功能打印它,它只包含变量,这些变量不是集合,也不是以其他方式与其他类链接.

If I print the object itself with spring it contains everything. But if I print it with the Resource feature, it contains only the variables, which are no collections or linked otherwise with an other class.

如何将集合添加到输出中?

How can I add the collections to the output?

推荐答案

默认情况下,Spring Data REST 除了链接之外不显示关联资源.如果您希望,您必须定义描述您想要查看的字段的投影,无论它们是您描述的简单字段还是相关资源.见

By default Spring Data REST does not show associated resources except as links. If you want that you have to define projections that describe the fields you want to see, whether they're simple fields like the ones you describe or associated resources. See

http://docs.spring.io/spring-data/rest/docs/current/reference/html/#projections-excerpts

例如,假设您有一个 Service 资源,该资源与诸如 serviceTypeserviceGroupowner 之类的资源关联,serviceInstancesdocLinks.如果您希望这些显示在响应正文中,您可以创建一个投影:

For example say you have a Service resource with associations to resources like serviceType, serviceGroup, owner, serviceInstances and docLinks. If you want those to show up in the response body you can create a projection:

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();
}

然后通过投影获取您的网址:

Then GET your URL with the projection:

http://localhost:8080/api/services/15?projection=serviceDetails

结果将包括投影的属性:

The result will include the projected properties:

{
  "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
  }, ... ],
  ...
}

这篇关于Spring Boot REST 资源未显示链接对象(集)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆