Prevents Dozer from mapping empty properties __java

Source: Internet
Author: User

Dozer is a Java Bean Copy class library that performs better than Apache Beanutils, but it is inconvenient that both of them will copy the empty attributes. In Dozer, you can disable an empty property copy using an XML map, and you have to configure XML to feel cumbersome.
In fact, you can construct a beanmappingbuilder to configure the mapping.

Mapping (Sources.getclass (), Destination.getclass (), Mapnull (false), mapemptystring (false));

respectively on

Org.dozer.loader.api.TypeMappingOptions.mapEmptyString
Org.dozer.loader.api.TypeMappingOptions.mapNull
Set to False.

So it can be encapsulated into a util

public static void Copyproperties (final object sources, final object destination) {Weakr
        Eference weakreference = new WeakReference (new Dozerbeanmapper ());
        Dozerbeanmapper mapper = (dozerbeanmapper) weakreference.get ();
                Mapper.addmapping (New Beanmappingbuilder () {@Override protected void Configure () {
            Mapping (Sources.getclass (), Destination.getclass (), Mapnull (false), mapemptystring (false));
        }
        });
        Mapper.map (sources, destination);
        Mapper.destroy ();
    Weakreference.clear (); }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.