I am using Spring MVC to a build a RESTFul application with JSON. Form my application the JSON needs to be parsed into an object before being used by the controller. For example.
I know this can work fine using @RequestBody if field values map to the attributes in the Employee object
But how about if the Employee Form is composed of PersonalDetails, EducationalDetails, WorkExperience and i need to apply some complex parsing to pass three different Objects to the handler instead of one.
Since the data is passed in the form of JSON, i need to parse the JSON into the above mentioned three object and then pass to the handler.
There are two different approaches i have found.
1. Using @InitBinder with PropertyEditorSupport
2. Using the Converter - org.springframework.core.convert.converter.Converter
The @InitBinder approach provides a setAsText(String) method. Is the setAsText method going to recieve the entire JSON as an argument and i would have to write custom logic for parsing and binding the result to PersonalDetail, EducationDetail and WorkExperience objects and set in the binding result.
Similarly for Converter we have to implement convert(String source) method.
My question is what if there are 5 Post methods in a controller each requiring such binding. Do i need 5 converter classes, one for each type of conversion? i may have 20 controllers in my application , that would mean 100 additional converter files for the application. That doesn't sound encouraging.
Any strategies ?
I found a beautiful pie. And a tiny ad:
Devious Experiments for a Truly Passive Greenhouse!