|
| 1 | +/* |
| 2 | + * Copyright 2002-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.web.client |
| 18 | + |
| 19 | +import org.springframework.core.ParameterizedTypeReference |
| 20 | +import org.springframework.http.ResponseEntity |
| 21 | + |
| 22 | +/** |
| 23 | + * Extension for [RestClient.RequestBodySpec.body] providing a `bodyWithType<Foo>(...)` variant |
| 24 | + * leveraging Kotlin reified type parameters. This extension is not subject to type |
| 25 | + * erasure and retains actual generic type arguments. |
| 26 | + * |
| 27 | + * @author Sebastien Deleuze |
| 28 | + * @since 6.1 |
| 29 | + */ |
| 30 | +inline fun <reified T : Any> RestClient.RequestBodySpec.bodyWithType(body: T): RestClient.RequestBodySpec = |
| 31 | + body(body, object : ParameterizedTypeReference<T>() {}) |
| 32 | + |
| 33 | + |
| 34 | +/** |
| 35 | + * Extension for [RestClient.ResponseSpec.body] providing a `body<Foo>()` variant |
| 36 | + * leveraging Kotlin reified type parameters. This extension is not subject to type |
| 37 | + * erasure and retains actual generic type arguments. |
| 38 | + * |
| 39 | + * @author Sebastien Deleuze |
| 40 | + * @since 6.1 |
| 41 | + */ |
| 42 | +inline fun <reified T : Any> RestClient.ResponseSpec.body(): T? = |
| 43 | + body(object : ParameterizedTypeReference<T>() {}) |
| 44 | + |
| 45 | + |
| 46 | +/** |
| 47 | + * Extension for [RestClient.ResponseSpec.toEntity] providing a `toEntity<Foo>()` variant |
| 48 | + * leveraging Kotlin reified type parameters. This extension is not subject to type |
| 49 | + * erasure and retains actual generic type arguments. |
| 50 | + * |
| 51 | + * @author Sebastien Deleuze |
| 52 | + * @since 6.1 |
| 53 | + */ |
| 54 | +inline fun <reified T : Any> RestClient.ResponseSpec.toEntity(): ResponseEntity<T> = |
| 55 | + toEntity(object : ParameterizedTypeReference<T>() {}) |
0 commit comments