How to define ignore properties for nested properties to spring BeanUtils?
BeanUtils.copyProperties(source, target, ignorePropertiesName);
I have class Contact with nested class Name and their two propeties 'firstName' and 'lastname'.
I tried three different patterns to pass the nested filed name to ignore list, but none of them worked.
"contact.name.lastName"
"name.lastName"
"lastName"
Below is the complete unit test with class definition, I am using spring-beans-4.3.9.RELEASE version with java 8.
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.BeanUtils;
public class NestedCopyPropertiesTest {
// @Test
public void CopyProperties() {
String firstName = "Andy";
String lastName = "Murray";
Contact source = new Contact(new Name(firstName, lastName));
Contact target = new Contact(new Name(null, null));
BeanUtils.copyProperties(source, target);
String targetFirstName = target.getName().getFirstName();
String targetLastName = target.getName().getLastName();
log("targetFirstName: " + targetFirstName);
log("targetLastName: " + targetLastName);
Assert.assertTrue("Failed to copy nested properties.", firstName.equals(targetFirstName));
Assert.assertTrue("Failed to copy nested properties.", lastName.equals(targetLastName));
}
@Test
public void CopyPropertiesWithIgnoreProperties() {
String firstName = "Andy";
String lastName = "Murray";
Contact source = new Contact(new Name(firstName, lastName));
Contact target = new Contact(new Name(null, null));
// -------------NOT WORKING?
// BeanUtils.copyProperties(source, target, "contact.name.lastName");
// -------------NOT WORKING?
// BeanUtils.copyProperties(source, target, "name.lastName");
// -------------NOT WORKING?
BeanUtils.copyProperties(source, target, "lastName");
String targetFirstName = target.getName().getFirstName();
String targetLastName = target.getName().getLastName();
log("targetFirstName: " + targetFirstName);
log("targetLastName: " + targetLastName);
Assert.assertTrue("Failed to copy nested properties.", firstName.equals(targetFirstName));
Assert.assertTrue("Failed to copy nested properties with ignore property.", !lastName.equals(targetLastName));
}
public void log(String string) {
System.out.println(string);
}
public class Contact {
private Name name;
public Contact(Name name) {
super();
this.name = name;
}
public Name getName() {
return name;
}
public void setName(Name name) {
this.name = name;
}
}
public class Name {
private String firstName;
private String lastName;
public Name(String firstName, String lastName) {
super();
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
}
I looked into the java documentation, it's so generic that it doesn't give any clue how to work with nested fields. I wonder the API writer consider adding example when they create it otherwise everyone just wonder how to use it! sad