使用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不会通过rest接口公开:
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);
}
}
但是有没有一种简便的方法可以公开所有ID,而无需手动维护此configureRepositoryRestConfiguration
方法中的列表?
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屋!