Hey, I've got a question about the new Spring Rout...
# spring
c
Hey, I've got a question about the new Spring Router DSL. I am migrating an application from the traditional Mappings and struggling on a POST from a web form containing an array for a value, let's say like this:
Copy code
<form name="users" action="POST">
<input type="text" name="someInfo">
<input type="text" name="users[name][0]">
<input type="text" name="users[surname][0]">
<input type="text" name="users[name][1]">
<input type="text" name="users[surname][1]">
</form>

data class InputForm(val someInfo: String, val users: Map<String, List<String>)
which could be wired like this:
Copy code
@PostMapping("/users")
fun updateUsers(inputForm: InputForm) {
  // access input form data like "someInfo" and the map "user"
}
With the migration I have something like:
Copy code
router {
  POST("/users", userController::updateUsers)
}

fun updateUsers(request: ServerRequest): ServerResponse {
  val inputForm = request.body<InputForm>()
  // do some stuff
  return ServerResponse.ok().render("success.html")
}
This is causing the following exception:
org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
What is the correct way to read the message in an automated way like it was possible with the traditional mapping? Or do I need to write a custom HttpMessageConverter?
m
If you are retrieving data from form, then you want to use
request.formData()
instead of
request.body()
.
c
That would be nice, but there is no such function.
Seems that this is something that WebFlux has inside its
DefaultServerRequest
but WebMVC not.
s
Try
request.params()
c
I already tried that, but with
val params: MultiValueMap<String, String> = request.params()
the form data is not converted into the target class. I could write a workaround for parsing the plain parameters to a map, but that was supported out of with the common, non-functional style.
s
Ok let me check
I have asked a feedback from Arjen Poutsma who developed that feature, I will forward you his feedback.
c
Thank you very much for your support!
s
So we are going to improve the documentation to make the
request.params()
more discoverable. For your need, I think what you want is data binding which is bot supported on WebFlux and WebMvc functional API. Feel free to create an issue if you care.