• 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 ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Databinding and Type conversion Problem

 
Ranch Hand
Posts: 208
  • 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 ?




 
Can you really tell me that we aren't dealing with suspicious baked goods? And then there is this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic