简体   繁体   中英

How to manually create Pageable object from a string

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

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.

For that purpose I've written something similar to what spring has, i'd be happy if spring exposes SortHandlerMethodArgumentResolver.parseParameterIntoSort for usage outside the package but so far it's not

private Sort parseMultipleSortQueries(final String query) {

    final String[] queries = query.split("&");
    return parseSortQuery(queries, ",");
}

private Sort parseSortQuery(final String[] query, String delimiter) {

    final List<Sort.Order> orders = new ArrayList<>();
    for (String q : query) {

        if (q == null) {
            continue;
        }

        final String[] parts = q.split(delimiter);
        final Sort.Direction direction = parts.length == 0 ? null : Sort.Direction.fromStringOrNull(parts[parts.length - 1]);
        for (int i = 0; i < parts.length; i++) {

            if (i == parts.length - 1 && direction != null) {
                continue;
            }

            final String property = parts[i];
            if (!StringUtils.hasText(property)) {
                continue;
            }
            orders.add(new Sort.Order(direction, property));
        }
    }
    return orders.isEmpty() ? null : new Sort(orders);
}

and here is the test

@Test
public void testParseQuery() {
    System.out.println(parseMultipleSortQueries("firstName,asc&lastName,desc")); //firstName: ASC,lastName: DESC
}

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