2016-10-05 64 views
1

有沒有使用Orika將字段值設置爲文字或常量值的方法?Orika:將常量默認值映射到字段

我的意思是這樣的:

public class ObjectMapper extends ConfigurableMapper { 

     protected void configure(MapperFactory factory) { 
      factory.classMap(SourceClass.class, DestinationClass.class) 
      .field("myField", "CONSTANT_VALUE") 
      .byDefault() 
      .register(); 
     } 
} 

我無法找到文件類似的東西...

回答

0

根據Orika文檔的this section,設置文本值的唯一途徑是通過自定義映射。例如:

public class ObjectMapper extends ConfigurableMapper { 

     protected void configure(MapperFactory factory) { 
      factory.classMap(SourceClass.class, DestinationClass.class) 
      .byDefault() 
      .customize(new CustomMapper<SourceClass, DestinationClass>() { 
       @Override 
       public void mapAtoB(SourceClass a, DestinationClass b, MappingContext context) { 
        b.setMyField("CONSTANT_VALUE"); 
       } 

       // ... 
      }) 
      .register(); 
     } 
}