简体   繁体   English

在Java中从一个对象映射到另一个(传输对象)时忽略空字段?

[英]Ignoring null fields when mapping from one object to another (Transfer Object) in java?

I'm writing a transfer object which simply maps an object coming back from a webservice to my own object: Car A to Car B. 我正在编写一个传输对象,该对象只是将一个从Web服务返回的对象映射到我自己的对象:Car A到CarB。

a Car class has a mileage property. 汽车类具有里程属性。

CarB.setMileage(CarA.getMileage()); //if the CarA.getMileage() is null, then my setter fails and I get a nullPointerException

I have 50 fields, and am I correct that I just need write 50 separate checks for null before setting my fields? 我有50个字段,我是否正确,我只需要在设置字段之前写50个单独的null检查?

ie,

if (CarA.getMileage() != null) {
   CarB.setMileage(CarA.getMileage());
}

Is there a way to avoid writing 50 separate if !=null check statements? 有没有一种方法可以避免编写50个单独的if !=null检查语句?

Try using Dozer mapping. 尝试使用推土机映射。 It is a good tool and it can help ignoring null values too. 这是一个很好的工具,它也可以帮助忽略空值。 It can be used to map two object programatically and using XML. 它可以用于以编程方式并使用XML映射两个对象。 You do not to specify any mapping between two fields of objects if attribute names in them are same. 如果对象的两个字段之间的属性名称相同,则不要指定任何映射。 Following is the link: 以下是链接:

http://dozer.sourceforge.net/documentation/faq.html http://dozer.sourceforge.net/documentation/faq.html

You can create a static factory method in CarB : 您可以在CarB创建静态工厂方法:

// carA is of type CarA returned by the web service
CarB carB = CarB.fromCarA(carA);

All null checks etc would then be done in that factory method. 然后,所有空检查等都将在该工厂方法中完成。 Sample code: 样例代码:

public class CarB
{
    //....

    public static CarB fromCarA(final CarA carA)
    {
        if (carA == null)
            throw new NullPointerException("Where is my car???");

        final CarB ret = new CarB();

        // Supposing CarA returns an Integer...
        final Integer mileage = carA.getMileage();
        if (mileage != null)
            ret.setMileage(mileage);

        // etc etc, then
        return ret;
    }

    // etc
}

Little common but the better way is a common method in your bean Class 很少见但更好的方法是bean Class的通用method

function String checkForNull(String str){

  //check for null and return corresponding
}

and then 接着

CarB.setMileage(checkForNull(CarA.getMileage()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java从一个对象到另一个对象的条件映射? - Java conditional mapping from one object to another? 从一个 object 复制 not null and not empty fields 到 java 中的另一个 object 相同类型(对象是相同类型) - Copy not null and not empty fields from one object to another object of the same type(Objects are same type) in java 如何将对象从一类转移到另一类-JAVA - How to transfer object from one class to another class - JAVA JAVA OOP资金从一个对象转移到另一个对象 - JAVA OOP money transfer from one object to another Java Web开发:在传递请求对象的同时将控制权从一个servlet转移到另一个servlet(版本2) - Java web development: transfer control from one servlet to another while passing the request object (Version 2) 将所有字段从一个对象移动到另一个对象 - Moving all fields from one object into another 将匹配字段从一个 object 列表复制到 java 中的另一个 object 列表 - Copying matching fields from one object list to another object list in java 发送 object 时忽略 JSON 字段(反序列化) - Ignoring JSON fields when sending an object (deserialization) Java bean映射库-选择哪个(将一个对象转换为另一个对象)? - Java bean mapping library - which to choose (transforming one object to another)? 我无法将对象从一项活动转移到另一项活动 - I can't transfer an object from one activity to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM