我花了很多时间在Webflux上找到关于Pageable的解决方案,不幸的是,在撰写本文时,Webflux不支持Pageable,所以我提出了一个解决方案,这就是我实现的。这是一个解决Spring Boot中Webflux控制器/资源的Pageable和Sort类型的hacky解决方案。
以下是解决方案:(我知道它可能很丑,但你可以使用它直到Spring团队解决问题,正在进行努力。Spring Jira
此外,要点是:Github
更新1:您也可以找到文章here in medium
版本:
Spring Boot: 2.0.2.RELEASE
Gradle Kotlin: 1.2.41
随意给我提示改进它。 (代码是Kotlin)
@Configuration
class PageableSerializer {
@Bean
@ConditionalOnMissingBean
fun pageableCustomizer(properties: SpringDataWebProperties): PageableHandlerMethodArgumentResolverCustomizer {
return PageableHandlerMethodArgumentResolverCustomizer { resolver ->
val pageable = properties.pageable
resolver.setPageParameterName(pageable.pageParameter)
resolver.setSizeParameterName(pageable.sizeParameter)
resolver.setOneIndexedParameters(pageable.isOneIndexedParameters)
resolver.setPrefix(pageable.prefix)
resolver.setQualifierDelimiter(pageable.qualifierDelimiter)
resolver.setFallbackPageable(PageRequest.of(0, pageable.defaultPageSize))
resolver.setMaxPageSize(pageable.maxPageSize)
}
}
@Bean
@ConditionalOnMissingBean
fun sortCustomizer(properties: SpringDataWebProperties): SortHandlerMethodArgumentResolverCustomizer {
return SortHandlerMethodArgumentResolverCustomizer { resolver ->
resolver.setSortParameter(properties.sort.sortParameter)
}
}
@Bean
fun pageableHandler(pageableResolver: Optional<PageableHandlerMethodArgumentResolverCustomizer>, sortHandler: SortHandlerMethodArgumentResolver, reactiveAdapterRegistry: ReactiveAdapterRegistry): PageableHandlerMethodArgumentResolver {
val handler = PageableHandlerMethodArgumentResolver(sortHandler)
pageableResolver.ifPresent { c -> c.customize(handler) }
return handler
}
@Bean
fun sortHandler(sortResolver: Optional<SortHandlerMethodArgumentResolverCustomizer>): SortHandlerMethodArgumentResolver {
val handler = SortHandlerMethodArgumentResolver()
sortResolver.ifPresent { c -> c.customize(handler) }
return handler
}
}
并注册处理程序:
@Component
class PageableResolver(registry: ReactiveAdapterRegistry, private val resolver: PageableHandlerMethodArgumentResolver) : HandlerMethodArgumentResolverSupport(registry) {
override fun resolveArgument(parameter: MethodParameter, bindingContext: BindingContext, exchange: ServerWebExchange): Mono<Any> {
return Mono.just(resolver.resolveArgument(parameter, null, MockNative(exchange.request.queryParams), null))
}
override fun supportsParameter(parameter: MethodParameter): Boolean {
return this.resolver.supportsParameter(parameter)
}
private class MockNative(private val parameters: MultiValueMap<String, String>) : NativeWebRequest {
override fun getParameter(paramName: String): String? {
return this.parameters.getFirst(paramName)
}
override fun getParameterValues(paramName: String): Array<String>? {
return this.parameters[paramName]?.toTypedArray()
}
override fun isUserInRole(role: String): Boolean {
return false
}
override fun getRemoteUser(): String? {
return null
}
override fun getLocale(): Locale {
return Locale.getDefault()
}
override fun getParameterMap(): MutableMap<String, Array<String>> {
return mutableMapOf()
}
override fun getSessionId(): String {
return ""
}
override fun getAttributeNames(scope: Int): Array<String> {
return arrayOf()
}
override fun registerDestructionCallback(name: String, callback: Runnable, scope: Int) {
}
override fun resolveReference(key: String): Any? {
return null
}
override fun getHeaderValues(headerName: String): Array<String>? {
return null
}
override fun getUserPrincipal(): Principal? {
return null
}
override fun getDescription(includeClientInfo: Boolean): String {
return ""
}
override fun getSessionMutex(): Any {
return ""
}
override fun getNativeResponse(): Any? {
return null
}
override fun <T : Any?> getNativeResponse(requiredType: Class<T>?): T? {
return null
}
override fun getParameterNames(): MutableIterator<String> {
return mutableListOf<String>().iterator()
}
override fun getNativeRequest(): Any {
return ""
}
override fun <T : Any?> getNativeRequest(requiredType: Class<T>?): T? {
return null
}
override fun removeAttribute(name: String, scope: Int) {
}
override fun getHeader(headerName: String): String? {
return null
}
override fun getContextPath(): String {
return ""
}
override fun checkNotModified(lastModifiedTimestamp: Long): Boolean {
return false
}
override fun checkNotModified(etag: String): Boolean {
return false
}
override fun checkNotModified(etag: String?, lastModifiedTimestamp: Long): Boolean {
return false
}
override fun getHeaderNames(): MutableIterator<String> {
return mutableListOf<String>().iterator()
}
override fun getAttribute(name: String, scope: Int): Any? {
return null
}
override fun setAttribute(name: String, value: Any, scope: Int) {
}
override fun isSecure(): Boolean {
return false
}
}
}
答案 0 :(得分:0)
请参见ReactivePageableHandlerMethodArgumentResolver类
WebFluxConfigure
的示例:
@Configuration
@ConditionalOnClass(EnableWebFlux.class)
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
public class WebfluxConfig implements WebFluxConfigurer {
@Override
public void configureArgumentResolvers(ArgumentResolverConfigurer configurer) {
configurer.addCustomResolver(new ReactivePageableHandlerMethodArgumentResolver());
}
}