简体   繁体   中英

Add auto prefix by mapping the fields names

I use Orika Mapper and have the following:

mapperFactory.classMap(BusinessResource.class, BusinessDto.class)
                .field("content._id", "_id")
                .field("content.uuid", "uuid")
                .field("content.created", "created")
                .field("content.name", "name")
                .field("content.phone", "phone")
                .field("content.fax", "fax")
                .field("content.email", "email")
                .field("content.address", "address")
                .field("content.coordinates", "coordinates")...

Why I have to use it this way? Because of BusinessResoruce:

public class BusinessResource extends Resource<Business> {
   private Business content;
   private Link[] links;
}

Is it possible to make some auto prefix by mapping which will add content. to every field method?

Thank you in advance

UPDATE #1: Tried to use DefaultFieldMapper and removed all the manual mappings - without success, but Links object is mapped by default without any problem. The main problem with this solution:

suggestMappedField(String fromProperty, Type<?> fromPropertyType)

receives the properties of BusinessResource which are content and links , and that's why the resulted object if empty..

Orika provide a way to give it a hint about how to do auto mapping : DefaultFieldMapper

You can use it on class map levels or even registering the default field mapper globally.

Here is an example:

DefaultFieldMapper myHint = new DefaultFieldMapper() {
public String suggestMappedField(String fromProperty, Type<?> fromPropertyType) {
  return "content." + fromProperty;
}};

You can create default

factory.registerDefaultFieldMapper(myHint);
// OR factory.classMap() ... .byDefault(myHint);

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