我跑過類似的情況,而且我最終做出了一個處理全局的方面。如果你需要一個一個的案例,我沒有答案,但至少,這個代碼可以讓你朝着正確的方向前進。
@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());
}
}