简体   繁体   English

ModelMapper和LocalDate-Spring Boot

[英]ModelMapper and LocalDate - Spring Boot

currently I'm trying to map a dto to a class that also contains LocalDate attribute. 目前,我正在尝试将dto映射到还包含LocalDate属性的类。 Merely I have no success here and the local date field always remains null. 仅仅我在这里没有成功,本地日期字段始终为空。 So I built a short example, where I followed the pretty helpful hints from Modelmapper to convert from String to LocalDate So I have a ModelMapper class like this : 因此,我构建了一个简短的示例,其中我遵循了Modelmapper的非常有用的提示,将字符串从String转换为LocalDate,所以我有一个像这样的ModelMapper类:

    @Bean
public ModelMapper createMapper() {
    ModelMapper modelMapper = new ModelMapper();
    modelMapper.createTypeMap(String.class, LocalDate.class);
    Provider<LocalDate> localDateProvider = new AbstractProvider<LocalDate>() {
        @Override
        public LocalDate get() {
            return LocalDate.now();
        }
    };

    Converter<String, LocalDate> toStringDate = new AbstractConverter<String, LocalDate>() {
        @Override
        protected LocalDate convert(String source) {
            DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate localDate = LocalDate.parse(source, format);
            return localDate;
        }
    };

    modelMapper.getTypeMap(String.class, LocalDate.class).setProvider(localDateProvider);
    modelMapper.addConverter(toStringDate);
    return modelMapper;
}

Furthermore I have a POJO that only has 2 fields, an id and a local date (w/o getters and setters for the sake of readability. 此外,我有一个POJO,它只有2个字段,一个id和一个本地日期(出于可读性考虑,不带getter和setter)。

public class JsonLocalDate {

private Long id;

@JsonFormat(pattern = "yyyy-MM-dd")
private LocalDate ld;

public Long getId() {
    return id;
}

And I created a test class where I tried to mock the json part by a LinkedHashMap as it comes in the web services I have implemented : 我创建了一个测试类,在其中我尝试通过LinkedHashMap模拟json部分,因为它已实现了Web服务:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.NONE)
public class ModelMapperTest {

@Autowired
ModelMapper mapper;

String jsonLd = "2018-06-11";

LinkedHashMap<String, String> lhm;

@Before
public void init() {
    lhm = new LinkedHashMap<>();
    lhm.put("id", "1");
    lhm.put("ld", jsonLd);
}

@Test
public void checkModelMapper() {
    assertNotNull(mapper);
    Collection<TypeMap<?, ?>> c = mapper.getTypeMaps();
    assertNotNull(c);
    for (TypeMap<?, ?> typeMap : c) {
        System.out.println("TypeMap : " + typeMap.getConverter().toString());
    }
}

@Test
public void testLocalDate() {
    LocalDate ld = mapper.map(jsonLd, LocalDate.class);
    assertNotNull(ld);
    assertEquals(ld, LocalDate.parse(jsonLd, 
DateTimeFormatter.ofPattern("yyyy-MM-dd")));
}

@Test
public void testLocalDateInObject() {
    JsonLocalDate jld = mapper.map(jsonLd, JsonLocalDate.class);
    assertNotNull(jld);
    assertEquals(jld.getLd(), LocalDate.parse(jsonLd, 
DateTimeFormatter.ofPattern("yyyy-MM-dd")));
}

}

testLocalDate where I just map the String to LocalDate works fine, while the testLocalDateInObject fails. 我仅将字符串映射到LocalDate的testLocalDate正常工作,而testLocalDateInObject失败。

Has anybody any idea how I have to deal with LocalDate fields to get them mapped ? 有谁知道我必须如何处理LocalDate字段才能将它们映射?

Thanks in advance ! 提前致谢 !

Cheers 干杯

Joern Joern

The test failed is because you are trying to map a String to an Object and ModelMapper doesn't know how to map a String to an Object. 测试失败是因为您试图将字符串映射到对象,而ModelMapper不知道如何将字符串映射到对象。

So you should try Property Mapping 所以你应该尝试属性映射

modelMapper.typeMap(String.class, JsonLocalDate.class)
    .addMapping(src -> src, JsonLocalDate::setLd);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM