Skip to content

Commit 3626a1c

Browse files
committed
Add Kotlin extensions for function Web API
Issue: SPR-15054
1 parent bb94ba6 commit 3626a1c

File tree

5 files changed

+111
-2
lines changed

5 files changed

+111
-2
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,8 @@ project("spring-web-reactive") {
858858
optional("io.undertow:undertow-websockets-jsr:${undertowVersion}") {
859859
exclude group: "org.jboss.spec.javax.websocket", module: "jboss-websocket-api_1.1_spec"
860860
}
861+
optional("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
862+
optional("org.jetbrains.kotlin:kotlin-stdlib:${kotlinVersion}")
861863
testCompile("io.projectreactor.addons:reactor-test:${reactorCoreVersion}")
862864
testCompile("javax.validation:validation-api:${beanvalVersion}")
863865
testCompile("org.hibernate:hibernate-validator:${hibval5Version}")
@@ -871,8 +873,6 @@ project("spring-web-reactive") {
871873
testCompile("com.fasterxml:aalto-xml:1.0.0")
872874
testCompile("org.xmlunit:xmlunit-matchers:${xmlunitVersion}")
873875
testCompile("org.slf4j:slf4j-jcl:${slf4jVersion}")
874-
testCompile("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
875-
testCompile("org.jetbrains.kotlin:kotlin-stdlib:${kotlinVersion}")
876876
testCompile("com.squareup.okhttp3:mockwebserver:${okhttp3Version}")
877877
testRuntime("javax.el:javax.el-api:${elApiVersion}")
878878
testRuntime("org.glassfish:javax.el:3.0.1-b08")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.springframework.web.reactive.function
2+
3+
import org.springframework.http.ReactiveHttpInputMessage
4+
import reactor.core.publisher.Flux
5+
import reactor.core.publisher.Mono
6+
import kotlin.reflect.KClass
7+
8+
/**
9+
* Extension for [BodyExtactors] providing [KClass] based API and avoiding specifying
10+
* a class parameter when possible thanks to Kotlin reified type parameters.
11+
*
12+
* @since 5.0
13+
*/
14+
object BodyExtractorsExtension {
15+
16+
/**
17+
* @see BodyExtactors.toMono
18+
*/
19+
inline fun <reified T : Any> toMono() : BodyExtractor<Mono<T>, ReactiveHttpInputMessage> =
20+
BodyExtractors.toMono(T::class.java)
21+
22+
/**
23+
* @see BodyExtactors.toMono
24+
*/
25+
fun <T : Any> toMono(elementClass: KClass<T>) : BodyExtractor<Mono<T>, ReactiveHttpInputMessage> =
26+
BodyExtractors.toMono(elementClass.java)
27+
28+
/**
29+
* @see BodyExtactors.toFlux
30+
*/
31+
inline fun <reified T : Any> toFlux() : BodyExtractor<Flux<T>, ReactiveHttpInputMessage> =
32+
BodyExtractors.toFlux(T::class.java)
33+
34+
/**
35+
* @see BodyExtactors.toFlux
36+
*/
37+
fun <T : Any> toFlux(elementClass: KClass<T>) : BodyExtractor<Flux<T>, ReactiveHttpInputMessage> =
38+
BodyExtractors.toFlux(elementClass.java)
39+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.springframework.web.reactive.function
2+
3+
import org.reactivestreams.Publisher
4+
import org.springframework.http.ReactiveHttpOutputMessage
5+
import org.springframework.http.server.reactive.ServerHttpResponse
6+
7+
/**
8+
* Extension for [BodyInserters] providing [KClass] based API and avoiding specifying
9+
* a class parameter when possible thanks to Kotlin reified type parameters.
10+
*
11+
* @since 5.0
12+
*/
13+
object BodyInsertersExtension {
14+
15+
/**
16+
* @see BodyInserters.fromPublisher
17+
*/
18+
inline fun <reified T : Publisher<S>, reified S : Any> fromPublisher(publisher: T) : BodyInserter<T, ReactiveHttpOutputMessage> =
19+
BodyInserters.fromPublisher(publisher, S::class.java)
20+
21+
/**
22+
* @see BodyInserters.fromServerSentEvents
23+
*/
24+
inline fun <reified T : Publisher<S>, reified S : Any> fromServerSentEvents(publisher: T) : BodyInserter<T, ServerHttpResponse> =
25+
BodyInserters.fromServerSentEvents(publisher, S::class.java)
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.springframework.web.reactive.function.client
2+
3+
import reactor.core.publisher.Flux
4+
import reactor.core.publisher.Mono
5+
import kotlin.reflect.KClass
6+
7+
/**
8+
* Extension for [ClientResponse] providing [KClass] based API.
9+
*
10+
* @since 5.0
11+
*/
12+
object ClientResponseExtension {
13+
14+
/**
15+
* @see ClientResponse.bodyToFlux
16+
*/
17+
fun <T : Any> ClientResponse.bodyToFlux(type: KClass<T>) : Flux<T> = bodyToFlux(type.java)
18+
19+
/**
20+
* @see ClientResponse.bodyToMono
21+
*/
22+
fun <T : Any> ClientResponse.bodyToMono(type: KClass<T>) : Mono<T> = bodyToMono(type.java)
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.springframework.web.reactive.function.server
2+
3+
import kotlin.reflect.KClass
4+
5+
/**
6+
* Extension for [ServerRequest] providing [KClass] based API.
7+
*
8+
* @since 5.0
9+
*/
10+
object ServerRequestExtension {
11+
12+
/**
13+
* @see ServerRequest.bodyToMono
14+
*/
15+
fun <T : Any> ServerRequest.bodyToMono(type: KClass<T>) = bodyToMono(type.java)
16+
17+
/**
18+
* @see ServerRequest.bodyToFlux
19+
*/
20+
fun <T : Any> ServerRequest.bodyToFlux(type: KClass<T>) = bodyToFlux(type.java)
21+
}

0 commit comments

Comments
 (0)