Defining custom methods with path “/{resourcename}/search/” using spring-data-rest

a 夏天 提交于 2019-12-10 21:05:59

问题


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);
}

I want that it to be like:

/user/search/
        findByNameLike
        findWaitingUsers

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)


回答1:


  • 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);
    }
    


来源:https://stackoverflow.com/questions/23340321/defining-custom-methods-with-path-resourcename-search-using-spring-data-re

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!