• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Databinding and Type conversion Problem

 
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

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!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic