此问题的案发现场是测试环境,为了减小没必要要的类,咱们这里用一个简单的例子进行模拟。java
lombok version:1.16.14api
jdk version:1.8oracle
上代码:
<!-- more -->app
├─src │ ├─main │ │ ├─java │ │ │ └─org │ │ │ └─jsbxyyx │ │ │ └─util │ │ │ DozerUtil.java │ │ │ │ │ └─resources │ └─test │ ├─java │ │ └─org │ │ └─jsbxyyx │ │ └─test │ │ A.java │ │ A1.java │ │ DozerTest.java │ │ │ └─resources
package org.jsbxyyx.util; import com.google.common.collect.Lists; import org.dozer.DozerBeanMapper; import java.util.Collection; import java.util.List; public class DozerUtil { private static DozerBeanMapper dozer = new DozerBeanMapper(); public static <T> T map(Object source, Class<T> destinationClass) { return dozer.map(source, destinationClass); } public static <T> List<T> mapList(@SuppressWarnings("rawtypes") Collection sourceList, Class<T> destinationClass) { List<T> destinationList = Lists.newArrayList(); for (Object sourceObject : sourceList) { T destinationObject = dozer.map(sourceObject, destinationClass); destinationList.add(destinationObject); } return destinationList; } public static void map(Object source, Object destinationObject) { dozer.map(source, destinationObject); } }
package org.jsbxyyx.test; import lombok.Getter; import lombok.Setter; import lombok.ToString; @Setter @Getter @ToString public class A { private String date; private String vNum; }
package org.jsbxyyx.test; import lombok.Getter; import lombok.Setter; import lombok.ToString; @Setter @Getter @ToString public class A1 { private String date; private String vaNum; }
package org.jsbxyyx.test; import java.util.HashMap; import java.util.Map; import org.jsbxyyx.util.DozerUtil; import org.junit.Assert; import org.junit.Test; public class DozerTest { @Test public void testA() throws Exception { Map<String, String> mapA = new HashMap<>(); mapA.put("date", "2018-04-03"); mapA.put("vNum", "123"); A a = DozerUtil.map(mapA, A.class); Assert.assertEquals("123", a.getVNum()); } @Test public void testA1() throws Exception { Map<String, String> mapA1 = new HashMap<>(); mapA1.put("date", "2018-04-03"); mapA1.put("vaNum", "123"); A1 a = DozerUtil.map(mapA1, A1.class); Assert.assertEquals("123", a.getVaNum()); } }
咱们执行测试用testA和testA1发现eclipse
testA红了ide
testA1绿了测试
红了表示测试不经过啊。。。。。咱们来看看为啥不经过。google
问题的关键在于lombok生成getter,setter的时候没有按照JavaBean的规范来生成。致使testA就红了。idea
直接看反编译代码:spa
A.class
咱们再看看eclipse或者idea生成getter,setter是怎么样的
到了这里,咱们发现问题了,lombok的getter,setter方法生成的不对。
解决办法,去掉@Getter @Setter注解,用IDE生成,获取覆盖相应的getter/setter
那么咱们来分析一下,标准JavaBean是如何生成的。
看到英语头大有木有。。。有木有。。。有木有。。。
好咱们解释一下。
首先咱们来描述一下JavaBean的规范
接下来咱们来讲一下javabean的getters/setters是如何生成,也就解决了问题。。
这个类Introspector.decapitalize()方法,能够获取到属性
Utility method to take a string and convert it to normal Java variable name capitalization. This normally means converting the first character from upper case to lower case, but in the (unusual) special case when there is more than one character and both the first and second characters are upper case, we leave it alone. Thus "FooBah" becomes "fooBah" and "X" becomes "x", but "URL" stays as "URL".
意思就是第一个字母转大写后,若是第二个字母和转换后的第一个字母同样是大写,那么就返回原值。不然就返回首字母大写后的值。
问题就到这里啦。。。。。。。