【问题标题】:Filtering jpa built-in rest resources过滤jpa内置的rest资源
【发布时间】:2019-11-08 05:41:33
【问题描述】:

试图公开存储库休息资源。

我希望 findAll()findById() 方法是公开的(即使用户未连接也可访问),其余方法仅在经过身份验证的用户具有 ROLE_ADMIN 时才可访问

@RepositoryRestResource
@PreAuthorize("hasRole('ROLE_ADMIN')")
public interface FilliereServices extends JpaRepository<Filliere, Integer> {

    @PreAuthorize("permitAll")
    public List<Filliere> findAll();

    @PreAuthorize("permitAll")
    public Optional<Filliere> findById(Integer id);
}

localhost:8080/fillieres 无法按预期访问,但是 localhost:8080/fillieres/search/findAll 抛出一个

org.springframework.data.rest.webmvc.ResourceNotFoundException

我在findAll() 上尝试过@RestResource(path="findAll"),但同样的问题。 但是,如果我只添加@Query("FROM Filliere"),它就像一个魅力。

有什么想法吗?

【问题讨论】:

    标签: spring spring-boot spring-security spring-data-jpa spring-data


    【解决方案1】:

    如果您覆盖 JpaRepository 默认方法,那么您将覆盖默认 REST 端点的行为。这些方法将不会出现在search 路径下。 findAll()在你尝试访问localhost:8080/fillieres时使用,findById(Integer id)在你调用localhost:8080/fillieres/{id}时使用。

    【讨论】:

    • 感谢您的信息,有什么方法可以让它们出现(在搜索路径下或不在搜索路径下)而不必用@Query 注释标记它们?
    • 你必须使用不同的方法名。
    • 例如,我可以用什么作为名称以与 findAll 具有相同的行为(不使用 @Query)?
    • findAllByIdIsNotNull 可以工作。您可以使用@RestResource 将实际路径重命名为更简单的名称。
    猜你喜欢
    • 2017-10-19
    • 2017-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-16
    • 2011-02-15
    • 1970-01-01
    • 2017-04-22
    相关资源
    最近更新 更多