如何将BeanUtils转换器用于List?

时间:2016-08-23 02:07:18

标签: java apache-commons-beanutils

这里,请求Param字符串是:

  

的firstName = JACK&放大器; lastName的=百合和放大器;性别= 1&安培;食品=牛排和放大器;食品=披萨&放大器;报价=输入+您+收藏+报价和放大器;!教育= Jr.High和放大器; TOFD =日

Mapped类是:

public class Student {
    private String firstName;
    private String lastName;
    private Integer gender;
    private List<String> foods;
    private String quote;
    private String education;
    private String tOfD;

    getXxx()....;
    setXxx()....;
}

现在,我想写一个通用的util类来将字符串转换为bean。

public final class InjectUtil<T> {
private static final Logger LOGGER = LoggerFactory.getLogger(InjectUtil.class);

public static <T> T converter2Obj(String source, Class<T> tClass) {
    T t = null;
    try {
        t = tClass.newInstance();

        if(source != null && source.length() > 0) {
            String[] fields = source.split("&");
            for(String field : fields) {
                String[] fieldKeyValue = field.split("\\=");
                String fieldKey = fieldKeyValue[0];
                String fieldValue = fieldKeyValue[1];
                // help
            }
        }
    } catch(InstantiationException | IllegalAccessException | InvocationTargetException e) {
        e.printStackTrace();
    }
    return t;
}

}

请注意帮助,我如何使用BeanUtils转换器将“foods = Steak&amp; foods = Pizza”添加到List属性中。

2 个答案:

答案 0 :(得分:0)

您可以使用如下方法,您需要填充foodList(值为[Steak,Pizza]),然后使用以下方法设置为bean。

PropertyUtils.setProperty(studentBean, "foods", foodList);

BeanUtils.setProperty(studentBean, "foods", foodList);

答案 1 :(得分:0)

这是一种方法:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;

public final class InjectUtil<T> {
    // private static final Logger LOGGER =
    // LoggerFactory.getLogger(InjectUtil.class);

    public static <T> T converter2Obj(String source, Class<T> tClass) {
        T t = null;

        try {
            t = tClass.newInstance();
            Map<String, Object> params = new HashMap<String, Object>();
            if (source != null && source.length() > 0) {
                String[] fields = source.split("&");
                for (String field : fields) {
                    String[] fieldKeyValue = field.split("\\=");
                    String fieldKey = fieldKeyValue[0];
                    String fieldValue = fieldKeyValue[1];

                    if (params.containsKey(fieldKey)) {

                        //the key does not exist as yet
                        Object keyValueRetrieved = params.get(fieldKey);
                        if (keyValueRetrieved instanceof String) {
                            //key exists , it is single value that has been added now.
                            //change that now to a list to add more values
                            ArrayList<String> values = new ArrayList<String>();
                            values.add(keyValueRetrieved.toString());
                            values.add(fieldValue);
                            params.put(fieldKey, values);
                        } else {
                            //key exists , it is a list already. Add more values
                            ((ArrayList<String>) keyValueRetrieved).add(fieldValue);
                        }
                    } else {
                        params.put(fieldKey, fieldValue);
                    }

                }
            }
            BeanUtils.populate(t, params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("--------------------------------------------");
        System.out.println(t.toString());
        System.out.println("--------------------------------------------");
        return t;
    }
}

您可以进行优化,希望这会给您一个想法