[英]Add auto prefix by mapping the fields names
I use Orika Mapper and have the following: 我使用Orika Mapper,并具有以下功能:
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:
由于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.
是否可以通过映射来添加一些自动前缀以添加
content.
to every field
method? 每种
field
方法?
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. 更新#1:尝试使用
DefaultFieldMapper
并删除了所有手动映射-没有成功,但默认情况下映射Links对象没有任何问题。 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.. 接收
content
和links
的BusinessResource
属性,这就是为什么结果对象为空的原因。
Orika provide a way to give it a hint about how to do auto mapping : DefaultFieldMapper Orika提供了一种方法来提示如何进行自动映射: 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.