When using regular @Controller
for both the Servlet as well as the Reactive stack there is support for data binding using @ModelAttribute
and there is support for deserializing a request body with @RequestBody
.
When using the functional approach in either the Servlet or Reactive stack deserializing is supported through the body
methods of the respective ServerRequest
interface/implementation. There is no direct support for data-binding. It currently requires, some work, to do data-binding in a method.
public ServerResponse handle(ServerRequest request) {
MyObject instance = new MyObject();
ExtendedServletRequestDataBinder binder = new ExtendedServletRequestDataBinder(instance, "myObject");
binder.bind(request.servletRequest());
return ServerResponse.ok().render("view", instance).build();.
}
Although this will work it requires more knowledge of the internal of data binding then one should need.
It would be nice if there would be a supporting bind
method(s) to support binding to a type like
<T> T bind(Class<?> bindType);
<T> T bind(Class<?> bindType, String objectName);
Which would reduce the code to something like this
public ServerResponse handle(ServerRequest request) {
MyObject instance =request.bind(MyObject.class, "myObject");
return ServerResponse.ok().render("view", instance).build();.
}
This could use the already available WebDataBinder
implementations to do the binding (at least the reactive stack has a special DataBinder
the functional servlet one needs to be created).
Comment From: rstoyanchev
Indeed, there is no binding in neither WebFlux.fn nor WebMvc.fn currently.
Comment From: poutsma
This turned out to be more complicated than I initially considered, but it will make it into 5.3.3 I hope.
Comment From: poutsma
In order for the functional endpoints to support constructor binding, we will need to refactor some code out of the model attribute argument resolvers. See #26721.
Comment From: poutsma
With #26721 resolved, we should be able to get this enhancement in 6.1.