Skip to content
Advertisement

Issue when mapping Collection of List using ModelMapper in Spring Boot

I have a Project domain which I want to map on a ProjectDTO using modelmapper. The Project domain contains a List of Locations. In the DTO, I don’t want the Location domains, just the ids of the locations.

JavaScript

So I followed the tutorial of Link and created a custom converter:

JavaScript

Then, I created the PropertyMap to tell modelmapp to use the converter for that field:

JavaScript

Finally, I created a bean in my SpringBootApplication class:

JavaScript

But it is not working, I get errors when starting the application.

JavaScript

Here the full ProjectConverter class:

JavaScript

Any ideas what I do wrong?

Advertisement

Answer

This is a bug of ModelMapper, fixed on version 2.3.2 or later:

In addition, NPE problem on locationToLongConverter#convert() should be fixed:

JavaScript

By the way, this code could be written more simply:

JavaScript

(ProjectConverter.java is not required.)

refs:

Advertisement