Unirest kotlin receive array object

杨华杰
Jun 9, 2022

Code 1

val response = Unirest.get("http://localhost:8080/person").asObject(Array<Person>::class.java)
println(response.status)
response.body.forEach { println(it) }

Code 2

val response = Unirest.get("http://localhost:8080/person").asObject(object : GenericType<List<Person>>(){})
println(response.status)
response.body.forEach { println(it) }

Controller sample

@RestController
@RequestMapping("/person")
class PersonController {
@GetMapping
fun list() = listOf(Person(1, "jie jie"), Person(2, "huihui"))
}

data class Person(val id: Int, val name: String)

Controller Response

[{"id":1,"name":"jiejie"},{"id":2,"name":"huihui"}]

--

--