使用路径“/{resourcename}/search/"定义自定义方法;使用弹簧数据休息 [英] Defining custom methods with path "/{resourcename}/search/" using spring-data-rest

查看:51
本文介绍了使用路径“/{resourcename}/search/"定义自定义方法;使用弹簧数据休息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑.我不知道如何将自定义搜索"方法与在 spring-data-rest 帮助下加载的方法一起定义.您能否回答我,该框架是否具有开箱即用"的可能性?如果有,你能告诉我在哪里可以找到吗?

I am confused. I could not find out, how to define together custom "search" methods with methods that were loaded with help of spring-data-rest. Could you answer me, does the framework has this possibility "out-of-box"? And if has, could you tell me, where can i find it?

为了更深入地了解我的情况,我描述了我的问题:

For a deeper understanding my situation i describe my issue:

class UserService {
    public String getListOfWaitingUsers() {

        return userRepository.findAll(UserSpecification.isWaiting());
    }
} 

public interface UserRepository extends PagingAndSortingRepository<User, Long>{
   Page<User> findByNameLike(@Param("name") String name, Pageable pageable);
}

我希望它是这样的:

/user/search/
        findByNameLike
        findWaitingUsers

如何实现我的规范或服务方法(存储库中没有方法)将定义为路径/resource_name/search/METHOD_NAME"(存储库方法> +(方法服务规格)

How to implement that my methods of specifications or services (there is not method in repository) will define with path "/resource_name/search/METHOD_NAME" ( methods of repository + ( METHODS SERVICES OR SPECIFICATIONS)

推荐答案

  • Spring Data REST 框架基于 Spring Data Respository,因此这里可以忽略您的服务类.
  • 所有不属于 CRUD/Paging Repository 的方法都作为搜索"方法公开,前提是您使用 @Param 注释对所有参数进行了注释.所以在你的情况下,你需要按照 Spring Data 公共文档中的约定大纲.因此,一旦您实现了 findByNameLike 方法,该方法将公开为 ../search/findByNameLike URL.如果需要,您可以使用 @RestResource 注释自定义 rel 和路径.
  • 另请注意,您的 UserRepository 理想情况下应该仅适用于 User 对象以及您定义的方法.在您的情况下,UserRepository 正在返回 Process/ProcessContext 对象?相反它应该像下面

    • Spring Data REST framework is based on Spring Data Respository, so your service class can be ignored here.
    • All methods that are not part of CRUD/Paging Repository as exposed as "search" methods provided you annotated all parameters with @Param annotation. So in your case, you need to implement your method following the conventions outline in Spring Data commons docs. So once you have implementation for findByNameLike method, the method would be exposed as ../search/findByNameLike URL. If needed, you could customize the rel and path with @RestResource annotation.
    • Also note your UserRepository should ideally be working only on User object and hence the methods you defined. In your case, UserRepository is returning Process/ProcessContext objects? Instead it should be like below

      public interface UserRepository extends PagingAndSortingRepository<User, Long>{
        Page<User> findByNameLike(@Param("name") String name, Pageable pageable);
      }
      

    • 这篇关于使用路径“/{resourcename}/search/"定义自定义方法;使用弹簧数据休息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆