我跑过类似的情况,而且我最终做出了一个处理全局的方面。如果你需要一个一个的案例,我没有答案,但至少,这个代码可以让你朝着正确的方向前进。
@Aspect
@Component
public class SortManipulatingAspect {
@Around("execution(public * org.springframework.data.repository.PagingAndSortingRepository+.*(..))")
public Object enableIgnoreCaseSorting(ProceedingJoinPoint joinPoint) throws Throwable {
return joinPoint.proceed(
Arrays.stream(joinPoint.getArgs()).map(SortManipulatingAspect::sortWithIgnoreCase).toArray()
);
}
private static Object sortWithIgnoreCase(Object arg) {
if (arg instanceof PageRequest) {
return pageRequestIgnoreCaseSort((PageRequest)arg);
} else {
return arg;
}
}
private static PageRequest pageRequestIgnoreCaseSort(PageRequest pageRequest) {
return new PageRequest(
pageRequest.getPageNumber(),
pageRequest.getPageSize(),
pageRequest.getSort() != null ? new Sort(toOrderStream(pageRequest.getSort())) : null
);
}
private static List<Sort.Order> toOrderStream(Sort sort) {
return StreamSupport.stream(sort.spliterator(), false)
.map(Sort.Order::ignoreCase)
.collect(Collectors.toList());
}
}