如何要求 BeanUtils 忽略空值
使用 Commons beanUtils 我想知道如何要求任何转换器(例如 Dateconverter)忽略 null 值并使用 null 作为默认值。作为一个例子,考虑一个公共类,
public class X {
private Date date1;
private String string1;
//add public getters and setters
}
我的转换器测试为,
public class Apache {
@Test
public void testSimple() throws Exception {
X x1 = new X(), x2 = new X();
x1.setString1("X");
x1.setDate1(null);
org.apache.commons.beanutils.BeanUtils.copyProperties(x2, x1);
//throws ConversionException
System.out.println(x2.getString1());
System.out.println(x2.getDate1());
}
}
上面抛出一个 NPE,因为日期恰好为空。对我来说,这看起来是一个非常原始的场景,应该默认处理(例如,我希望 x2 对于 date1 具有空值)。 doco告诉我,我可以询问 转换器 来执行此操作。有人可以指出我执行此操作的最佳方法吗?
我不想让 Converter 和 isUseDefault() 为 true,因为那样我就必须对所有 Date、Enum 和许多其他转换器执行此操作!
Using Commons beanUtils I would like to know how to ask any converter say the Dateconverter to ignore null values and use null as default. As an example consider a public class,
public class X {
private Date date1;
private String string1;
//add public getters and setters
}
and my convertertest as,
public class Apache {
@Test
public void testSimple() throws Exception {
X x1 = new X(), x2 = new X();
x1.setString1("X");
x1.setDate1(null);
org.apache.commons.beanutils.BeanUtils.copyProperties(x2, x1);
//throws ConversionException
System.out.println(x2.getString1());
System.out.println(x2.getDate1());
}
}
The above throws a NPE since the date happens to be null. This looks a very primitive scenario to me which should be handled by default (as in, I would expect x2 to have null value for date1). The doco tells me that I can ask the converter to do this. Can someone point me as to the best way for doing this ?
I dont want to get hold of the Converter and isUseDefault() to be true because then I have to do it for all Date, Enum and many other converters !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
显然,有一种方法可以告诉 ConvertUtils 不要对空值抛出异常,这是通过调用来实现的
Apparently it looks like, there is a way to tell the ConvertUtils to not throw exceptions on null values which is achieved by calling
最好的解决方案是更新到 BeanUtils 1.9.0,因为这个问题已得到解决,如您在此处看到的 https://issues.apache.org/jira/browse/BEANUTILS-454
The best solution is update to BeanUtils 1.9.0, since this problem is fixed as you can see here https://issues.apache.org/jira/browse/BEANUTILS-454
也许有点晚了,但看起来你可以注册一个 DateConverter
https://issues.apache.org/jira/browse/BEANUTILS-387
Maybe a little late but looks that you can register a DateConverter
https://issues.apache.org/jira/browse/BEANUTILS-387
我最近遇到了这个问题,只是将变量转换为字符串以避免此错误,并在需要时将其转换回日期。这不是最优雅的解决方案,但为了简单起见并避免此类问题,它是一个可行的解决方案。另一个需要注意的是,BeanUtils 会在我的类加载之前触发它的方法,因此我选择了这种方法,而不是使用自定义类加载器来解决问题的更复杂的解决方案。
顺便说一下,在 1.8.0 版本之前,BeanUtils 本身会忽略这些空值。
请参阅此链接:
当字段是具有空值的 java.util.Date 时,没有为“日期”指定值 获取详细解释。
I recently ran into this issue and just converted my variable to a string to avoid this error and converted it back to a date when needed. Not the most elegant solution, but for simplicity and to avoid problems like this, it's a viable solution. The other caveat was that BeanUtils would fire off it's methods before my classes would load, so I opted for this rather than a more complicated solution to the problem using custom classloaders.
By the way, prior to verion 1.8.0, BeanUtils itself would ignore these null values.
See this link:
No value specified for 'Date' when the field is a java.util.Date with a null value for a detailed explanation.
我有点惊讶,像这样在 bean 中设置 null 值这样的简单情况,
会导致崩溃行为,如上所述。
对于它的价值,这是我的解决方法:
I am somewhat amazed that such a simple case as setting a null value in a bean, like this:
causes crashing behavior, as described above.
For what it's worth, here is my workaround: