加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

java – 有没有办法在没有Projection的情况下在spring数据中返

发布时间:2020-12-15 01:26:58 所属栏目:大数据 来源:网络整理
导读:我使用的是spring-boot-starter-parent 1.4.1.RELEASE. Spring启动@ResponseBody默认情况下不会序列化实体ID. 如果我使用exposeIdsFor返回id,但我需要为每个类配置它 例如 RepositoryRestConfiguration.exposeIdsFor(User.class); RepositoryRestConfigurati

我使用的是spring-boot-starter-parent 1.4.1.RELEASE.

> Spring启动@ResponseBody默认情况下不会序列化实体ID.

如果我使用exposeIdsFor返回id,但我需要为每个类配置它

例如

RepositoryRestConfiguration.exposeIdsFor(User.class);
RepositoryRestConfiguration.exposeIdsFor(Address.class);

是否有更简单的配置来执行此操作而不配置每个实体类.

参见:https://jira.spring.io/browse/DATAREST-366

> REST资源将该属性作为URI呈现给它对应的关联资源.我们需要返回关联的对象而不是URI.

如果我使用Projection,它将返回关联的对象,但我需要为每个类配置它

例如

@Entity
public class Person {

  @Id @GeneratedValue
  private Long id;
  private String firstName,lastName;

  @ManyToOne
  private Address address;
  …
}

PersonRepository:

interface PersonRepository extends CrudRepository

PersonRepository返回,

{
  "firstName" : "Frodo","lastName" : "Baggins","_links" : {
    "self" : {
      "href" : "http://localhost:8080/persons/1"
    },"address" : {
      "href" : "http://localhost:8080/persons/1/address"
    }
  }
}

我的预测:

@Projection(name = "inlineAddress",types = { Person.class }) 
interface InlineAddress {

  String getFirstName();

  String getLastName();

  Address getAddress(); 
}

添加投影后,它返回..

{
  "firstName" : "Frodo","address" : { 
    "street": "Bag End","state": "The Shire","country": "Middle Earth"
  },"address" : { 
      "href" : "http://localhost:8080/persons/1/address"
    }
  }
}

如果其他一些类具有关联作为地址,那么我还需要为这些类添加投影.

但我不想为每个类配置.如何实现动态投影?这样我的所有实体都将返回嵌套对象作为响应.

参见:https://jira.spring.io/browse/DATAREST-221

最佳答案
关于您的第一个问题,已经回答了here,exposeIdsFor接受了任意数量的参数.另外,如提到in this thread如果所有实体类都位于同一个包中,您可以使用Reflections library获取类的列表并将其提供给exposeIdsFor().

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读