Browse Source

Add Kotlin extensions for function Web API

Issue: SPR-15054
pull/1276/merge
Sebastien Deleuze 8 years ago
parent
commit
3626a1c7f9
  1. 4
      build.gradle
  2. 39
      spring-web-reactive/src/main/kotlin/org/springframework/web/reactive/function/BodyExtractorsExtension.kt
  3. 26
      spring-web-reactive/src/main/kotlin/org/springframework/web/reactive/function/BodyInsertersExtension.kt
  4. 23
      spring-web-reactive/src/main/kotlin/org/springframework/web/reactive/function/client/ClientResponseExtension.kt
  5. 21
      spring-web-reactive/src/main/kotlin/org/springframework/web/reactive/function/server/ServerRequestExtension.kt

4
build.gradle

@ -858,6 +858,8 @@ project("spring-web-reactive") { @@ -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") { @@ -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")

39
spring-web-reactive/src/main/kotlin/org/springframework/web/reactive/function/BodyExtractorsExtension.kt

@ -0,0 +1,39 @@ @@ -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)
}

26
spring-web-reactive/src/main/kotlin/org/springframework/web/reactive/function/BodyInsertersExtension.kt

@ -0,0 +1,26 @@ @@ -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)
}

23
spring-web-reactive/src/main/kotlin/org/springframework/web/reactive/function/client/ClientResponseExtension.kt

@ -0,0 +1,23 @@ @@ -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)
}

21
spring-web-reactive/src/main/kotlin/org/springframework/web/reactive/function/server/ServerRequestExtension.kt

@ -0,0 +1,21 @@ @@ -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…
Cancel
Save