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

本文介绍了一种有效解决SpringDataRest不暴露资源ID问题的方法。通过配置RepositoryRestConfigurer,扫描并设置所有实体类的ID公开策略,避免了手动为每个实体添加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);
            	      }
            	    }
            }
        };
    }

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值