Java 具有组合属性的Modelmapper
给以下模型Java 具有组合属性的Modelmapper,java,modelmapper,Java,Modelmapper,给以下模型 class Project { String groupId; String artifactId; String version; } class ProjectWithId { String id; String groupId; String artifactId; String version; } 如何正确使用ModelMapper组合groupId、artifactId和version的值?例如,是否有办法避免以下情况: ProjectWi
class Project {
String groupId;
String artifactId;
String version;
}
class ProjectWithId {
String id;
String groupId;
String artifactId;
String version;
}
如何正确使用ModelMapper组合groupId、artifactId和version的值?例如,是否有办法避免以下情况:
ProjectWithId projectWithId = modelMapper.map(project, ProjectWithId.class);
projectWithId.setId(project.getGroupId() + ":" + project.getArtifactId() + ":" + project.getVersion());
您需要创建自定义转换器来组合3个属性,即groupId、artifactId和version 例如
Converter<String, String> converter = new Converter<String, String>() {
public String convert(MappingContext<String, String> context) {
Project project = (Project) context.getParent().getSource();
return project.groupId + project.artifactId+ project.version;
}
};
modelMapper.addConverter(converter);