wood burning stoves*
The moose likes Other Open Source Projects and the fly likes BeanUtils.populate not working with uppercase map properties Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "BeanUtils.populate not working with uppercase map properties" Watch "BeanUtils.populate not working with uppercase map properties" New topic
Author

BeanUtils.populate not working with uppercase map properties

Pawan Kalyan
Ranch Hand

Joined: Jun 18, 2009
Posts: 34
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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61106
    
  66

Change the property names to be correct. If they're not correct, it won't work. Simple as that.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Jun 18, 2009
Posts: 34
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61106
    
  66

No need to change the XML if that's hard, just adjust the case when you create the Map entries.
Pawan Kalyan
Ranch Hand

Joined: Jun 18, 2009
Posts: 34
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Jun 18, 2009
Posts: 34
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Jun 18, 2009
Posts: 34
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

That sounds like a possibility too -- let us know how it works out!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: BeanUtils.populate not working with uppercase map properties