This commit is contained in:
Looly 2020-10-29 16:54:51 +08:00
parent 282e1e9090
commit 1f9d1c2888
5 changed files with 55 additions and 13 deletions

View File

@ -3,7 +3,7 @@
-------------------------------------------------------------------------------------------------------------
# 5.4.7 (2020-10-28)
# 5.4.7 (2020-10-29)
### 新特性
* 【core 】 增加OptionalBeanpr#1182@Github
@ -13,7 +13,9 @@
* 【http 】 增加SoapClient增加addSOAPHeader重载
* 【http 】 ArrayUtil增加containsAll方法
* 【http 】 增加CharsetDetector
### Bug修复
* 【core 】 修复BeanUtil.beanToMap方法中editor返回null没有去掉的问题
-------------------------------------------------------------------------------------------------------------

View File

@ -618,7 +618,7 @@ public class BeanUtil {
* @param bean bean对象
* @param targetMap 目标的Map
* @param ignoreNullValue 是否忽略值为空的字段
* @param keyEditor 属性字段Map的key编辑器用于筛选编辑key
* @param keyEditor 属性字段Map的key编辑器用于筛选编辑key如果这个Editor返回null则忽略这个字段
* @return Map
* @since 4.0.5
*/

View File

@ -172,6 +172,12 @@ public class BeanCopier<T> implements Copier<T>, Serializable {
return;
}
// 对key做映射映射后为null的忽略之
key = copyOptions.editFieldName(copyOptions.getMappedFieldName(key, false));
if(null == key){
return;
}
Object value;
try {
value = prop.getValue(bean);
@ -188,8 +194,6 @@ public class BeanCopier<T> implements Copier<T>, Serializable {
return;
}
// 对key做映射
key = copyOptions.editFieldName(copyOptions.getMappedFieldName(key, false));
targetMap.put(key, value);
});
}

View File

@ -194,7 +194,8 @@ public class CopyOptions implements Serializable {
/**
* 设置字段属性编辑器用于自定义属性转换规则例如驼峰转下划线等<br>
* 此转换器只针对源端的字段做转换请确认转换后与目标端字段一致
* 此转换器只针对源端的字段做转换请确认转换后与目标端字段一致<br>
* 当转换后的字段名为null时忽略这个字段
*
* @param fieldNameEditor 字段属性编辑器用于自定义属性转换规则例如驼峰转下划线等
* @return CopyOptions

View File

@ -16,8 +16,11 @@ import java.io.Serializable;
import java.lang.reflect.Type;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;
@ -76,7 +79,7 @@ public class BeanUtilTest {
}
@Test
public void toBeanTest(){
public void toBeanTest() {
SubPerson person = new SubPerson();
person.setAge(14);
person.setOpenid("11213232");
@ -95,7 +98,7 @@ public class BeanUtilTest {
* 忽略转换错误测试
*/
@Test
public void toBeanIgnoreErrorTest(){
public void toBeanIgnoreErrorTest() {
HashMap<String, Object> map = CollUtil.newHashMap();
map.put("name", "Joe");
// 错误的类型此处忽略
@ -372,7 +375,7 @@ public class BeanUtilTest {
@Getter
@Setter
public static class Person {
private String name;
private String name;
private int age;
private String openid;
}
@ -384,6 +387,7 @@ public class BeanUtilTest {
private int age;
private String openid;
}
public static class Person2 {
public Person2(String name, int age, String openid) {
@ -445,9 +449,9 @@ public class BeanUtilTest {
Assert.assertEquals(info.getBookID(), entity.getBookId());
Assert.assertEquals(info.getCode(), entity.getCode2());
}
@Test
public void copyBeanTest(){
public void copyBeanTest() {
Food info = new Food();
info.setBookID("0");
info.setCode("123");
@ -473,14 +477,14 @@ public class BeanUtilTest {
}
@Test
public void setPropertiesTest(){
public void setPropertiesTest() {
Map<String, Object> resultMap = MapUtil.newHashMap();
BeanUtil.setProperty(resultMap, "codeList[0].name", "张三");
Assert.assertEquals("{codeList={0={name=张三}}}", resultMap.toString());
}
@Test
public void beanCopyTest(){
public void beanCopyTest() {
final Station station = new Station();
station.setId(123456L);
@ -499,7 +503,38 @@ public class BeanUtilTest {
}
@Data
public static class Entity<T>{
public static class Entity<T> {
private T id;
}
@Test
public void toMapTest() {
// 测试转map的时候返回key
String name = null;
PrivilegeIClassification a = new PrivilegeIClassification();
a.setId("1");
a.setName("2");
a.setCode("3");
a.setCreateTime(new Date());
a.setSortOrder(9L);
Map<String, Object> f = BeanUtil.beanToMap(
a,
new LinkedHashMap<>(),
false,
key -> Arrays.asList("id", "name", "code", "sortOrder").contains(key) ? key : null);
Assert.assertFalse(f.containsKey(null));
}
@Data
public static class PrivilegeIClassification implements Serializable {
private static final long serialVersionUID = 1L;
private String id;
private String name;
private String code;
private Long rowStatus;
private Long sortOrder;
private Date createTime;
}
}