Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace default SortArgumentResolver

I need to add private static final Sort sortById = new Sort(Sort.Direction.DESC, ID); to each Pageable. I guess, the best way to do that is to create decorator/adapter for SortArgumentResolver.

I've created class:

public class IdSortArgumentResolver implements SortArgumentResolver {

private static final String ID = "id";
private static final Sort sortById = new Sort(Sort.Direction.DESC, ID);
private final SortArgumentResolver delegate;

public IdSortArgumentResolverAdapter(SortArgumentResolver delegate) {
    this.delegate = delegate;
}

@Override
public boolean supportsParameter(MethodParameter methodParameter) {
    return delegate.supportsParameter(methodParameter);
}

@Override
public Sort resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) {
    Sort sort = delegate.resolveArgument(parameter, mavContainer, webRequest, binderFactory);
    if (isNull(sort)) {
        return sortById;
    }
    if (containsSortById(sort)) {
        return sort;
    }
    return sort.and(sortById);
}

private static boolean containsSortById(Sort currentSort) {
    return StreamSupport.stream(currentSort.spliterator(), false)
                        .anyMatch(order -> ID.equalsIgnoreCase(order.getProperty()));
}

}

How can I change default SortArgumentResolver to IdSortArgumentResolver ? Is it possible? or maybe there is a better way to do that...

P.S It's spring-boot 1.5.2 RELEASE and current SortHandlerMethodArgumentResolver is configured in SpringDataWebConfiguration

like image 757
Lukasz Avatar asked May 18 '26 05:05

Lukasz


2 Answers

Note: in your post, your class name is IdSortArgumentResolver but your constructor is IdSortArgumentResolverAdapter.

Create a configuration class extending WebMvcConfigurerAdapter:

@Configuration
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {

    // ...
}

Override the addArgumentResolvers method and configure your Sort and Pageable resolvers:

@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
    // FIXME Replace null with what you want
    SortArgumentResolver sortResolver = new IdSortArgumentResolver(null);

    // For sorting resolution alone
    argumentResolver.add(sortResolver);

    PageableHandlerMethodArgumentResolver pageableResolver = new PageableHandlerMethodArgumentResolver(sortResolver);

    // For sorting resolution encapsulated inside a pageable
    argumentResolver.add(pageableResolver);
}

Better solution without a custom class:

@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
    SortHandlerMethodArgumentResolver sortResolver = new SortHandlerMethodArgumentResolver();

    sortResolver.setFallbackSort(new Sort(Sort.Direction.DESC, "id"));

    // For sorting resolution alone
    argumentResolver.add(sortResolver);

    PageableHandlerMethodArgumentResolver pageableResolver = new PageableHandlerMethodArgumentResolver(sortResolver);

    // For sorting resolution encapsulated inside a pageable
    argumentResolver.add(pageableResolver);
}
like image 172
kagmole Avatar answered May 20 '26 22:05

kagmole


Starting in spring-data-commons version 2.0, there is are 2 new classes that will make this kind of thing easier:

  • SortHandlerMethodArgumentResolverCustomizer
  • PageableHandlerMethodArgumentResolverCustomizer

Unfortunately that's not the version that ships with the current version of Spring Boot, so replace at your own risk.

@Bean SortHandlerMethodArgumentResolverCustomizer sortCustomizer() {
    // s is SortHandlerMethodArgumentResolver
    return s -> s.setPropertyDelimiter("<-->");
}

In this case, one would probably call resolveArgument to manipulate it.

Spring Data Web Support

like image 20
Snekse Avatar answered May 20 '26 21:05

Snekse



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!