问题
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
findByNameLikemethod, the method would be exposed as../search/findByNameLikeURL. If needed, you could customize the rel and path with@RestResourceannotation. Also note your
UserRepositoryshould ideally be working only onUserobject and hence the methods you defined. In your case,UserRepositoryis returningProcess/ProcessContextobjects? Instead it should be like belowpublic 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