Hi Team
It is general question, I asked in stackoverflow also but needed your input.
We are upgrading Spring 5.x to Spring 6.x. We have a use case where we have to populate some custom property during initbinder which used to work earlier but now not working anymore. Do we have a way to attach mutable properties in case binder target comes null ?
Something like this:
MutablePropertyValues map = new MutablePropertyValues(); // Any way to attach these in case target is null ??
map.addPropertyValue("Test", "123");
webDataBinder.bind(map);
Comment From: rstoyanchev
Please, provide more detail on what exactly used to work, or provide a minimal sample.
Comment From: Secret629
@rstoyanchev I am attaching sample project where if I change spring boot version to older in pom then target is not coming null and I can attach mutable properties while the same is not true with 6.x
Sample curl curl --location 'http://localhost:8082/api/pojo/field1' \
--form 'requestContent2="Hello"'
Comment From: rstoyanchev
This is similar to #35648, see my https://github.com/spring-projects/spring-framework/issues/35648#issuecomment-3422482659 on what changed, but unlike there, here you actually need the target instance.
That said, @InitBinder
methods are to initialize the WebDataBinder
instance, and we don't expect them to be used for data binding at that point, but I can see what you're after.
If you need to add more binding values, the main way to do that right now is to override RequestMappingHandlerAdapter#createDataBinderFactory
and return a subclass of ServletRequestDataBinderFactory
that creates a custom data binder that enriches the MutablePropertyValues
similar to what ServletRequestDataBinderFactory
does by adding URI variables as binding values.
That's a bit convoluted and decoupled from any knowledge of the target command object. We could consider exposing something directly on WebDataBinder
like a Consumer<MutablePropertyValues>
that would let you add binding values to be applied, and that would work for both constructor and setter binding.
What is the logic you use to decide what properties to add? The sample doesn't show that, it's just hardcoded.