我必须通过ModelMapper将我从UI获得的DTO映射到我的实体。
在使用ModelMapper之前,我有以下方法:
private Operation map(OperationForm src) {
Operation operation = new Operation();
LocalDateTime NOW = LocalDateTime.now();
operation.setOperationKey(OperationKey.of(src.getLegalEntityId(),
src.getEmployeeId(),
NOW)); // here
operation.setIndividualId(src.getIndividualId());
operation.setKey(src.getLegalEntityId() + "_"
+ src.getEmployeeId() + "_"
+ NOW); // and here
return operation;
}
在这种情况下,我必须通过LocalDateTime.now()生成新的LocalDateTime,并在两个不同的字段中使用它。
所以,我尝试这样配置ModelMapper:
public OperationFormMapper(ModelMapper modelMapper) {
Converter<OperationForm, OperationKey> operationKeyConverter = (context) -> OperationKey.of(context.getSource().getLegalEntityId(),
context.getSource().getEmployeeId(),
LocalDateTime.now()); // generate new LocalDateTime to
// destination instance
Converter<Operation, String> keyConverter = (context) -> {
Operation src = context.getSource();
return src.getOperationKey().getLegalEntityId() + "_"
+ src.getOperationKey().getEmployeeId() + "_"
+ src.getOperationKey().getOperationDate();
}; // converter for my purpose
// I am trying to get value not from source,
// but from destination instance, hoping that
// all needed fields are already mapped
this.modelMapper = modelMapper;
this.modelMapper.addMappings(new PropertyMap<OperationForm, Operation>() {
@Override
protected void configure() {
using(operationKeyConverter).map(source, destination.getOperationKey());
map().setIndividualId(source.getIndividualId());
map().setOperationStatus(Operation.OperationStatus.CREATED);
using(keyConverter).map(destination, destination.getKey());
// trying to get all needed data from already mapped fields
}
});
}
它不起作用并不奇怪。我得到了NullPointerException。显然,在我试图获取关键字段的值时,字段仍然没有映射。
那么,有人知道如何处理这一时刻,并使ModelMapper也为我的案例工作吗?或者至少有一些方法可以完全定制映射过程并创建整个方法,该方法将由ModelMapper用于映射(或者使其使用我现有的方法)?
更新:
我的实体:
public class Operation {
private OperationKey operationKey;
private Long individualId;
private String operationStatus;
private String operationResult;
private String guardCardSeries;
private String guardCardNumber;
private LocalDateTime guardCardStartDate;
private LocalDateTime guardCardEndDate;
private String resultMessage;
private String key;
}
用户界面中的我的DTO:
public class OperationForm implements Serializable {
private Long legalEntityId;
private Long employeeId;
private Long individualId;
}
我的Cassandra DB复合密钥:
public class OperationKey {
private final Long legalEntityId;
private final Long employeeId;
private final LocalDateTime operationDate;
}
我只是通过删除持久性注释简化了它们。
看来我找到了解决办法。
我的ModelMapper配置现在。
public OperationFormMapper(ModelMapper modelMapper) {
this.modelMapper = modelMapper;
Converter<OperationForm, Operation> converter = context -> {
OperationForm src = context.getSource();
Operation dest = new Operation();
final LocalDateTime NOW = LocalDateTime.now();
dest.setOperationKey(OperationKey.of(src.getLegalEntityId(), src.getEmployeeId(), NOW));
dest.setIndividualId(src.getIndividualId());
dest.setKey(src.getLegalEntityId() + "_" + src.getEmployeeId() + "_" + NOW);
return dest;
};
this.modelMapper.addConverter(converter);
}
我创建了转换器并将我的方法推入其中。现在它起作用了。
问题内容: 我正在尝试使用Jackson将一些JSON对象映射到Java对象。JSON对象中的某些字段是必填字段(可以用标记),而某些字段是可选字段。 用Jackson映射之后,在JSON对象中未设置的所有字段在Java中都将具有空值。是否有类似的注释可以告诉Jackson为Java类成员设置默认值(如果为null)? 编辑:为了使问题更清楚,这里是一些代码示例。 Java对象: JSON对象可以
我正在尝试用Jackson将一些JSON对象映射到Java对象。JSON对象中的一些字段是强制性的(我可以用标记),而一些字段是可选的。 在与Jackson进行映射之后,JSON对象中没有设置的所有字段在Java中都将有一个空值。是否有类似于的注释可以告诉Jackson为Java类成员设置默认值,以防它为空? 或: 注释只是为了显示我的要求。这不是真正的注解。如果JSON对象与第一个示例类似,我希
概述:最初,我对这个程序的预期目的是制作一个Treemap来读取文本文档,特别是找到所有单词和单词所在的索引/行。现在我想列出一个“十大”名单,其中包含使用最多的单词。我想“翻转”我的树状图,这样整型值将是按顺序排列的,然后是字符串
我与ModelMapper框架有麻烦。请解释为什么我看到以下行为。 我在build.gradle有以下依赖性 和一个类客户: 我还有一个地图绘制工具: 还有一个测试 在fred()中,方法输出是非红色的“Customer{name=fred,age=40}”(“Customer{name=null,age=40}”)。你能解释一下为什么吗?为什么我在第一个方法中看不到输出“George”?
我需要将Java转换为的实例(包括映射内容) 我应该用什么来代替来使此代码可编译?
我是初学者,我想通过使用两个旧映射hm1和hm2创建一个新映射hm3,在该映射中,我需要第二个映射的值作为键,第一个映射的值作为值,例如:如果映射hm1包含a1作为key1和abc作为value1,它还包含a2作为key2和xyz作为value2,并且还有另一个映射hm2,它包含a1作为key1和b1作为value1,还包含a2作为key2和b2作为value2,那么在映射hm3中,我需要b1作为