Expose all IDs when using Spring Data Rest

前端 未结 12 1845
北海茫月
北海茫月 2020-11-30 06:26

I\'d like to expose all IDs using a Spring Rest interface.

I know that per default an ID like this will not be exposed via the rest interface:

    @I         


        
12条回答
  •  被撕碎了的回忆
    2020-11-30 06:53

    If you want to expose the id field for all your entity classes:

    import java.util.stream.Collectors;
    
    import javax.persistence.EntityManager;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
    import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter;
    
    @Configuration
    public class MyRepositoryRestConfigurerAdapter extends RepositoryRestConfigurerAdapter {
    
        @Autowired
        private EntityManager entityManager;
    
        @Override
        public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
            config.exposeIdsFor(entityManager.getMetamodel().getEntities().stream().map(e -> e.getJavaType()).collect(Collectors.toList()).toArray(new Class[0]));
        }
    
    }
    

提交回复
热议问题