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:

@Configuration
public class MyCoolConfiguration extends RepositoryRestMvcConfiguration {

    @Override
    protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
        config.exposeIdsFor(FooEntity.class);
    }
}

The entity class could look like that:

@Entity
public class FooEntity {

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

    String name;
}

With this configuration you will receive your entity id back.

{
  "_links":{
  "self":{
    "href":"http://localhost:8081/api/fooentity/1"
  }
},
  "id":1,
  "name":"bar"
}

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.

Standard

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 application.properties. The reason I noticed is because my base path was not set. „spring.data.rest.basePath“.

    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:

      @Configuration
      @EnableConfigurationProperties(RepositoryRestProperties.class)
      public class MyRepositoryConfig extends RepositoryRestMvcConfiguration {

      @Autowired
      private RepositoryRestProperties properties;

      @Bean
      @Override
      public RepositoryRestConfiguration config() {
      RepositoryRestConfiguration config = super.config();
      this.properties.applyTo(config);
      return config;
      }

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

      }

      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
    DEPRECATED

    Instead extend RepositoryRestConfigurerAdapter and override according to documentation:
    http://docs.spring.io/spring-data/rest/docs/current/api/org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.html

    See documentation:

    protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config)
    Deprecated.
    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:

    @Component
    public class SpringDataRestCustomization extends RepositoryRestConfigurerAdapter {

    @Override
    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {

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

    (see http://docs.spring.io/spring-data/rest/docs/2.5.1.RELEASE/reference/html/#customizing-sdr)

Schreibe einen Kommentar

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