In `spring boot` I'm trying to use `RestTemplate` ...
# getting-started
u
In
spring boot
I'm trying to use
RestTemplate
in tests to simply call requests and assert responses. There are kotlin extensions available for
getForObject
Copy code
@Throws(RestClientException::class)
inline fun <reified T : Any> TestRestTemplate.getForObject(url: String, vararg uriVariables: Any): T? =
		getForObject(url, T::class.java, *uriVariables)
However when I use it with a
List
it breaks
Copy code
val people = client.getForObject<List<Person>>("/people")
When I hook up debugger the
people
variable is of type
List
and then element is
LinkedHashMap
If then in debugger I evaluate
people.first()
I get a class cast exception Anyone know what's up?
e
that means it's a
List
containing something which isn't a
Person
. due to generic erasure that can't be checked until the item is retrieved
u
That makes sense but now what, I cannot use endpoints returning a list at all?
e
same as Java, there is another form which will preserve the generic type information which
Class<*>
does not retain
Copy code
exchange(url, GET, null, object : ParameterizedTypeReference<List<Person>>() {}, *uriVariables)
👍 1