将DTO转换为参数列表

时间:2018-08-30 05:30:28

标签: java

public class CreateCustomerRequest {
    private String fName;
    private String lName;
    private String gender;
    private String address1;
    private String address2;
    private String mobileNo;
}

我上面有请求DTO。得到此请求后我需要什么,然后我需要convert this DTO with List<String> fields; List<String> objects包含{fName, lName, gender, address1, address2, mobileNo}

的地方

请提出我将如何实现这一目标。一无所知。

2 个答案:

答案 0 :(得分:3)

基于反思,如果使字段公开,则下面的代码起作用,如果要将其设为私有而不是设置字段,则可以通过构造函数对其进行转换。

public class CreateCustomerRequest {
    public String fName;
    public String lName;
    public String gender;
    public String address1;
    public String address2;
    public String mobileNo;
}


public static void main(String[] args) throws Throwable {
    List<String> param = new ArrayList<String>();
    param.add("fnameValue");
    param.add("lNameValue");
    param.add("genderValue");
    param.add("address1Value");
    param.add("address2Value");
    param.add("mobileNoValue");
    CreateCustomerRequest object = convert(param, CreateCustomerRequest.class);
    System.out.println("Gender : " + object.gender);
}

public static <T extends Object> T convert(List<String> param, Class<T> clazz) throws Throwable
{
    Constructor<?> ctor = clazz.getConstructor();
    T result = (T) ctor.newInstance();
    Field[] fields = result.getClass().getFields();
    if(fields == null)
        return null;
    for(int i =0;i<fields.length;i++)
    {
        if(fields[i].getType().equals(String.class))
        {
            if(param.size() > i)
                fields[i].set(result, param.get(i));
        }
    }       
    return result;
}

干杯。

编辑

为使DTO类字段私有并通过getter方法读取它们,我编辑了答案以支持它。

更改:

  1. 将result.getClass()。getFields()---> result.getClass()。getDeclaredFields()转换为能够读取私有字段。
  2. 在将值设置为field之前调用field [i] .setAccessible(true)。
  3. 通过getter读取字段值-> System.out.println(“ Gender:” + object.getGender());

所以DTO类的新代码是:

public class CreateCustomerRequest {
    private String fName;
    private String lName;
    private String gender;
    private String address1;
    private String address2;
    private String mobileNo;

    public String getfName() {
        return fName;
    }
    public void setfName(String fName) {
        this.fName = fName;
    }
    public String getlName() {
        return lName;
    }
    public void setlName(String lName) {
        this.lName = lName;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public String getAddress1() {
        return address1;
    }
    public void setAddress1(String address1) {
        this.address1 = address1;
    }
    public String getAddress2() {
        return address2;
    }
    public void setAddress2(String address2) {
        this.address2 = address2;
    }
    public String getMobileNo() {
        return mobileNo;
    }
    public void setMobileNo(String mobileNo) {
        this.mobileNo = mobileNo;
    }
}

,转换代码为:

public static void main(String[] args) throws Throwable {
    List<String> param = new ArrayList<String>();
    param.add("fnameValue");
    param.add("lNameValue");
    param.add("genderValue");
    param.add("address1Value");
    param.add("address2Value");
    param.add("mobileNoValue");
    CreateCustomerRequest object = convert(param, CreateCustomerRequest.class);
    System.out.println("Gender : " + object.getGender());
}

public static <T extends Object> T convert(List<String> param, Class<T> clazz) throws Throwable
{
    Constructor<?> ctor = clazz.getConstructor();
    T result = (T) ctor.newInstance();
    Field[] fields = result.getClass().getDeclaredFields();
    if(fields == null)
        return null;

    for(int i =0;i<fields.length;i++)
    {           
        if(fields[i].getType().equals(String.class))
        {
            if(param.size() > i)
            {
                fields[i].setAccessible(true);
                fields[i].set(result, param.get(i));
            }
        }
    }       
    return result;
}

答案 1 :(得分:2)

假设您有一个对象,并且想要获取该对象的类的字段,则可以通过以下方式使用反射来获取这些字段的列表,

Field[] fields = createCustomerRequestObject.getClass().getDeclaredFields();
//OR
//Field[] fields = CreateCustomerRequest.class.getDeclaredFields();
List<String> fieldNames = new ArrayList<>();
for (Field field : fields) {
    fieldNames.add(field.getName());
}
System.out.println(fieldNames);

输出

[fName, lName, gender, address1, address2, mobileNo]