使用 Spring Data Rest 时暴露所有 ID [英] Expose all IDs when using Spring Data Rest
问题描述
我想使用 Spring Rest 接口公开所有 ID.
I'd like to expose all IDs using a Spring Rest interface.
我知道默认情况下这样的 ID 不会通过其余接口公开:
I know that per default an ID like this will not be exposed via the rest interface:
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(unique=true, nullable=false)
private Long id;
我知道我可以使用它来公开 User
的 ID:
I'm aware that I can use this to expose the ID for User
:
@Configuration
public class RepositoryConfig extends RepositoryRestMvcConfiguration {
@Override
protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.exposeIdsFor(User.class);
}
}
但是有没有一种简单的方法可以在不手动维护此 configureRepositoryRestConfiguration
方法中的列表的情况下公开所有 ID?
But is there an easy way to expose all IDs without manually maintaining a list in this configureRepositoryRestConfiguration
method?
推荐答案
目前,SDR 没有提供这种方法.SDR Jira 跟踪器上的 这个问题 解释了为什么这不是(和也许不应该)是可能的.
Currently, there is no way to do this provided by SDR. This issue on the SDR Jira tracker gives some explanation as to why this isn't (and perhaps shouldn't) be possible.
这个论点基本上是因为 ID 已经包含在响应中的 self
链接中,所以您不需要将它们作为对象本身的属性公开.
The argument is basically that since the IDs are already contained within the self
links in the response, you don't need to expose them as properties of the object itself.
也就是说,您可以使用反射来检索所有具有 javax.persistence.Id
注释的类,然后调用 RepositoryRestConfiguration#exposeIdsFor(Class>...domainTypes)
.
That said, you may be able to use reflection to retrieve all classes that have a javax.persistence.Id
annotation and then call RepositoryRestConfiguration#exposeIdsFor(Class<?>... domainTypes)
.
这篇关于使用 Spring Data Rest 时暴露所有 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!