How to manually create Pageable object from a string

Stressed out :

I didn't find anywhere whether it's possible to create pageable manually from string, let assume that we have the following service method

public <T> List<T> findAnything(final int page, final int size, final String sort) { // e.g. id,desc&username,asc

    final Pageable pageable = new PageRequest(page, size, null);
    return null;
}

my question is how can i instantiate an object of

org.springframework.data.domain.Sort

from a given string of format, important note that these parameters are chagned dynamically, so more likely i need a path to the spring parser, in my example im passing null instead the object

id,desc&username,asc

EDIT

A little bit more details I'm looking for a mechanism of how spring converts the 'sort' string(with the rest of default parameters) that's coming to the rest endpoint as a query param to Pageable object

Mykhailo Moskura :

You can do :

private Sort orderBy() {
    return new Sort(Sort.Direction.DESC, "ID")
                .and(new Sort(Sort.Direction.ASC, "username"));
}

I think this is helpful

Sort class has static nested class Order :

public static class Order{
    private final Direction direction;
    private final String property;
    private final boolean ignoreCase;
    private final NullHandling nullHandling;
}

and then you can use :

public static Sort by(List<Order> orders)

where you create your Order from String like simply splitting.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=86979&siteId=1