diff --git a/build.gradle b/build.gradle index c9e57f28bc10..69bf7ede5fbe 100644 --- a/build.gradle +++ b/build.gradle @@ -791,6 +791,7 @@ project("spring-web") { optional("javax.xml.bind:jaxb-api:${jaxbVersion}") optional("javax.xml.ws:jaxws-api:${jaxwsVersion}") optional("javax.mail:javax.mail-api:${javamailVersion}") + optional("org.jetbrains.kotlin:kotlin-stdlib:${kotlinVersion}") testCompile(project(":spring-context-support")) // for JafMediaTypeFactory testCompile("io.projectreactor.addons:reactor-test:${reactorCoreVersion}") testCompile("org.apache.taglibs:taglibs-standard-jstlel:1.2.1") { @@ -807,7 +808,6 @@ project("spring-web") { 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}") testRuntime("com.sun.mail:javax.mail:${javamailVersion}") testRuntime("com.sun.xml.bind:jaxb-core:${jaxbVersion}") testRuntime("com.sun.xml.bind:jaxb-impl:${jaxbVersion}") diff --git a/spring-web/src/main/kotlin/org/springframework/web/client/RestOperationsExtension.kt b/spring-web/src/main/kotlin/org/springframework/web/client/RestOperationsExtension.kt new file mode 100644 index 000000000000..e5f49165f20f --- /dev/null +++ b/spring-web/src/main/kotlin/org/springframework/web/client/RestOperationsExtension.kt @@ -0,0 +1,116 @@ +package org.springframework.web.client + +import org.springframework.http.HttpEntity +import org.springframework.http.HttpMethod +import org.springframework.http.RequestEntity +import java.net.URI + +/** + * Extension for [RestOperations] providing [KClass] based API and avoiding specifying + * a class parameter when possible thanks to Kotlin reified type parameters. + * + * @author Jon Schneider + * @author Sebastien Deleuze + * @since 5.0 + */ +object RestOperationsExtension { + + /** + * @see RestOperations.getForObject + */ + @Throws(RestClientException::class) + inline fun RestOperations.getForObject(url: String, vararg uriVariables: Any) = + getForObject(url, T::class.java, *uriVariables) + + /** + * @see RestOperations.getForObject + */ + @Throws(RestClientException::class) + inline fun RestOperations.getForObject(url: String, uriVariables: Map) = + getForObject(url, T::class.java, uriVariables) + + /** + * @see RestOperations.getForObject + */ + @Throws(RestClientException::class) + inline fun RestOperations.getForObject(url: URI) = + getForObject(url, T::class.java) + + /** + * @see RestOperations.getForEntity + */ + @Throws(RestClientException::class) + inline fun RestOperations.getForEntity(url: String, vararg uriVariables: Any) = + getForEntity(url, T::class.java, *uriVariables) + + /** + * @see RestOperations.postForObject + */ + @Throws(RestClientException::class) + inline fun RestOperations.postForObject(url: String, request: Any, vararg uriVariables: Any) = + postForObject(url, request, T::class.java, *uriVariables) + + /** + * @see RestOperations.postForObject + */ + @Throws(RestClientException::class) + inline fun RestOperations.postForObject(url: String, request: Any, uriVariables: Map) = + postForObject(url, request, T::class.java, uriVariables) + + /** + * @see RestOperations.postForObject + */ + @Throws(RestClientException::class) + inline fun RestOperations.postForObject(url: URI, request: Any) = + postForObject(url, request, T::class.java) + + /** + * @see RestOperations.postForEntity + */ + @Throws(RestClientException::class) + inline fun RestOperations.postForEntity(url: String, request: Any, vararg uriVariables: Any) = + postForEntity(url, request, T::class.java, *uriVariables) + + /** + * @see RestOperations.postForEntity + */ + @Throws(RestClientException::class) + inline fun RestOperations.postForEntity(url: String, request: Any, uriVariables: Map) = + postForEntity(url, request, T::class.java, uriVariables) + + /** + * @see RestOperations.postForEntity + */ + @Throws(RestClientException::class) + inline fun RestOperations.postForEntity(url: URI, request: Any) = + postForEntity(url, request, T::class.java) + + /** + * @see RestOperations.exchange + */ + @Throws(RestClientException::class) + inline fun RestOperations.exchange(url: String, method: HttpMethod, requestEntity: HttpEntity<*>, vararg uriVariables: Any) = + exchange(url, method, requestEntity, T::class.java, *uriVariables) + + /** + * @see RestOperations.exchange + */ + @Throws(RestClientException::class) + inline fun RestOperations.exchange(url: String, method: HttpMethod, requestEntity: HttpEntity<*>, uriVariables: Map) = + exchange(url, method, requestEntity, T::class.java, uriVariables) + + /** + * @see RestOperations.exchange + */ + @Throws(RestClientException::class) + inline fun RestOperations.exchange(url: URI, method: HttpMethod, requestEntity: HttpEntity<*>) = + exchange(url, method, requestEntity, T::class.java) + + /** + * @see RestOperations.exchange + */ + @Throws(RestClientException::class) + inline fun RestOperations.exchange(requestEntity: RequestEntity<*>) = + exchange(requestEntity, T::class.java) + +}