Add Kotlin extensions for function Web API
Issue: SPR-15054
This commit is contained in:
parent
bb94ba6e3f
commit
3626a1c7f9
|
|
@ -858,6 +858,8 @@ project("spring-web-reactive") {
|
|||
optional("io.undertow:undertow-websockets-jsr:${undertowVersion}") {
|
||||
exclude group: "org.jboss.spec.javax.websocket", module: "jboss-websocket-api_1.1_spec"
|
||||
}
|
||||
optional("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
|
||||
optional("org.jetbrains.kotlin:kotlin-stdlib:${kotlinVersion}")
|
||||
testCompile("io.projectreactor.addons:reactor-test:${reactorCoreVersion}")
|
||||
testCompile("javax.validation:validation-api:${beanvalVersion}")
|
||||
testCompile("org.hibernate:hibernate-validator:${hibval5Version}")
|
||||
|
|
@ -871,8 +873,6 @@ project("spring-web-reactive") {
|
|||
testCompile("com.fasterxml:aalto-xml:1.0.0")
|
||||
testCompile("org.xmlunit:xmlunit-matchers:${xmlunitVersion}")
|
||||
testCompile("org.slf4j:slf4j-jcl:${slf4jVersion}")
|
||||
testCompile("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
|
||||
testCompile("org.jetbrains.kotlin:kotlin-stdlib:${kotlinVersion}")
|
||||
testCompile("com.squareup.okhttp3:mockwebserver:${okhttp3Version}")
|
||||
testRuntime("javax.el:javax.el-api:${elApiVersion}")
|
||||
testRuntime("org.glassfish:javax.el:3.0.1-b08")
|
||||
|
|
|
|||
|
|
@ -0,0 +1,39 @@
|
|||
package org.springframework.web.reactive.function
|
||||
|
||||
import org.springframework.http.ReactiveHttpInputMessage
|
||||
import reactor.core.publisher.Flux
|
||||
import reactor.core.publisher.Mono
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
/**
|
||||
* Extension for [BodyExtactors] providing [KClass] based API and avoiding specifying
|
||||
* a class parameter when possible thanks to Kotlin reified type parameters.
|
||||
*
|
||||
* @since 5.0
|
||||
*/
|
||||
object BodyExtractorsExtension {
|
||||
|
||||
/**
|
||||
* @see BodyExtactors.toMono
|
||||
*/
|
||||
inline fun <reified T : Any> toMono() : BodyExtractor<Mono<T>, ReactiveHttpInputMessage> =
|
||||
BodyExtractors.toMono(T::class.java)
|
||||
|
||||
/**
|
||||
* @see BodyExtactors.toMono
|
||||
*/
|
||||
fun <T : Any> toMono(elementClass: KClass<T>) : BodyExtractor<Mono<T>, ReactiveHttpInputMessage> =
|
||||
BodyExtractors.toMono(elementClass.java)
|
||||
|
||||
/**
|
||||
* @see BodyExtactors.toFlux
|
||||
*/
|
||||
inline fun <reified T : Any> toFlux() : BodyExtractor<Flux<T>, ReactiveHttpInputMessage> =
|
||||
BodyExtractors.toFlux(T::class.java)
|
||||
|
||||
/**
|
||||
* @see BodyExtactors.toFlux
|
||||
*/
|
||||
fun <T : Any> toFlux(elementClass: KClass<T>) : BodyExtractor<Flux<T>, ReactiveHttpInputMessage> =
|
||||
BodyExtractors.toFlux(elementClass.java)
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
package org.springframework.web.reactive.function
|
||||
|
||||
import org.reactivestreams.Publisher
|
||||
import org.springframework.http.ReactiveHttpOutputMessage
|
||||
import org.springframework.http.server.reactive.ServerHttpResponse
|
||||
|
||||
/**
|
||||
* Extension for [BodyInserters] providing [KClass] based API and avoiding specifying
|
||||
* a class parameter when possible thanks to Kotlin reified type parameters.
|
||||
*
|
||||
* @since 5.0
|
||||
*/
|
||||
object BodyInsertersExtension {
|
||||
|
||||
/**
|
||||
* @see BodyInserters.fromPublisher
|
||||
*/
|
||||
inline fun <reified T : Publisher<S>, reified S : Any> fromPublisher(publisher: T) : BodyInserter<T, ReactiveHttpOutputMessage> =
|
||||
BodyInserters.fromPublisher(publisher, S::class.java)
|
||||
|
||||
/**
|
||||
* @see BodyInserters.fromServerSentEvents
|
||||
*/
|
||||
inline fun <reified T : Publisher<S>, reified S : Any> fromServerSentEvents(publisher: T) : BodyInserter<T, ServerHttpResponse> =
|
||||
BodyInserters.fromServerSentEvents(publisher, S::class.java)
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
package org.springframework.web.reactive.function.client
|
||||
|
||||
import reactor.core.publisher.Flux
|
||||
import reactor.core.publisher.Mono
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
/**
|
||||
* Extension for [ClientResponse] providing [KClass] based API.
|
||||
*
|
||||
* @since 5.0
|
||||
*/
|
||||
object ClientResponseExtension {
|
||||
|
||||
/**
|
||||
* @see ClientResponse.bodyToFlux
|
||||
*/
|
||||
fun <T : Any> ClientResponse.bodyToFlux(type: KClass<T>) : Flux<T> = bodyToFlux(type.java)
|
||||
|
||||
/**
|
||||
* @see ClientResponse.bodyToMono
|
||||
*/
|
||||
fun <T : Any> ClientResponse.bodyToMono(type: KClass<T>) : Mono<T> = bodyToMono(type.java)
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
package org.springframework.web.reactive.function.server
|
||||
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
/**
|
||||
* Extension for [ServerRequest] providing [KClass] based API.
|
||||
*
|
||||
* @since 5.0
|
||||
*/
|
||||
object ServerRequestExtension {
|
||||
|
||||
/**
|
||||
* @see ServerRequest.bodyToMono
|
||||
*/
|
||||
fun <T : Any> ServerRequest.bodyToMono(type: KClass<T>) = bodyToMono(type.java)
|
||||
|
||||
/**
|
||||
* @see ServerRequest.bodyToFlux
|
||||
*/
|
||||
fun <T : Any> ServerRequest.bodyToFlux(type: KClass<T>) = bodyToFlux(type.java)
|
||||
}
|
||||
Loading…
Reference in New Issue