[英]How to copy values of all attributes from one instance of an object to another if they are not null or empty
Given a class Person:给定一个 Person 类:
String name;
String surname;
String id;
String address;
I have an object obj1
with the following values:我有一个具有以下值的对象
obj1
:
name="Name"
surname=null
id="ABC123"
address="Here"
Through an api call I get the following json:通过 api 调用,我得到以下 json:
{
"name":"John",
"surname":"Doe",
"id":"A1B2C3"
}
which gets mapped into an object obj2
like this:它被映射到一个对象
obj2
如下所示:
name="John"
surname="Doe"
id="A1B2C3"
address=null
I want to copy all non-null (or empty string) values of obj2
into obj1
so the final result is this:我想将
obj2
所有非空(或空字符串)值复制到obj1
因此最终结果是这样的:
name="John"
surname="Doe"
id="A1B2C3"
address="Here"
I have two problems.我有两个问题。 The first one is that I don't want to have to manually type the get/set call for each attribute of the object.
第一个是我不想为对象的每个属性手动键入 get/set 调用。 The second problem, is that I want the method to work for any type of object with no or minimal changes.
第二个问题是,我希望该方法适用于任何类型的对象,而无需更改或更改最少。
At the very least, I need the first problem solved.至少,我需要解决第一个问题。 The second one is optional, but would be great to learn a solution too.
第二个是可选的,但也可以学习解决方案。
You can use reflection to get all the instance fields and use Field#set
to copy over non-null values.您可以使用反射来获取所有实例字段并使用
Field#set
来复制非空值。
try {
for (Field field : Person.class.getDeclaredFields()) {
if (!Modifier.isStatic(field.getModifiers())) {
field.setAccessible(true);
Object val = field.get(obj2);
if (val != null) {
field.set(obj1, val);
}
}
}
System.out.println(obj1);
} catch (IllegalAccessException e) {
// Handle exception
}
public static void main(String[] args) {
MyClass obj1 = new MyClass(1,"1");
MyClass obj2 = new MyClass(2,"2");
System.out.println(obj2);
copy(obj1, obj2);
System.out.println(obj2);
}
public static MyClass copy(MyClass obj1, MyClass obj2){
Arrays.stream(obj1.getClass().getDeclaredFields()).forEach(f -> {
try {
f.set(obj2, f.get(obj1));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
});
return obj2;
}
static class MyClass {
int myInt;
String myString;
String emptyString = null;
public MyClass(int myInt, String myString) {
this.myInt = myInt;
this.myString = myString;
}
@Override
public String toString() {
return "MyClass{" +
"myInt=" + myInt +
", myString='" + myString + '\'' +
", emptyString='" + emptyString + '\'' +
'}';
}
}
in case you have private fields you can use如果您有可以使用的私有字段
f.setAccessible(true);
but I don't suggest that.但我不建议这样做。
Reading your question, seems like, you have two classes, in two distinct projects, which, exchange information in json.阅读您的问题,似乎您在两个不同的项目中有两个类,它们在 json 中交换信息。
So, It is easy if you can use Jackson(default in spring)所以,如果你可以使用杰克逊很容易(春季默认)
You could annotate the target class with the annotation of jackson @JsonInclude(JsonInclude.Include.NON_NULL)
您可以使用 jackson
@JsonInclude(JsonInclude.Include.NON_NULL)
的注释来注释目标类
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.