|
| 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