【问题标题】:Spring Data REST repository 404 from time to timeSpring Data REST 存储库 404 不时
【发布时间】:2017-10-18 01:24:49
【问题描述】:

我有带有 Spring Data REST 的 Spring Boot 应用程序。

我有以下课程:

用于身份验证的数据 JPA 存储库:

public interface UserRepository extends JpaRepository<User, Long> {
    User findByUsername(String username);
}

用于 API 使用的安全数据 REST 存储库:

@RepositoryRestResource
@Secured(Role.ROLE_USER_READ)
public interface UserDataRestRepository extends PagingAndSortingRepository<User, Long> {
    @Override
    @Secured(Role.ROLE_USER_WRITE)
    <S extends User>S save(S entity);

    @Override
    @Secured(Role.ROLE_USER_DELETE)
    void delete(Long id);
}

存储库 REST 配置器适配器:

@Configuration
public class RepositoryRestConfig extends RepositoryRestConfigurerAdapter {
    @Override
    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
        config.setRepositoryDetectionStrategy(RepositoryDetectionStrategies.ANNOTATED);
        config.setReturnBodyOnCreate(true);
        config.setReturnBodyOnUpdate(true);
        config.setReturnBodyForPutAndPost(true);
    }
}

问题是当我启动我的应用程序时,API 的数据 REST 存储库有时不可用。我猜这是因为 Spring 使用第一个 JPA 存储库重写了 User 类型的存储库 bean。

在执行器 bean 端点中,即使 REST API 显示 /users 页面的 404,我也可以看到两个 bean。

同样,这种行为对我来说是不可预测的 - 有时有效,有时无效。

您知道如何告诉 Spring 将确切的 bean 用于 Data REST 吗?

提前致谢。

【问题讨论】:

    标签: java spring spring-boot spring-data-jpa spring-data-rest


    【解决方案1】:

    经过一番调查,我发现here 发布的问题完全相同。

    另外,请参阅this one

    最后,我将 2 个存储库合并为一个,它解决了我的问题。但这绝对不是我喜欢的方式。

    【讨论】:

    • 如何实现不安全的save() 方法?在用户被授权之前,我需要在身份验证期间更改用户记录。
    • @Arthur,我们正在为此类方法使用其他名称。
    猜你喜欢
    • 2017-04-30
    • 1970-01-01
    • 2020-09-14
    • 1970-01-01
    • 2015-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-21
    相关资源
    最近更新 更多