简体   繁体   中英

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

So far I have the folowing code in 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 ?

The uid is the user id in the user object and I need to pick all the users that have an id containing ab

Any help would be apreciated.

Try getting uid value with @RequestParam

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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