Java-如何处理大型可空字段集的NullPointerException?
假设我有一个10个唯一的POJO,每个POJO有10个唯一命名的字段 此POJO是一个反序列化的SOAP响应。我想将SOAP POJO中的字段“复制”到表示JSON对象的POJO上,然后序列化该POJO 让我们将SOAP对象命名为o0。。。o9Java-如何处理大型可空字段集的NullPointerException?,java,json,soap,Java,Json,Soap,假设我有一个10个唯一的POJO,每个POJO有10个唯一命名的字段 此POJO是一个反序列化的SOAP响应。我想将SOAP POJO中的字段“复制”到表示JSON对象的POJO上,然后序列化该POJO 让我们将SOAP对象命名为o0。。。o9 让我们将SOAP字段命名为f0。。。f9 让我们将JSON对象命名为jo 让我们将JSON字段命名为f0。。。f99 最后,所有字段都是值的容器,因此每个字段都有一个.getValue()方法 假设我们所有的字段都有getter和setter,根据我的理
让我们将SOAP字段命名为f0。。。f9 让我们将JSON对象命名为jo
让我们将JSON字段命名为f0。。。f99 最后,所有字段都是值的容器,因此每个字段都有一个.getValue()方法 假设我们所有的字段都有getter和setter,根据我的理解,实现这一点的唯一方法是硬编码以下内容:
jo.setF0(o0.getF0().getValue());
jo.setF1(o0.getF1().getValue());
...
jo.setF49(o4.getF9().getValue());
...
jo.setF99(o9.getF9().getValue());
现在,问题是属于o0的任何字段。。。o9可能为空,这将导致NullPointerException
有没有办法不用写100个try/catch块就可以解决这个问题?也许您可以防止空指针,在设置值之前测试对象是否为空
o0.getF0!=null ? o0.getF0().getValue(): null;
请看一些类似于复制属性的框架 这里是e bean值复制器的一个简单实现,它复制源和目标中存在的所有非空属性:
public class BeanValueCopier {
static final Map<Class<?>, Map<String, PropertyDescriptor>> beanPropertiesByType = new ConcurrentHashMap<>();
public static void copyNonNullValues(Object src, Object dest) {
try {
Map<String, PropertyDescriptor> sourceMap = propertyMap(src.getClass());
Map<String, PropertyDescriptor> targetMap = propertyMap(dest.getClass());
for (Map.Entry<String, PropertyDescriptor> entry : sourceMap.entrySet()) {
final String propertyName = entry.getKey();
if (targetMap.containsKey(propertyName)) {
final PropertyDescriptor sourceDesc = entry.getValue();
final PropertyDescriptor targetDesc = targetMap.get(propertyName);
if (targetDesc.getPropertyType().equals(sourceDesc.getPropertyType()) && sourceDesc.getReadMethod() != null && targetDesc.getWriteMethod() != null) {
final Object value = sourceDesc.getReadMethod().invoke(src);
if (value != null) targetDesc.getWriteMethod().invoke(dest, value);
}
}
}
} catch (InvocationTargetException | IllegalAccessException | IntrospectionException e) {
throw new IllegalStateException(e);
}
}
private static Map<String, PropertyDescriptor> propertyMap(Class<?> beanClass) throws IntrospectionException {
Map<String, PropertyDescriptor> beanProperties = beanPropertiesByType.get(beanClass);
if (beanProperties == null) {
beanProperties = new HashMap<>();
for (PropertyDescriptor propertyDescriptor : Introspector.getBeanInfo(beanClass, Object.class).getPropertyDescriptors()) {
beanProperties.put(propertyDescriptor.getName(), propertyDescriptor);
}
beanPropertiesByType.put(beanClass, beanProperties);
}
return beanProperties;
}
}
公共类BeanValue复印机{
静态最终映射,如果某个对象为null,则该对象为null。这意味着您要创建的新对象的某些属性中必须能够(允许)null。您可以在setFXXX()中检查null如果您不能在源域和目标域模型上更改任何其他内容,则调用null.getValue()会得到null NullPointerException,而不是因为obj.getValue()的原因==null。这相当于编写try/catch块,因为它仍然不是一般情况。BeanUtils属性副本与我的用例不匹配,因为无法保证字段是相同的结构。例如,soapresponse/body/user/id
可能会被复制到jsonresponse/userid
。如果允许null可启用属性?BeanUtils属性副本与我的用例不匹配,因为不能保证字段是相同的结构。例如,soapresponse/body/user/id
可能会被复制到jsonresponse/userid
。还有一个问题是soapPOJO:jsonPOJO的比例为10:1。@PhilLiu太糟糕了。也许你应该有门蒂在你的问题中,也许像Dozer这样的属性映射器框架适合你