• 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

BeanUtils.populate not working with uppercase map properties

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am having a problem with populating a Java Bean( with 4 fields) contents from a map which has 4 key, value pairs while using apache BeanUtils.populate(bean,map) that when my map has a key with upper case string then my bean is not getting populated and i guess its because of Java reflection that BeanUtils.populate() uses and expectes the first letter of every map key should begin with a lower case letter and i am wondering if there is any fix for my problem.

Here is the sample code



And when i say



i get null. But when the map key starts with lower case then the value is copied from the map to bean using BeanUtils.populate().

How can i copy the map contents to my bean if the keys in the map start with upper case and i cant change that as my service is providing the xml in that format and i am converting the xml to HashMap and then using that to copy the contents to a bean.

Appreciate your suggestion and guidance.
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Change the property names to be correct. If they're not correct, it won't work. Simple as that.
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the problem is that the property name should be "firstName" but you're being given "FirstName" instead by some process which is difficult to change, then it isn't very hard to write a line of code which systematically changes the first character of a string to lower-case.
 
Pawan Kalyan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are correct Paul, i am getting an xml from a different service which always gives me properties beginning with upper case and i am converting the xml to a hashmap and then trying to copy it over a bean. I am not sure if i can format the xml response to change the first char to upper case as its involves some permissions so i was trying to see if there is any other way i can copy my map contents over a bean with my situation.
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No need to change the XML if that's hard, just adjust the case when you create the Map entries.
 
Pawan Kalyan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, i can do that but it has impact to other consumers of my service that they need to modify the way they can extract the map contents and all my consumers need to know that they have to use a key string starting with lower case when they are extracting contents from the map and that raised a question that its not the efficient way to manipulate the xml response by altering it case and storing it in a map so i was looking if there is any other way to override the BeanUtils.populate functionality and write my own class on top of it so that it will take care of all the case sensitivity issue and dont worry about altering the case of the map key string while extracting the xml response and save it in the map.

 
Paul Clapham
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. Let me try one more time.

You extract the name and the value from the XML. Then you modify the name (which is simply a Java string) by making its first character lower-case. There is no need to modify the XML at all.

And this map -- its only purpose is to populate the bean, right? You weren't keeping it around for other purposes, I hope? If you are, and you really need the upper-cased keys, then just make two copies of the map, one to make your bean and the other for those other purposes.

Really, you're making a big deal over nothing here.
 
Pawan Kalyan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul,

Sorry my scenario looks like a small deal but its different here. I am storing the map and its been used somewhere else too thats the reason i am not willing to format the case of the map keys and i cant use 2 maps as my service prodived might add/remove few variables in their response and its makes a lot of effort to change it in 2 places.

Anyways thanks for your inputs, i am looking at something else to overwrite Beanutils.populate and see if that works so that it can be a perfect solution.
 
Paul Clapham
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pawan Kalyan wrote:Anyways thanks for your inputs, i am looking at something else to overwrite Beanutils.populate and see if that works so that it can be a perfect solution.



That would be the temporary map with the correctly-cased keys which is only used to populate the bean.
 
Pawan Kalyan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, i am trying to write a custom class which will override BeanUtils.populate() and write my own login to take care of the Map to Bean copy so that no matter what will the case of the starting letter of the key in the map my populate() method will copy the keys/values to the bean so that i dont have to deal with duplicate or second map to handle the scenario.
 
Paul Clapham
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That sounds like a possibility too -- let us know how it works out!
 
reply
    Bookmark Topic Watch Topic
  • New Topic