diff --git a/build.gradle b/build.gradle index de15cf2df31..c9e57f28bc1 100644 --- a/build.gradle +++ b/build.gradle @@ -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") diff --git a/spring-web-reactive/src/main/kotlin/org/springframework/web/reactive/function/BodyExtractorsExtension.kt b/spring-web-reactive/src/main/kotlin/org/springframework/web/reactive/function/BodyExtractorsExtension.kt new file mode 100644 index 00000000000..53c5376368b --- /dev/null +++ b/spring-web-reactive/src/main/kotlin/org/springframework/web/reactive/function/BodyExtractorsExtension.kt @@ -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 toMono() : BodyExtractor, ReactiveHttpInputMessage> = + BodyExtractors.toMono(T::class.java) + + /** + * @see BodyExtactors.toMono + */ + fun toMono(elementClass: KClass) : BodyExtractor, ReactiveHttpInputMessage> = + BodyExtractors.toMono(elementClass.java) + + /** + * @see BodyExtactors.toFlux + */ + inline fun toFlux() : BodyExtractor, ReactiveHttpInputMessage> = + BodyExtractors.toFlux(T::class.java) + + /** + * @see BodyExtactors.toFlux + */ + fun toFlux(elementClass: KClass) : BodyExtractor, ReactiveHttpInputMessage> = + BodyExtractors.toFlux(elementClass.java) +} diff --git a/spring-web-reactive/src/main/kotlin/org/springframework/web/reactive/function/BodyInsertersExtension.kt b/spring-web-reactive/src/main/kotlin/org/springframework/web/reactive/function/BodyInsertersExtension.kt new file mode 100644 index 00000000000..b6b20aa1e39 --- /dev/null +++ b/spring-web-reactive/src/main/kotlin/org/springframework/web/reactive/function/BodyInsertersExtension.kt @@ -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 S : Any> fromPublisher(publisher: T) : BodyInserter = + BodyInserters.fromPublisher(publisher, S::class.java) + + /** + * @see BodyInserters.fromServerSentEvents + */ + inline fun , reified S : Any> fromServerSentEvents(publisher: T) : BodyInserter = + BodyInserters.fromServerSentEvents(publisher, S::class.java) +} diff --git a/spring-web-reactive/src/main/kotlin/org/springframework/web/reactive/function/client/ClientResponseExtension.kt b/spring-web-reactive/src/main/kotlin/org/springframework/web/reactive/function/client/ClientResponseExtension.kt new file mode 100644 index 00000000000..cf296ec4f24 --- /dev/null +++ b/spring-web-reactive/src/main/kotlin/org/springframework/web/reactive/function/client/ClientResponseExtension.kt @@ -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 ClientResponse.bodyToFlux(type: KClass) : Flux = bodyToFlux(type.java) + + /** + * @see ClientResponse.bodyToMono + */ + fun ClientResponse.bodyToMono(type: KClass) : Mono = bodyToMono(type.java) +} diff --git a/spring-web-reactive/src/main/kotlin/org/springframework/web/reactive/function/server/ServerRequestExtension.kt b/spring-web-reactive/src/main/kotlin/org/springframework/web/reactive/function/server/ServerRequestExtension.kt new file mode 100644 index 00000000000..8c4f9f3f212 --- /dev/null +++ b/spring-web-reactive/src/main/kotlin/org/springframework/web/reactive/function/server/ServerRequestExtension.kt @@ -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 ServerRequest.bodyToMono(type: KClass) = bodyToMono(type.java) + + /** + * @see ServerRequest.bodyToFlux + */ + fun ServerRequest.bodyToFlux(type: KClass) = bodyToFlux(type.java) +}