This week's book giveaway is in the Spring forum.
We're giving away four copies of Java Persistence with Spring Data and Hibernate and have Cătălin Tudose on-line!
See this thread for details.
Win a copy of Java Persistence with Spring Data and Hibernate this week in the Spring forum!
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris

Databinding and Type conversion Problem

Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?

No matter. Try again. Fail again. Fail better. This time, do it with this tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
    Bookmark Topic Watch Topic
  • New Topic