Baiteman Baiteman - 3 months ago 44
reST (reStructuredText) Question

Expose all IDs when using Spring Data Rest

I'd like to expose all IDs using a Spring Rest interface.
I know that per default an ID like this

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(unique=true, nullable=false)
private Long id;


will not be exposed via the rest interface.

I'm aware that I can use this

@Configuration
public class RepositoryConfig extends RepositoryRestMvcConfiguration {
@Override
protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.exposeIdsFor(User.class);
}


to expose the IDs for the user.

But is there an easy way to expose all of my IDs without manually maintaing a list in this configureRepository method?

Thanks in advance!

Answer

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.

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.

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).