我有 2 个对象:
@Setter
@Getter
public class Agent {
public int userID;
public String name;
public boolean isVoiceRecorded;
public boolean isScreenRecorded;
public boolean isOnCall;
public LocalDateTime startEventDateTime;
}
public class AgentLine {
public int userID;
public String name;
public boolean isVoiceRecorded;
public boolean isScreenRecorded;
public boolean isOnCall;
public String startEventDateTime;
}
我想在 AgentLine 和 Agent 之间进行映射。由于 Localdatetime 转换,我无法使用默认映射。我已经定义:
@Bean
ModelMapper getModelMapper() {
ModelMapper modelMapper = new ModelMapper();
Converter<AgentLine, Agent> orderConverter = new Converter<AgentLine, Agent>() {
@Override
public Agent convert(MappingContext<AgentLine, Agent> mappingContext) {
AgentLine s = mappingContext.getSource();
Agent d = mappingContext.getDestination();
/* d.userID = s.userID;
d.name = s.name;*/
d.startEventDateTime = LocalDateTime.parse(s.startEventDateTime, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
return d;
}
};
modelMapper.addConverter(orderConverter);
return modelMapper;
}
为了使用它:
AgentLine line;
@Autowired
private ModelMapper modelMapper;
Agent agent = modelMapper.map(line, Agent.class);
它可以工作,但我不想在转换方法中指定所有代理属性,我想指定 startEventDateTime 转换,其余属性将默认映射。
另外我试图定义:
PropertyMap<AgentLine, Agent> orderMap = new PropertyMap<AgentLine, Agent>() {
@Override
protected void configure() {
map().setName(source.name);
}
};
modelMapper.addMappings(orderMap);
但是,在映射中您无法处理日期转换。如果我为映射器 PropertyMap 和 Converter 定义 PropertyMap 将被忽略。
我不想在 convert 方法中指定所有 Agent 属性,我想指定 startEventDateTime 转换,其余属性将默认映射。