BeanUtils with maps

How can I use the setProperty BeanUtils method using Maps.

For example, this method: public void setAddress (string type, address of the address); It can be set using: BeanUtils.setProperty (beanObject, address (home) ", addressObject);

But if the object I want to set is Map, is that possible? as?

+3
source share
1 answer

Use map syntax foo(bar)( baris the map key foo):

public static class Bean{
    private Map<String, String> data = new HashMap<String, String>();
    public Map<String, String> getData(){
        return data;
    }
    public void setData(final Map<String, String> data){
        this.data = data;
    }
}

public static void main(final String[] args) throws Exception{
    final Bean bean = new Bean();
    // assign the foo key of the data property to the value 'bar'
    BeanUtils.setProperty(bean, "data(foo)", "bar");
    System.out.println(bean.data);
}

Output:

{Foo = bar}

+2
source

Source: https://habr.com/ru/post/1716036/


All Articles