sonofblip
07/11/2019, 3:35 PMapplication.routing {
get("myroute") {
val queryParams = call.parameters // the query params from the call
val paramsMap: Map<String, String> = queryParams.toMap().mapValues { it.value.first() } // I can put them in a map of string fairly easily
val instanceOfMyClass: MyClass = MyClass(paramsMap["val1"], paramsMap["val2"], etc...) // I have to map them to my class manually each time :(
// do stuff with the query params
call.respondJson("your response here.")
}
}
To be clear, the equivalent of what I’m looking for in spring would look like:
import javax.validation.Valid;
@RequestMapping(value = "/getItem", method = RequestMethod.GET)
public ServiceRequest<List<SomeModel>> getClaimStatuses(@Valid RequestParamsModel model) {
// ...
}
and in JAX-RS it’d use @BeanParam
https://dennis-xlc.gitbooks.io/restful-java-with-jax-rs-2-0-2rd-edition/en/part1/chapter5/bean_param.htmlDennis Schröder
07/11/2019, 3:37 PMDennis Schröder
07/11/2019, 3:40 PMval myDataClass = call.receive<MyDataClass>()
The feature you are looking for, when I get you right, is the Content Negotiation Feature
.
https://ktor.io/servers/features/content-negotiation.htmlsonofblip
07/11/2019, 4:04 PMsonofblip
07/11/2019, 4:05 PMCannotTransformContentToTypeException
. Even with all the values of the class’ constructor (including the nullable ones) passed in.tseisel
07/11/2019, 4:58 PMLocations
feature : https://ktor.io/servers/features/locations.htmlhalley
07/11/2019, 5:27 PMKevin Schmeichel
07/11/2019, 5:35 PMKevin Schmeichel
07/11/2019, 5:37 PMKevin Schmeichel
07/11/2019, 5:47 PMLocations
instead of Routing
sonofblip
07/11/2019, 5:49 PMLocations
feature worked like a charm! Only downside is that it doesn’t allow for multiple different models to be combined in one routesonofblip
07/11/2019, 5:50 PMKevin Schmeichel
07/11/2019, 5:57 PMLocations
removes the path from the routing definition, and you have to keep jumping back to the annotation to view the pathKevin Schmeichel
07/11/2019, 5:57 PMcall.receive<>()