3

I have a base class

@MappedSuperclass
@Data //lombok annotation for getters/setter
public class BaseEntity implements Identifiable<Long> {
    @Id @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Version
    private Long version;
}

For any derived class Spring Data REST returns JSON without "id" and "version" attributes.

I found 2 solutions:

  1. Projections.
  2. Add getters/setters with another names:

    public Long getRevision() {
        return version;
    }
    
    public void setRevision(Long revision) {
        this.version = revision;
    }
    
    public Long getIdentifier() {
        return id;
    }
    
    public void setIdentifier(Long identifier) {
        this.id = identifier;
    }
    

Both solutions look like hacks. Does better approach exist?

Aleksandr
  • 110
  • 1
  • 8
  • 1
    Exposing ids is adressed and answered [here](https://stackoverflow.com/q/34973156/5873923), [here](https://stackoverflow.com/q/24839760/5873923), [here](https://stackoverflow.com/q/24936636/5873923), [here](https://stackoverflow.com/q/30912826/5873923)... exposing version is addressed [here](https://stackoverflow.com/q/36853343/5873923). – Marc Tarin Dec 11 '17 at 09:06
  • Thanks for posting your MappedSuperclass code, it helped me figure out I hadn't added the Data annotation to it, even though it was registered against my entity classes. As of Mar 2020 this was all I needed to add, all the other "exposeIds" techniques were not required, nor did they (unsurprisingly) solve my issue. – Stryder Mar 30 '20 at 18:32

1 Answers1

2

Showing the ID of the entity is configuring in the RepositoryRestConfigurerAdapter:

@Bean
public RepositoryRestConfigurerAdapter repositoryRestConfigurerAdapter() {
    return new RepositoryRestConfigurerAdapter() {
        /**
         * Exposing ID for some entities
         */
        @Override
        public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
            config.exposeIdsFor(MyEntity.class);            
            super.configureRepositoryRestConfiguration(config);
        }

    };
}
Cepr0
  • 28,144
  • 8
  • 75
  • 101