解决Spring Data Rest不暴露ID字段的问题

大家都知道 使用Spring data rest 会遇到列表中没有ID的问题. 

找了好久也没找到解决方案 只能一个一个加真的很麻烦

config.exposeIdsFor(User.class);

在公司郭大神的帮助下 找到了这个神奇的方法 简直太棒了

参考:http://tommyziegler.com/how-to-expose-the-resourceid-with-spring-data-rest/


特此记录 希望可以帮助到有用的朋友 如果这个文章帮到你了 ,请回复下, 谢谢

最后别忘记修改包名哦! 


/**
	 * 为了解决Spring Data Rest不暴露ID字段的问题。
	 * 参考:http://tommyziegler.com/how-to-expose-the-resourceid-with-spring-data-rest/
	 * Created by Dante on 2016/8/18.
	 */
	@Bean
    public RepositoryRestConfigurer repositoryRestConfigurer() {

        return new RepositoryRestConfigurerAdapter() {
            @Override
            public void configureRepositoryRestConfiguration(
            		
                    RepositoryRestConfiguration config) {
            		
            	final ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
            	    provider.addIncludeFilter(new AnnotationTypeFilter(Entity.class));
            	    final Set<BeanDefinition> beans = provider.findCandidateComponents("com.xxxx.xxxx");
            	    for (final BeanDefinition bean : beans) {
            	      try {
            	        config.exposeIdsFor(Class.forName(bean.getBeanClassName()));
            	      } catch (final ClassNotFoundException e) {
            	        // Can't throw ClassNotFoundException due to the method signature. Need to cast it
            	        throw new IllegalStateException("Failed to expose `id` field due to", e);
            	      }
            	    }
            }
        };
    }

猜你喜欢

转载自blog.csdn.net/winnershili/article/details/80433458