简体   繁体   中英

Dozer: Is it possible to one-way exclude a field on all mapped classes?

I want to exclude all 'createdDate' fields from being deserialized on all of my mapped fields:

Similar to this:

<field-exclude type="one-way""> <a>createdDate</a> <b>createdDate</b> </field-exclude>

Thus, having the field visible when querying but not updatable.

Is there a way to do this globally without having to specify it on each and every mapping?

Move all your common fields to super class on source and destination classes. and create mapping for super classes. Full example can be found at Dozer fields - Global Exclude Example

Step 1:Source POJOs:

    //PersonType.java
public class AddressType extends BaseType{

    private String addrLine1;
    private String city;
    private String state;
    private int zipCode;
    // Setter and Getter methods are removed
}
// BaseType.java
public class BaseType { 
        private String createdDate;
    // Setter and Getter methods are removed
}

Step 2: Target POJOs:

// Address.java
public class Address extends BaseDomain{

    private String addrLine1;
    private String city;
    private String state;
    private int zip5;
// Setter and Getter methods are removed
}
//BaseDomain.java
public class BaseDomain {   
    private String createdDate;
// Setter and Getter methods are removed
}

Step 3: Create Mapping

<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozer.sourceforge.net"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://dozer.sourceforge.net
          http://dozer.sourceforge.net/schema/beanmapping.xsd">
     <mapping>
            <class-a>com.codesimplify.dozersamples.fieldexclude.AddressType</class-a>
            <class-b>com.codesimplify.dozersamples.fieldexclude.Address</class-b>   
    </mapping>

   <mapping>
    <class-a>com.codesimplify.dozersamples.fieldexclude.BaseType</class-a>
    <class-b>com.codesimplify.dozersamples.fieldexclude.BaseDomain</class-b>
    <field-exclude type="one-way">
        <a>createdDate</a>
        <b>createdDate</b>
    </field-exclude>    
    </mapping>
</mappings>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM