如何使用Spring Data Rest公开resourceId [英] How to expose the resourceId with Spring Data Rest

查看:118
本文介绍了如何使用Spring Data Rest公开resourceId的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我本来要公开在实体中用@Id注释的主键.ID字段仅在资源路径上可见,而在JSON正文上不可见.

I had was to expose the primary key which is annotated with @Id in entity.the ID field is only visible on the resource path, but not on the JSON body.

推荐答案

您可以在实体级别使用RepositoryRestConfigurerAdapter进行配置.

You can configure this using the RepositoryRestConfigurerAdapter on entity level.

@Configuration
public class ExposeEntityIdRestConfiguration extends RepositoryRestConfigurerAdapter {

    @Override
    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
        config.exposeIdsFor(MyEntity.class);
    }
}

请注意,使用此功能违反了spring-data-rest的原则-sdr通过使用链接在资源之间导航来促进超媒体能够使用API​​-在这里,您的资源由链接标识和引用,因此不再需要ID.在客户端上使用id会增加构建到客户端资源的链接的复杂性.并且客户不应该被这些知识所困扰.

Be aware that using this you are working against the principles of spring-data-rest - sdr promotes hypermedia to be able to use an API by navigating between resources using links - here your resources are identified and referenced by links and thus the ids are not needed anymore. Using ids on your client pushes the complexity of constructing links to resources to the client. And the client should not be bothered with this knowledge.

这篇关于如何使用Spring Data Rest公开resourceId的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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