简体   繁体   English

使用来自请求的Spring过滤数据

[英]FIlter data with Spring coming from request

I have some infromation coming from request such as: 我有一些来自请求的信息,例如:

http://localhost:9080/online/accounts/list/usersQuery?filter=uid&value=ab

And I have to treat this in Spring where the object is uid and the filter value is ab 我必须在Spring中处理这个问题,其中对象是uid并且过滤器值为ab

So far I have the folowing code in Spring : 到目前为止,我在Spring有以下代码:

@RequestMapping(produces="application/json", value = "/usersQuery", method=RequestMethod.GET)
public @ResponseBody PagedResources<Resource<UserDetails>> listItemsSortQuery(@PageableDefault(size = 20, page = 0) Pageable pageable, PagedResourcesAssembler<UserDetails> assembler) {


    Page<UserDetails> lstUserDetails = userDetailsRepository.findAll(pageable);

    return assembler.toResource(lstUserDetails);
}

But it doesn't consider nothing about those two values. 但这对这两个值没有任何考虑。

What should I change in order to filter data according to the field uid and filter data ab ? 为了根据字段uid和过滤数据ab过滤数据,我应该更改什么?

The uid is the user id in the user object and I need to pick all the users that have an id containing ab uiduser对象中的user id ,我需要选择id包含ab所有用户

Any help would be apreciated. 任何帮助将不胜感激。

Try getting uid value with @RequestParam 尝试使用@RequestParam获取uid值

    @RequestMapping(produces="application/json", value = "/usersQuery", method=RequestMethod.GET)
    public @ResponseBody PagedResources<Resource<UserDetails>> listItemsSortQuery(@PageableDefault(size = 20, page = 0) Pageable pageable, PagedResourcesAssembler<UserDetails> assembler, 
  @RequestParam("filter")String filter, @RequestParam("value")String value) {


        Page<UserDetails> lstUserDetails = userDetailsRepository.findByFilter(pageable, filter, value);

        return assembler.toResource(lstUserDetails);
    }

EDITED: 编辑:

In your repository you need a method to filter your data, ie 在您的存储库中,您需要一种过滤数据的方法,即

    public interface UserDetailsRepository extends JpaRepository<UserDetails, Long> {
           @Query("SELECT u FROM UserDetails u WHERE LOWER(?1) = LOWER(?2)")
           Page<UserDetails> findByFilter(String filter, String value, Pageable pageable);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM