Joern
Joern

Reputation: 41

ModelMapper and LocalDate - Spring Boot

currently I'm trying to map a dto to a class that also contains LocalDate attribute. 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 :

    @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.

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 :

@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.

Has anybody any idea how I have to deal with LocalDate fields to get them mapped ?

Thanks in advance !

Cheers

Joern

Upvotes: 0

Views: 4668

Answers (1)

Chun Han Hsiao
Chun Han Hsiao

Reputation: 381

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.

So you should try Property Mapping

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

Upvotes: 1

Related Questions