Monday, 15 June 2015

java - ModelMapper changes generic type on runtime - weird behavior -


I am facing a very strange behavior in Java, I get two different classes that do not have a hierarchical connection : Class templates (type hierarchy object -> a -> b -> template), and square template deto (object -> template dto).

I for model mapping (Org.modelmapper.ModelMapper) for mapping between two classes (which uses the same mapping from field names).

The following is the code:

  List & lt; Template & gt; Templates = cvService.getTemplates (); & Lt; TemplateDto & gt; Results = New Arrestist & gt; TemplatesDo & gt; (); ModelMapper.map (templates, resultDtos); TemplateDto Example = Results Dtos.get (0);  

and throws the last row:

  java.lang.ClassCastException: com.vs.framework.domain.cv.Template should not be included in the comm can. Vs.framework.dto.cv.TemplateDto  

This is very strange. When I want to debug this segment, after the mapping, the result is that instead of the list there is a list of inventory list which blows my brain.

I have tried to clean my tomatoes, but it is still clear.

Any ideas?

Java , which means that the runtime code should be known There is no way that your ArrayList a ArrayList & gt; TemplateDto & gt; is considered.

To get this problem with the list, to use a TypeToken , this should look something like this:

  list & lt; Templates & gt; Templates = cvService.getTemplates (); Type list type = new typography & lt; List & gt; TemplatesDown & gt; & Gt; () {} .getType (); & Lt; TemplateDto & gt; ResultDtos = modelMapper.map (templates, list type); TemplateDto Example = Results Dtos.get (0);  

No comments:

Post a Comment