Java 如何要求 BeanUtils 忽略空值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2601175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:43:59  来源:igfitidea点击:

How to ask BeanUtils to ignore null values

javaapache-commons-beanutils

提问by Kannan Ekanath

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,

使用 Commons beanUtils 我想知道如何让任何转换器说 Dateconverter 忽略空值并使用 null 作为默认值。作为一个例子,考虑一个公共类,

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 converterto do this. Can someone point me as to the best way for doing this ?

由于日期恰好为空,因此上面会抛出一个 NPE。对我来说,这看起来是一个非常原始的场景,应该默认处理(例如,我希望 x2 为 date1 具有空值)。doco 告诉我,我可以要求转换器执行此操作。有人可以指出我这样做的最佳方法吗?

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 !

我不想让 Converter 和 isUseDefault() 为真,因为那样我必须为所有 Date、Enum 和许多其他转换器做这件事!

采纳答案by Kannan Ekanath

Apparently it looks like, there is a way to tell the ConvertUtils to not throw exceptions on null values which is achieved by calling

显然,有一种方法可以告诉 ConvertUtils 不对空值抛出异常,这是通过调用实现的

BeanUtilsBean.getInstance().getConvertUtils().register(false, false, 0);

回答by Ninju Bohra

Maybe a little late but looks that you can register a DateConverter https://issues.apache.org/jira/browse/BEANUTILS-387

也许有点晚了,但看起来你可以注册一个 DateConverter https://issues.apache.org/jira/browse/BEANUTILS-387

回答by James Drinkard

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.

我最近遇到了这个问题,只是将我的变量转换为字符串以避免此错误,并在需要时将其转换回日期。不是最优雅的解决方案,但为了简单起见并避免此类问题,这是一个可行的解决方案。另一个警告是 BeanUtils 会在我的类加载之前触发它的方法,所以我选择了这个而不是使用自定义类加载器来解决问题的更复杂的解决方案。

By the way, prior to verion 1.8.0, BeanUtils itself would ignore these null values.

顺便说一句,在 1.8.0 版之前,BeanUtils 本身会忽略这些空值。

See this link: No value specified for 'Date' when the field is a java.util.Date with a null valuefor a detailed explanation.

有关详细说明,请参阅此链接: 当字段为具有空值的 java.util.Date 时,没有为“日期”指定值

回答by Diego Plentz

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

最好的解决方案是更新到 BeanUtils 1.9.0,因为这个问题已经修复,你可以在这里看到https://issues.apache.org/jira/browse/BEANUTILS-454

回答by Marc

I am somewhat amazed that such a simple case as setting a null value in a bean, like this:

我有点惊讶在 bean 中设置空值这样简单的情况,如下所示:

BeanUtils.setProperty(pojo, "date", null);

causes crashing behavior, as described above.

导致崩溃行为,如上所述。

For what it's worth, here is my workaround:

对于它的价值,这是我的解决方法:

import org.apache.commons.beanutils.BeanMap;

BeanMap beanMap = new BeanMap(pojo);
Method writeMethod = beanMap.getWriteMethod("date");
writeMethod.invoke(pojo, null);