How can I serialise a javabean to be loaded by Spring's DataBinder?


How can I serialise a javabean to be loaded by Spring's DataBinder?



I need to serialise a javabean (with arbitrary nested beans) to plain text, and then later restore as an Object graph.

For the purposes of this question I am not interested in XML, JSON, etc. but rather name/value pairs.

The Spring DataBinder is great for turning a list of name/value pairs into a real Object. For example we can supply:

values.addPropertyValue("id", "6789"); values.addPropertyValue("nestedBean[0].systemId", "FOO"); values.addPropertyValue("nestedBean[1].systemId", "BAR"); 

And use the DataBinder to produce a top level bean (with property id) containing a List of two nested beans.

My question is, how can I easily serialise a bean into the format that DataBinder expects?

I would expect something of the form:

Map<String,String> reduceBeanToNameValuePairs(Object bean) 

but have had no luck finding anything of the sort.




What is Spring's Minimum Dependencies for Dependency Injection?

1:



when using @transactional do i need to use jpatemplate/hibernatetemplate?
You could have a look at Commons BeanUtils.


How to detect unused properties in Spring
Its BeanMap class provides an entrySet method for accessing the wrapped bean's properties as entries of a map..
Integrating grails into an existing spring application?
This functionality is already sufficient for "flat" beans and would probably provide a good starting point for adding the necessary recursion for nested bean graphs..
Can you use Future/Futuretask objects with Spring TaskExecutors?


Ouput XML with Freemarker


94 out of 100 based on 84 user ratings 934 reviews