This isn't specific to Kotlin but maybe someone kn...
# spring
s
This isn't specific to Kotlin but maybe someone knows. I cannot get spring to bind a query string to a nested object when using WebFlux. An example I threw together:
Copy code
@GetMapping("/api/users")
fun getAll(user: User) = Mono.just(user)
-- snip --

data class User(
    val firstName: String,
    val address: Address
)

data class Address(
    val street: String
)
Curl:
Copy code
curl '<http://localhost:8080/api/users?firstName=shawn&address.street=foo>'
500 error:
Copy code
org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.expediagroup.application.User]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: Parameter specified as non-null is null: method com.expediagroup.application.User.<init>, parameter address
I am pretty sure this works with the servlet API.
t
does not work with servlet either. however it works if you add a default value
Copy code
data class User(
    val firstName: String,
    val address: Address = Address("")
)
quickly tested with servlet, I think I had the same problem a couple of years back and resulted in not using nested objects for query params. but ofc in a couple years improvements might have come
s
Thanks @thanksforallthefish that is certainly ugly 😉 I hadn't actually tried it via the servlet API I was just trusting an article that claimed it worked, it was Java though. So maybe it is a kotlin issue with how objects are constructed when there is a nested user type ¯\_(ツ)_/¯ (or maybe it never actually worked)
w
I've run into that before. Spring MVC works by constucting the object, then injecting its values, but it works for flattened classes. So the same thing with jackson more or less works. I've had to go around this in the past using java like objects (default constructors and mutable properties) Which I've then transformed. I think it could be an enhancement request for the spring MVC.
An arguably better approach is here https://blog.trifork.com/2011/12/08/use-immutable-objects-in-your-spring-mvc-controller-by-implementing-your-own-webargumentresolver/ but its quite a bit of work and may or may not be worth it.
s
thanks @Wesley Acheson