Pihentagy
06/15/2023, 10:01 AMval params= mapOf("foo" to "bar")
, how can I pass it to an UriComponentBuilder
?Klitos Kyriacou
06/15/2023, 11:26 AMval uri = UriComponentsBuilder.newInstance()
.scheme("http")
.host("<http://example.org|example.org>")
.path("example/path")
.query("fn={firstName}")
.query("sn={surname}")
.buildAndExpand(mapOf("firstName" to "John", "surname" to "Smith"))
.toUri()
Pihentagy
06/15/2023, 11:31 AMfirstName
) to query parameters (fn
). Isn't there a one-stop solution, so that if I have mapOf("foo" to "bar", "ping" to "pong")
it would just become ?foo=bar&ping=pong
without further instruction?Klitos Kyriacou
06/15/2023, 11:54 AMval uriComponents1 = UriComponentsBuilder.newInstance()
.scheme("http")
.host("<http://example.org|example.org>")
.path("example/path")
.queryParams(CollectionUtils.toMultiValueMap(originalMap.mapValues { listOf(it.value) }))
.build()
val uri1 = uriComponents1.toUri()
println(uri1)
val uriComponents2 = UriComponentsBuilder.newInstance()
.scheme("http")
.host("<http://example.org|example.org>")
.path("example/path")
.apply {
originalMap.forEach { (k, v) -> queryParam(k, v) }
}
.build()
val uri2 = uriComponents2.toUri()
println(uri2)
Pihentagy
06/15/2023, 12:21 PM