Skip to content

Commit 546687d

Browse files
committed
Add Kotlin extension for RestTemplate
Issue: SPR-15056
1 parent 3626a1c commit 546687d

File tree

2 files changed

+117
-1
lines changed

2 files changed

+117
-1
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,7 @@ project("spring-web") {
791791
optional("javax.xml.bind:jaxb-api:${jaxbVersion}")
792792
optional("javax.xml.ws:jaxws-api:${jaxwsVersion}")
793793
optional("javax.mail:javax.mail-api:${javamailVersion}")
794+
optional("org.jetbrains.kotlin:kotlin-stdlib:${kotlinVersion}")
794795
testCompile(project(":spring-context-support")) // for JafMediaTypeFactory
795796
testCompile("io.projectreactor.addons:reactor-test:${reactorCoreVersion}")
796797
testCompile("org.apache.taglibs:taglibs-standard-jstlel:1.2.1") {
@@ -807,7 +808,6 @@ project("spring-web") {
807808
testCompile("org.xmlunit:xmlunit-matchers:${xmlunitVersion}")
808809
testCompile("org.slf4j:slf4j-jcl:${slf4jVersion}")
809810
testCompile("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
810-
testCompile("org.jetbrains.kotlin:kotlin-stdlib:${kotlinVersion}")
811811
testRuntime("com.sun.mail:javax.mail:${javamailVersion}")
812812
testRuntime("com.sun.xml.bind:jaxb-core:${jaxbVersion}")
813813
testRuntime("com.sun.xml.bind:jaxb-impl:${jaxbVersion}")
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package org.springframework.web.client
2+
3+
import org.springframework.http.HttpEntity
4+
import org.springframework.http.HttpMethod
5+
import org.springframework.http.RequestEntity
6+
import java.net.URI
7+
8+
/**
9+
* Extension for [RestOperations] providing [KClass] based API and avoiding specifying
10+
* a class parameter when possible thanks to Kotlin reified type parameters.
11+
*
12+
* @author Jon Schneider
13+
* @author Sebastien Deleuze
14+
* @since 5.0
15+
*/
16+
object RestOperationsExtension {
17+
18+
/**
19+
* @see RestOperations.getForObject
20+
*/
21+
@Throws(RestClientException::class)
22+
inline fun <reified T: Any> RestOperations.getForObject(url: String, vararg uriVariables: Any) =
23+
getForObject(url, T::class.java, *uriVariables)
24+
25+
/**
26+
* @see RestOperations.getForObject
27+
*/
28+
@Throws(RestClientException::class)
29+
inline fun <reified T: Any> RestOperations.getForObject(url: String, uriVariables: Map<String, Any?>) =
30+
getForObject(url, T::class.java, uriVariables)
31+
32+
/**
33+
* @see RestOperations.getForObject
34+
*/
35+
@Throws(RestClientException::class)
36+
inline fun <reified T: Any> RestOperations.getForObject(url: URI) =
37+
getForObject(url, T::class.java)
38+
39+
/**
40+
* @see RestOperations.getForEntity
41+
*/
42+
@Throws(RestClientException::class)
43+
inline fun <reified T: Any> RestOperations.getForEntity(url: String, vararg uriVariables: Any) =
44+
getForEntity(url, T::class.java, *uriVariables)
45+
46+
/**
47+
* @see RestOperations.postForObject
48+
*/
49+
@Throws(RestClientException::class)
50+
inline fun <reified T: Any> RestOperations.postForObject(url: String, request: Any, vararg uriVariables: Any) =
51+
postForObject(url, request, T::class.java, *uriVariables)
52+
53+
/**
54+
* @see RestOperations.postForObject
55+
*/
56+
@Throws(RestClientException::class)
57+
inline fun <reified T: Any> RestOperations.postForObject(url: String, request: Any, uriVariables: Map<String, *>) =
58+
postForObject(url, request, T::class.java, uriVariables)
59+
60+
/**
61+
* @see RestOperations.postForObject
62+
*/
63+
@Throws(RestClientException::class)
64+
inline fun <reified T: Any> RestOperations.postForObject(url: URI, request: Any) =
65+
postForObject(url, request, T::class.java)
66+
67+
/**
68+
* @see RestOperations.postForEntity
69+
*/
70+
@Throws(RestClientException::class)
71+
inline fun <reified T: Any> RestOperations.postForEntity(url: String, request: Any, vararg uriVariables: Any) =
72+
postForEntity(url, request, T::class.java, *uriVariables)
73+
74+
/**
75+
* @see RestOperations.postForEntity
76+
*/
77+
@Throws(RestClientException::class)
78+
inline fun <reified T: Any> RestOperations.postForEntity(url: String, request: Any, uriVariables: Map<String, *>) =
79+
postForEntity(url, request, T::class.java, uriVariables)
80+
81+
/**
82+
* @see RestOperations.postForEntity
83+
*/
84+
@Throws(RestClientException::class)
85+
inline fun <reified T: Any> RestOperations.postForEntity(url: URI, request: Any) =
86+
postForEntity(url, request, T::class.java)
87+
88+
/**
89+
* @see RestOperations.exchange
90+
*/
91+
@Throws(RestClientException::class)
92+
inline fun <reified T: Any> RestOperations.exchange(url: String, method: HttpMethod, requestEntity: HttpEntity<*>, vararg uriVariables: Any) =
93+
exchange(url, method, requestEntity, T::class.java, *uriVariables)
94+
95+
/**
96+
* @see RestOperations.exchange
97+
*/
98+
@Throws(RestClientException::class)
99+
inline fun <reified T: Any> RestOperations.exchange(url: String, method: HttpMethod, requestEntity: HttpEntity<*>, uriVariables: Map<String, *>) =
100+
exchange(url, method, requestEntity, T::class.java, uriVariables)
101+
102+
/**
103+
* @see RestOperations.exchange
104+
*/
105+
@Throws(RestClientException::class)
106+
inline fun <reified T: Any> RestOperations.exchange(url: URI, method: HttpMethod, requestEntity: HttpEntity<*>) =
107+
exchange(url, method, requestEntity, T::class.java)
108+
109+
/**
110+
* @see RestOperations.exchange
111+
*/
112+
@Throws(RestClientException::class)
113+
inline fun <reified T: Any> RestOperations.exchange(requestEntity: RequestEntity<*>) =
114+
exchange(requestEntity, T::class.java)
115+
116+
}

0 commit comments

Comments
 (0)