Java, Spring

How to expose the resourceId with Spring-Data-Rest?

Spring-Data-Rest is a quite new project in the Spring family of pivotal. The intention of this project is to reduce the boilercode of controllers and services when you need only CRUD methods on an entity for your REST resources. – Quote from project page is “Spring-Data-Rest makes it easy to expose JPA based repositories as RESTful endpoints.”

One requirement I had was to expose the CRUD identifier and database primary key which is annotated with @Id. In my case that was needed because the field was functional required. For that I had to expose it because at the standard configuration the ID field is only visible on the resource path, but not on the JSON body.

To expose it you need to configure your RepositoryRestMvcConfiguration like that:

public class MyCoolConfiguration extends RepositoryRestMvcConfiguration {

    protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {

The entity class could look like that:

public class FooEntity {

    @GeneratedValue(strategy= GenerationType.AUTO)
    Long id;

    String name;

With this configuration you will receive your entity id back.


Additional Comment from a Spring-Developer: URI stands for unique, resource, *identifier* – The URI *is* the id. What I expose here is a database internals.


11 thoughts on “How to expose the resourceId with Spring-Data-Rest?

  1. contoh rpp sd terbaru 2013 says:

    I think this is among the such a lot vital info for me. And i’m happy reading your article. However wanna observation on few common issues, The website style is wonderful, the articles is in reality nice : D. Just right task, cheers

  2. Ruixiang Jiang says:

    Hello, I have read your great article about how to expose the ID property when using the Spring Data Rest.
    But unfortunately the ID property is still not exposed according to your settings. So could you kindly tell me the concrete configuration for the „MyCoolConfiguration“, I use @Component for the „MyCoolConfiguration“ and @ComponentScan for it, but unfortunately it did not work, Sorry to bother you ! Thanks very much

  3. Hello RUIXIANG, thx that you like the article! I always enjoy if people tell me that my experience helps. – To your problem, do you have a sample project where it doesn’t work!? Maybe the best on Github (Gist), or drop the code here!

    Sry for replying so late. Have a nice day …

  4. Steen Brahe says:

    Thanks Tommy, this was exactly what I needed. Spring Data REST is nice, but it is a pain to use when you dont want to use the HATEOAS approach, and then the id is needed.

  5. This is a great article. I found that if you add the configuration class spring boot ignores your The reason I noticed is because my base path was not set. „“.

    How did you go about resolving this problem in your project?

    • Brian Smith says:

      I ran into the same issue, it’s because Spring Boot is skipping it’s entire auto-configuration if you configure a RepositoryRestMvcConfiguration bean in the context. I borrowed a bit of magic from the auto-config class and added it into my own; I would have just extended their class but it was not public.

      Long story short, I wound up with this:

      public class MyRepositoryConfig extends RepositoryRestMvcConfiguration {

      private RepositoryRestProperties properties;

      public RepositoryRestConfiguration config() {
      RepositoryRestConfiguration config = super.config();;
      return config;

      protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {


      Hope that helps!

  6. Mikko Helin says:

    This is old but guess someone will google to this page one every day.

    Anyway, the method configureRepositoryRestConfiguration is now

    Instead extend RepositoryRestConfigurerAdapter and override according to documentation:

    See documentation:

    protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config)
    since 2.4, implement RepositoryRestConfigurer.configureRepositoryRestConfiguration(RepositoryRestConfiguration) either directly or extend RepositoryRestConfigurerAdapter and override the method.

  7. With Spring Boot you simply list the classes that should expose the ID via RepositoryRestConfigurerAdapter:

    public class SpringDataRestCustomization extends RepositoryRestConfigurerAdapter {

    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {

    config.exposeIdsFor(Account.class, Category.class, Transaction.class, User.class);


Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.