-
Notifications
You must be signed in to change notification settings - Fork 38.9k
Description
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).