我需要为每个private static final Sort sortById = new Sort(Sort.Direction.DESC, ID);
添加Pageable
。我想,最好的方法是为SortArgumentResolver
创建装饰器/适配器。
我创建了课程:
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()));
}
}
如何将默认SortArgumentResolver
更改为IdSortArgumentResolver
?可能吗?或者有更好的方法来做到这一点......
在spring-boot 1.5.2 RELEASE
SortHandlerMethodArgumentResolver
和当前SpringDataWebConfiguration
答案 0 :(得分:2)
注意:在您的帖子中,您的班级名称为IdSortArgumentResolverAdapter
,但您的构造函数为WebMvcConfigurerAdapter
。
创建一个扩展@Configuration
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {
// ...
}
的配置类:
addArgumentResolvers
覆盖Sort
方法并配置您的Pageable
和@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);
}
解析器:
@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);
}
没有自定义类的更好的解决方案:
jQuery.each(titles, function( index, value ) {
$("#title").fadeIn(200).delay(500).fadeOut(200, function() {
$("#title").text(value);
});
});
答案 1 :(得分:1)
从spring-data-commons
版本2.0
开始,有2个新类可以让这种事情变得更容易:
SortHandlerMethodArgumentResolverCustomizer
PageableHandlerMethodArgumentResolverCustomizer
不幸的是,这不是当前版本的Spring Boot随附的版本,因此请自行承担风险。
@Bean SortHandlerMethodArgumentResolverCustomizer sortCustomizer() {
// s is SortHandlerMethodArgumentResolver
return s -> s.setPropertyDelimiter("<-->");
}
在这种情况下,可能会调用resolveArgument
来操纵它。
答案 2 :(得分:1)
对于春季靴2.2.1发布:
@Configuration
public class LocaleConfiguration implements WebMvcConfigurer {
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
SortHandlerMethodArgumentResolver sortArgumentResolver = new SortHandlerMethodArgumentResolver();
Sort defaultSort = Sort.by(new Sort.Order(Sort.Direction.DESC, "id"));
sortArgumentResolver.setFallbackSort(defaultSort);
PageableHandlerMethodArgumentResolver resolver = new PageableHandlerMethodArgumentResolver(sortArgumentResolver);
resolver.setOneIndexedParameters(true);
resolver.setMaxPageSize(20);
resolvers.add(resolver);
}
}