java - ModelMapper:匹配多个源属性层次结构

标签 java modelmapper

我无法解决 modelMapper 错误。您知道问题出在哪里吗?

注意:鉴于 java.sql.Time 没有非参数构造函数,我没有找到比编写转换器更好的方法

org.modelmapper.ConfigurationException: ModelMapper configuration errors:

1) The destination property 
biz.models.CarWash.setSecondShift()/java.util.Date.setTime() matches 
multiple source property hierarchies:

biz.dto.CarWashDTO.getFirstShift()/java.time.LocalTime.getSecond()
biz.dto.CarWashDTO.getSecondShift()/java.time.LocalTime.getSecond()

错误是由这段代码造成的

@SpringBootTest
@RunWith(SpringRunner.class)
public class CarWashDTO2CarWash {

@Autowired
protected ModelMapper modelMapper;

@Test
public void testCarWashDTO2CarWash_allFiledShouldBeConverted(){
    CarWashDTO dto = CarWashDTO.builder()
            .name("SomeName")
            .address("SomeAddress")
            .boxCount(2)
            .firstShift(LocalTime.of(9, 0))
            .secondShift(LocalTime.of(20, 0))
            .phoneNumber("5700876")
            .build();

    modelMapper.addConverter((Converter<CarWashDTO, CarWash>) mappingContext -> {
        CarWashDTO source = mappingContext.getSource();
        CarWash destination = mappingContext.getDestination();
        destination.setId(source.getId());
        destination.setFirstShift(source.getFirstShift() == null ? null : Time.valueOf(source.getFirstShift()));
        destination.setSecondShift(source.getSecondShift() == null ? null : Time.valueOf(source.getSecondShift()));
        destination.setEnable(true);
        destination.setAddress(source.getAddress());
        destination.setBoxCount(source.getBoxCount());
        destination.setName(source.getName());
        destination.setDateOfCreation(source.getDateOfCreation());
        return destination;
    });

    final CarWash entity = modelMapper.map(dto, CarWash.class);
    assertNotNull(entity);
    assertEquals(2, entity.getBoxCount().intValue());
    assertEquals("SomeAddress", entity.getAddress());
    assertEquals("SomeName", entity.getName());
}

modelmapper bean是通过接下来的配置构建的

@Bean
public ModelMapper modelMapper(){
    return new ModelMapper();
}

Dto:

public class CarWashDTO {
private Long id;
private String name;
private String address;
private String phoneNumber;
private Integer boxCount;
private LocalTime firstShift;
private LocalTime secondShift;
private LocalDateTime dateOfCreation;
}

实体(firstShift 和 secondShift 有 java.sql.Time 类型):

public class CarWash {
private Long id;
private String name;
private String address;
private String phoneNumber;
private Integer boxCount;
private Time firstShift;
private Time secondShift;
private LocalDateTime dateOfCreation;
private Boolean enable;
private Owner owner;
}

最佳答案

试试 modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT)

关于java - ModelMapper:匹配多个源属性层次结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49831753/

相关文章:

java - 使用数组的通用队列

java - Jackson 不会将 XML 文件完全解析为 JSON

java - 模型映射器空值跳过

java - eclipse - 为日志方法的参数创建模板

java - Shedlock 仅选择某些服务器

Java:将 List<List<String>> 转换为 List<List<Double>>

java - 删除从输入文件到 spring 批处理程序的所有行中的尾随空格

java - 如何防止模型映射器在 spring data jpa 中急切加载关联集合?

java - 尝试定义自定义 PropertyMap 时出现 NullPointerException

java - ModelMapper:如何映射到已知具体类型的通用实例列表