Expose all IDs when using Spring Data Rest

前端 未结 12 1734
北海茫月
北海茫月 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]));
        }
    
    }
    
    0 讨论(0)
  • 2020-11-30 06:55

    You can use this method to find all @Entity classes of the EntityManagerFactory:

    private List<Class<?>> getAllManagedEntityTypes(EntityManagerFactory entityManagerFactory) {
        List<Class<?>> entityClasses = new ArrayList<>();
        Metamodel metamodel = entityManagerFactory.getMetamodel();
        for (ManagedType<?> managedType : metamodel.getManagedTypes()) {
            Class<?> javaType = managedType.getJavaType();
            if (javaType.isAnnotationPresent(Entity.class)) {
                entityClasses.add(managedType.getJavaType());
            }
        }
        return entityClasses;
    }
    

    then, to expose the IDs for all your entity classes:

    @Configuration
    public class RestConfig extends RepositoryRestMvcConfiguration {
    
        @Bean
        public RepositoryRestConfigurer repositoryRestConfigurer(EntityManagerFactory entityManagerFactory) {
            List<Class<?>> entityClasses = getAllManagedEntityTypes(entityManagerFactory);
    
            return new RepositoryRestConfigurerAdapter() {
    
                @Override
                public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
                    for (Class<?> entityClass : entityClasses) {
                        config.exposeIdsFor(entityClass);
                    }
                }
        }
    }
    
    0 讨论(0)
  • 2020-11-30 06:57

    You can try with this solution: - First import reflections library to your POM file:

    <dependency>
        <groupId>org.reflections</groupId>
        <artifactId>reflections</artifactId>
        <version>0.9.11</version>
    </dependency>
    

    - Then change your RepositoryConfig class to:

    @Configuration
    public class RepositoryConfig extends RepositoryRestMvcConfiguration {
        @Override
        protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
            Reflections reflections = new Reflections("com.example.entity");
            Set<Class<?>> idExposedClasses = reflections.getTypesAnnotatedWith(Entity.class, false);
            idExposedClasses.forEach(config::exposeIdsFor);
            return config;
        }
    }
    

    Change "com.example.entity" to your Entity package and you are good to go. Good luck!

    0 讨论(0)
  • 2020-11-30 06:58

    Proabably you can try this to include all id fields. I havent tried it yet, but will keep posted.

     public class ExposeAllRepositoryRestConfiguration extends RepositoryRestConfiguration {
        @Override
        public boolean isIdExposedFor(Class<?> domainType) {
            return true;
            }
        }
    

    Excerpt from this link

    0 讨论(0)
  • 2020-11-30 06:59

    Full working example based on Francois Gengler answer (please upvote his answer but not mine):

    @SpringBootApplication
    public class DataRestApplication {
        public static void main(String[] args) {
            SpringApplication.run(DataRestApplication.class, args);
        }
    
        @Bean
        public RepositoryRestConfigurer repositoryRestConfigurer(EntityManager entityManager) {
            return RepositoryRestConfigurer.withConfig(config -> {
                config.exposeIdsFor(entityManager.getMetamodel().getEntities()
                        .stream().map(Type::getJavaType).toArray(Class[]::new));
            });
        }
    }
    
    0 讨论(0)
  • 2020-11-30 07:05

    Following piece of code looks prettier:

    .exposeIdsFor(entityManager.getMetamodel().getEntities().stream().map(entityType -> entityType.getJavaType()).toArray(Class[]::new))
    
    0 讨论(0)
提交回复
热议问题