spring-boot-starter-data-rest 资源暴露策略设置 RepositoryDetectionStrategies

最近一直在学习spring boot,今天学到了spring boot 的 rest,就照着网上的教程编码(http://blog.csdn.net/qiutongyeluo/article/details/51900509)。

代码配置也没多少,一下子就敲好了,然后启动运行并访问,然而,并没有得到预期的结果,返回错误页面404状态码。既然出错了,那就只好找了,将案例重头看一次,和本地的对比,发现只有spring-boot-starter-parent的版本不一样,案例上的是1.3.6.RELEASE,我本地的是1.5.1.RELEASE,引用的spring-data-rest的版本也不同,分别是2.4.4和2.6.0,我将我本地的spring-boot-starter-parent改1.3.6.RELEASE后运行就访问成功了,改回1.5.1.RELEASE就失败。

然后我就去百度一下两个版本的差异,但也没找到什么资料,最后只好去看官网纯英文的文档,一边有道一边看,发现spring-data-rest版本从2.5.x开始增加了资源暴露策略设置。

暴露策略有四个(下面中文是按我自己理解写的):

Name Description
DEFAULT Exposes all public repository interfaces but considers @(Repository)RestResource’s `exported flag.
默认值,策略为:ANNOTATED + VISIBILITY两个同时生效
ALL Exposes all repositories independently of type visibility and annotations.
暴露所有资源
ANNOTATION Only repositories annotated with @(Repository)RestResource are exposed, unless their exported flag is set to false.
暴露@RepositoryRestResource和@RestResource并exported为true的资源
VISIBILITY Only public repositories annotated are exposed.
暴露所有public的资源



配置方式:

@Component
public class RestConfigurer extends RepositoryRestConfigurerAdapter {

    @Override
    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
        config.setRepositoryDetectionStrategy(RepositoryDetectionStrategy.RepositoryDetectionStrategies.VISIBILITY);
        super.configureRepositoryRestConfiguration(config);
    }
}
@Configuration
public class RestConfigurer{

    @Bean
    public RepositoryRestConfigurer repositoryRestConfigurer() {

        return new RepositoryRestConfigurerAdapter() {
            @Override
            public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
                config.setRepositoryDetectionStrategy(RepositoryDetectionStrategy.RepositoryDetectionStrategies.VISIBILITY);
            }
        };
    }
}

将策略改为VISIBILITY就可以像以前那写rest了

你可能感兴趣的:(spring-boot-starter-data-rest 资源暴露策略设置 RepositoryDetectionStrategies)